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

首頁 > 編程 > C++ > 正文

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

2020-05-23 13:30:34
字體:
供稿:網(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)存畫板  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);  //選中原來的畫筆  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;  //寫入文件  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); } 

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 偃师市| 墨竹工卡县| 西充县| 册亨县| 西和县| 沐川县| 新化县| 张家港市| 乌拉特前旗| 辽源市| 长宁县| 海南省| 连平县| 龙游县| 宁乡县| 鄢陵县| 浑源县| 舟曲县| 綦江县| 武城县| 手游| 蒙城县| 临夏市| 忻州市| 遵化市| 进贤县| 兖州市| 景洪市| 宝应县| 张家界市| 沙田区| 灵寿县| 廊坊市| 饶阳县| 富川| 苍山县| 蒙山县| 措勤县| 达尔| 乌拉特后旗| 岳普湖县|