How to save the client area of a child Window to a Bitmap file?

感情迁移 提交于 2019-11-30 05:34:44
RECT rect     = {0};

GetWindowRect( hwnd, &rect );
ATL::CImage* image_ = new CImage();
image_ -> Create( rect.right - rect.left, rect.bottom - rect.top, 32 );

HDC device_context_handle = image_ -> GetDC();
PrintWindow( hwnd, device_context_handle, PW_CLIENTONLY );
image_ -> Save( filename );
image_ -> ReleaseDC();

delete image_;

PrintWindow() should do the trick.

To save as HBITMAP:

HDC hDC       = GetDC( hwnd );
HDC hTargetDC = CreateCompatibleDC( hDC );
RECT rect     = {0};

GetWindowRect( hwnd, &rect );

HBITMAP hBitmap = CreateCompatibleBitmap( hDC, rect.right - rect.left,
    rect.bottom - rect.top );
SelectObject( hTargetDC, hBitmap );
PrintWindow( hwnd, hTargetDC, PW_CLIENTONLY );
SaveBMPFile( filename, hBitmap, hTargetDC, rect.right - rect.left,
    rect.bottom - rect.top );

DeleteObject( hBitmap );
ReleaseDC( hwnd, hDC );
DeleteDC( hTargetDC );

I will leave the implementation of SaveBMPFile up to you ; )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!