2014-01-15 34 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'消息? –