国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

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

2020-01-26 13:42:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

最近維護(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)存畫(huà)板  CDC memorydc;  memorydc.CreateCompatibleDC(pdeskdc);  //選中畫(huà)筆  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)的畫(huà)筆  memorydc.SelectObject(pold);  BITMAP bit;  bmp.GetBitmap(&bit);  //定義 圖像大小(單位:byte)  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;  //寫(xiě)入文件  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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 塔城市| 梅州市| 巴东县| 黔东| 阿勒泰市| 沐川县| 渭南市| 荣成市| 云梦县| 承德县| 改则县| 乌兰县| 轮台县| 铜鼓县| 洪湖市| 阿拉善左旗| 沂水县| 桐庐县| 鞍山市| 佛山市| 克拉玛依市| 巴中市| 连江县| 兰考县| 安西县| 濉溪县| 旺苍县| 吐鲁番市| 盈江县| 长泰县| 西吉县| 台中市| 康保县| 贵定县| 军事| 社会| 渝北区| 调兵山市| 始兴县| 阳西县| 奉化市|