Programming Windows - Chapter 14 Blow-Up程序的错误
2012-01-21 23:23
218 查看
在Programming Windows Chapter 14 Blow-Up的程序中,在完成屏幕截取后,作者使用如下代码复制屏幕内容到位图:
在我自己的Win7-32bit下测试是无法正确复制内容的,因为GetDC( _hWnd )只是获取客户区的DC,无法获得客户区外的数据,效果如下:
复制内容后:
为了正确的复制截取的屏幕内容,我们需要获取屏幕DC,并且使用屏幕坐标来进行操作,代码如下:
这样,我们就可以正确将截取的屏幕内容复制到位图了,如下:
复制内容后:
HDC hdc = GetDC( _hWnd ); HDC hdcMem = CreateCompatibleDC( hdc ); hBitmap = CreateCompatibleBitmap( hdc, abs(ptEnd.x-ptBeg.x), abs(ptEnd.y-ptBeg.y) ); SelectObject( hdcMem, hBitmap ); StretchBlt( hdcMem, 0, 0, abs(ptEnd.x-ptBeg.x), abs(ptEnd.y-ptBeg.y), hdc, ptBeg.x, ptBeg.y, ptEnd.x-ptBeg.x, ptEnd.y-ptBeg.y, SRCCOPY ); DeleteDC( hdcMem ); ReleaseDC( _hWnd, hdc );
在我自己的Win7-32bit下测试是无法正确复制内容的,因为GetDC( _hWnd )只是获取客户区的DC,无法获得客户区外的数据,效果如下:
复制内容后:
为了正确的复制截取的屏幕内容,我们需要获取屏幕DC,并且使用屏幕坐标来进行操作,代码如下:
POINT ptScreenBeg = ptBeg; POINT ptScreenEnd = ptEnd; ClientToScreen( _hWnd, &ptScreenBeg ); ClientToScreen( _hWnd, &ptScreenEnd ); HDC hdc = GetDCEx( hWndDesktop, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE ); HDC hdcMem = CreateCompatibleDC( hdc ); hBitmap = CreateCompatibleBitmap( hdc, abs(ptScreenEnd.x-ptScreenBeg.x), abs(ptScreenEnd.y-ptScreenBeg.y) ); SelectObject( hdcMem, hBitmap ); StretchBlt( hdcMem, 0, 0, abs(ptScreenEnd.x-ptScreenBeg.x), abs(ptScreenEnd.y-ptScreenBeg.y), hdc, ptScreenBeg.x, ptScreenBeg.y, ptScreenEnd.x-ptScreenBeg.x, ptScreenEnd.y-ptScreenBeg.y, SRCCOPY ); DeleteDC( hdcMem ); ReleaseDC( hWndDesktop, hdc );
这样,我们就可以正确将截取的屏幕内容复制到位图了,如下:
复制内容后:
相关文章推荐
- 升级 VS2010 SP1 后程序退出错误,与 theme clean up 相关
- 微信小程序:wx.downloadFile 不成功,没报错 downloadFile:fail Error: socket hang up 错误
- 向程序发送命令时出现错误
- C++程序错误集锦,如:thiscall,_tmain已经定义,重载,未定义的标识符
- 用户 'sa' 登录失败。错误: 18456,严重性: 14,状态: 10
- 微信小程序常见错误及基本排除方法
- 微信小程序开发视频教程学习(第3天)2:PHP测验错误题分析
- Windows IIS 调试ASP 错误号:3706 提示 “未找到提供程序 该程序可能未正确安装”解决办法
- 使用MATLAB的MCC命令生成C/C++程序 C++错误知识
- photoshop 打开图片 无法完成请求因为程序错误
- 共享错误 无任何网络提供程序接受指定的网络路径/找不到网络路径
- android 程序错误处理全局处理
- Microsoft SQL Server 2005 安装程序 错误解决
- 修复ubuntu中检测到系统程序错误
- proc*c第一个程序出现的错误及解决办法
- 安装SQL Server 遇到错误提示:以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机!。
- Debug技巧-------控制台程序或者应用程序输出错误到txt
- XP下正常的程序在Win7/Win8报错,错误码是ERROR_ACCESS_DENIED
- Win7下MingW编译Boost Asio程序的错误解决方法
- 使用Opencv 2.1/2.2 编译64位程序时,编译器内部错误 error c1001