截取屏幕图
2015-07-04 17:40
253 查看
void CtestDlg::OnBnClickedBtnOk() { HWND hwnd = ::GetDesktopWindow(); RECT rect; ::GetClientRect(hwnd,&rect); CClientDC *pDC=new CClientDC(this); CBitmap Bitmap; HBITMAP hBitmap=CopyScreenToBitmap(&rect); Bitmap.Attach(hBitmap); CDC DCCompatible; DCCompatible.CreateCompatibleDC(pDC); DCCompatible.SelectObject(&Bitmap); BITMAP bmInfo; Bitmap.GetObject(sizeof(bmInfo),&bmInfo); pDC->BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight, &DCCompatible, 0,0,SRCCOPY); delete pDC; } HBITMAP CtestDlg::CopyScreenToBitmap(LPRECT lpRect) { if (IsRectEmpty(lpRect)) return NULL; //为屏幕创建设备描述表 HDC hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL); //为屏幕设备描述表创建兼容的内存设备描述表 HDC hMemDC = CreateCompatibleDC(hScrDC); // 获得选定区域坐标 int nX=lpRect->left; int nY=lpRect->top; int nX2=lpRect->right; int nY2=lpRect->bottom; // 获得屏幕分辨率 int xScrn=GetDeviceCaps(hScrDC, HORZRES); int yScrn=GetDeviceCaps(hScrDC, VERTRES); //确保选定区域是可见的 if(nX<0) nX= 0; if(nY<0) nY= 0; if(nX2>xScrn) nX2 = xScrn; if(nY2>yScrn) nY2 = yScrn; // 位图宽度和高度 int nWidth = nX2 - nX; int nHeight = nY2 - nY; //创建一个与屏幕设备描述表兼容的位图 HBITMAP hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight); //把新位图选到内存设备描述表中 HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); //把屏幕设备描述表拷贝到内存设备描述表中 BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY); //得到屏幕位图的句柄 hBitmap=(HBITMAP)SelectObject(hMemDC, hOldBitmap); //清除 DeleteDC(hScrDC); DeleteDC(hMemDC); // 返回位图句柄 return hBitmap; }
相关文章推荐
- error C2440: ‘static_cast’ : cannot convert from ‘UINT (__thiscall CMenuBar::* )(CPoint)’ to ‘LRESUL
- 浏览器
- 新浪微薄份额2(分享)
- JavaScript中的this陷阱的最全收集
- ios开发流程详解
- Factorization Machines 学习笔记(四)学习算法
- [转载] tmux的使用tips
- 新手安装ubuntu问题解决(easyBCD没作用,分区,双显卡闪屏等)
- adb脚本输出android调试信息
- 最终时刻
- Mysql User表为空
- 计算几何:线段判交模板--ACM
- java web之Filter详解
- Swift开发教程--Swift的基本语法
- gcc 已知的问题及规避方案 https://gcc.gnu.org/bugs/#known
- error C2440: ‘static_cast’ : cannot convert from ‘HRESULT (__thiscall CtestpalView::* )(WPARAM,LPARA
- javascript prototype详解
- android 随着认识的去除率EditText(它配备了防抖效果)
- (转)Sublime Text 2 2.0.2 序列号
- 联系我们如何添加百度地图路线坐标