WM_PRINTCLIENT to BMP coming out all black

2019-09-09 16:05发布

I am new to working with MFC and bitmaps. I have a HWND, which I want to print onto a bitmap using WM_PRINTCLIENT. This is what I have thus far:

EDIT:

CRect rcWindow;
GetClientRect(hWnd, &rcWindow);         

HDC hDC = GetDC(hWnd);          
HDC hBitmapDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rcWindow.Width(), rcWindow.Height());             

SelectObject(hBitmapDC, hBitmap);

SendMessage(hWnd, WM_PRINTCLIENT, (WPARAM)hBitmapDC, PRF_CHILDREN | PRF_CLIENT | PRF_NONCLIENT);                                

CImage image;
image.Attach(hBitmap);
image.Save(_T("C:\\Test.bmp"), Gdiplus::ImageFormatBMP);

This however results in a bitmap that is all black. Can anyone see what I am doing wrong?

1条回答
叛逆
2楼-- · 2019-09-09 16:52

Try the following :

    HDC hBitmapDC = ::CreateCompatibleDC(hDC);
    HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, rcWindow.Width(), rcWindow.Height());
    ::SelectObject(hBitmapDC, hBitmap);

    // Blt the existing background into the bitmap DC
    ::BitBlt(hBitmapDC, 
             0, 0, rcWindow.Width(), rcWindow.Height(), 
             hDC, rcWindow.left, rcWindow.top, SRCCOPY);

Don't forget to delete the bitmap object using ::DeleteObject, and the bitmap DC using DeleteDC, when you've finished with them...

Hope this helps

查看更多
登录 后发表回答