• Programming Windows Chapter 14 BlowUp程序的错误


    在Programming Windows Chapter 14 Blow-Up的程序中,在完成屏幕截取后,作者使用如下代码复制屏幕内容到位图:

                    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 );
    

    这样,我们就可以正确将截取的屏幕内容复制到位图了,如下:

     复制内容后: 

  • 相关阅读:
    ubuntu 系统命令
    js模板引擎实例一
    读取页面上所有的checkbox
    使用fileReader实现图片预览
    html5中的audio标签针对IOS系统的兼容处理
    CSS单位
    使用变换属性的旋转和动画属性实现大风车效果
    css动画属性--轮播图效果
    php curl详解
    linux权限详解
  • 原文地址:https://www.cnblogs.com/twjcnblog/p/2328704.html
Copyright © 2020-2023  润新知