C++實(shí)現(xiàn)屏幕截圖功能

本文實(shí)例為大家分享了C++實(shí)現(xiàn)全屏截圖功能的具體代碼,供大家參考,具體內(nèi)容如下

專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)伊吾免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

最近維護(hù)的項(xiàng)目,在某些情況下,光有日志還不行,于是添加了截圖功能,特定情況下,會(huì)自動(dòng)截圖,輔助分析,從而改進(jìn)程序。以下是截圖實(shí)現(xiàn)代碼。

void CDemoDlg::ScreenShot(void) 
{ 
 CWnd *pDesktop = GetDesktopWindow(); 
 CDC *pdeskdc = pDesktop->GetDC(); 
 CRect re; 
 //獲取窗口的大小 
 pDesktop->GetClientRect(&re); 
 CBitmap bmp; 
 bmp.CreateCompatibleBitmap(pdeskdc, re.Width(), re.Height()); 
 //創(chuàng)建一個(gè)兼容的內(nèi)存畫板 
 CDC memorydc; 
 memorydc.CreateCompatibleDC(pdeskdc); 
 //選中畫筆 
 CBitmap *pold = memorydc.SelectObject(&bmp); 
 //繪制圖像 
 memorydc.BitBlt(0, 0, re.Width(), re.Height(), pdeskdc, 0, 0, SRCCOPY); 
 //獲取鼠標(biāo)位置,然后添加鼠標(biāo)圖像 
 CPoint po; 
 GetCursorPos(&po); 
 HICON hinco = (HICON)GetCursor(); 
 memorydc.DrawIcon(po.x - 10, po.y - 10, hinco); 
 //選中原來(lái)的畫筆 
 memorydc.SelectObject(pold); 
 BITMAP bit; 
 bmp.GetBitmap(&bit); 
 //定義 圖像大?。▎挝唬篵yte) 
 DWORD size = bit.bmWidthBytes * bit.bmHeight; 
 LPSTR lpdata = (LPSTR)GlobalAlloc(GPTR, size); 
 //后面是創(chuàng)建一個(gè)bmp文件的必須文件頭 
 BITMAPINFOHEADER pbitinfo; 
 pbitinfo.biBitCount = 24; 
 pbitinfo.biClrImportant = 0; 
 pbitinfo.biCompression = BI_RGB; 
 pbitinfo.biHeight = bit.bmHeight; 
 pbitinfo.biPlanes = 1; 
 pbitinfo.biSize = sizeof(BITMAPINFOHEADER); 
 pbitinfo.biSizeImage = size; 
 pbitinfo.biWidth = bit.bmWidth; 
 pbitinfo.biXPelsPerMeter = 0; 
 pbitinfo.biYPelsPerMeter = 0; 
 GetDIBits(pdeskdc->m_hDC, bmp, 0, pbitinfo.biHeight, lpdata, (BITMAPINFO*) 
  &pbitinfo, DIB_RGB_COLORS); 
 BITMAPFILEHEADER bfh; 
 bfh.bfReserved1 = bfh.bfReserved2 = 0; 
 bfh.bfType = ((WORD)('M' << 8) | 'B'); 
 bfh.bfSize = size + 54; 
 bfh.bfOffBits = 54; 
 //寫入文件 
 CFile file; 
 CString strFileName(GetAppPathW().c_str()); 
 strFileName += _T("ScreenShot\\"); 
 CreateDirectory((LPCTSTR)strFileName, NULL); 
 CTime t = CTime::GetCurrentTime(); 
 CString tt = t.Format("%Y-%m-%d_%H-%M-%S"); 
 strFileName += tt; 
 strFileName += _T(".bmp"); 
 if (file.Open((LPCTSTR)strFileName, CFile::modeCreate | CFile::modeWrite)) 
 { 
  file.Write(&bfh, sizeof(BITMAPFILEHEADER)); 
  file.Write(&pbitinfo, sizeof(BITMAPINFOHEADER)); 
  file.Write(lpdata, size); 
  file.Close(); 
 } 
 GlobalFree(lpdata); 
} 

代碼注釋詳盡,我就不多說(shuō)啥了,保存為bmp無(wú)損文件,體積會(huì)有些大,朋友可以轉(zhuǎn)化為png格式保存哦。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前題目:C++實(shí)現(xiàn)屏幕截圖功能
URL鏈接:http://bm7419.com/article28/pciecp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、、企業(yè)網(wǎng)站制作、商城網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)