2014-01-15 32 views
0

我是新来MFC和位图的工作。我有一个HWND,我想用WM_PRINTCLIENT打印到位图上。这是我迄今:WM_PRINTCLIENT到BMP全黑色

编辑:

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

然而,这导致了位图,它是全黑的。任何人都可以看到我做错了什么?

+1

您正在为DC创建兼容位图,将其选中到DC中,然后将其打印出来......而实际上没有将任何东西放入位图中...... – Liam

回答

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位图对象,...

希望这可以帮助

+0

像一个魅力一样工作:感谢您的帮助。 –

+0

没问题,很高兴我能帮上忙。 – Liam

+0

我只记得我为什么要使用'WM_PRINTCLIENT'。我需要在我的位图中隐藏客户区以及所有可见的部分。我将如何调整你的答案以使用'WM_PRINTCLIENT'消息? –