我是使用MFC和位图的新手。我有一个HWND
,我想使用WM_PRINTCLIENT
将其打印到位图上。这就是我到目前为止所做的:
编辑:
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);
然而,这导致位图全黑。谁能看到我做错了什么?
答案 0 :(得分:2)
尝试以下方法:
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);
当你完成它们时,不要忘记使用:: DeleteObject删除位图对象,使用DeleteDC删除位图DC ...
希望这有帮助