Programming Windows – Chapter 14 Blow-Up程式的錯誤

來源:互聯網
上載者:User

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

這樣,我們就可以正確將截取的螢幕內容複寫到位元影像了,如下:

 複製內容後: 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.