2012-11-26 79 views
2

我創建了一個分層窗口(帶有WS_EX_LAYERED),大小約爲400X300像素。
當繪製窗口(使用UpdateLayeredWindow)一切都很正常。從分層窗口獲取HBITMAP - 不正確的數據

的問題是,我無法繪製它後得到窗口的HBITMAP。 當試圖通過窗口的HDC獲取HBITMAP時,我得到一個空的(黑色)位圖,這是我整個桌面的大小(1920X1080像素,全尺寸爲400X300像素)。

是否有人知道它甚至可能得到一個分層窗口的HDC \ HBITMAP?

代碼樣本

這裏是我如何繪製分層窗口的代碼(同樣,偉大工程):

void MyLayeredWindow::DrawLayered() const 
{ 
    RECT rcWindow; 
    GetWindowRect(rcWindow); 

    int nWidth = abs(rcWindow.right - rcWindow.left); 
    int nHeight = abs(rcWindow.bottom - rcWindow.top); 

    // Create 32Bit bitmap to apply transparency 
    // (have to set negative height because if not the (0,0) point would be the bottom left instead of top left) 
    VOID *ppvBits = NULL; 
    BITMAPINFO BitmapInfo = {0}; 
    BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    BitmapInfo.bmiHeader.biWidth = nWidth; 
    BitmapInfo.bmiHeader.biHeight = -nHeight; 
    BitmapInfo.bmiHeader.biPlanes = 1; 
    BitmapInfo.bmiHeader.biBitCount = 32; 
    BitmapInfo.bmiHeader.biCompression = BI_RGB; 

    // Copy view buffer to a temp DC and bitmap 
    HDC hDcTemp = ::CreateCompatibleDC(NULL); 
    assert(hDcTemp); 
    HBITMAP hBitmapTemp = ::CreateDIBSection(hDcTemp, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0); 
    assert(hBitmapTemp && hBitmapTemp!=(HBITMAP)ERROR_INVALID_PARAMETER) 
    ::SelectObject(hDcTemp, hBitmapTemp); 

    // Darwing the window's conent here 
    // .... 
    // .... 

    // Call UpdateLayeredWindow 
    BLENDFUNCTION blend = {0}; 
    blend.BlendOp = AC_SRC_OVER; 
    blend.SourceConstantAlpha = 190; 
    blend.AlphaFormat = AC_SRC_ALPHA; 

    SIZE sizeWnd = {0}; 
    sizeWnd.cx = nWidth; 
    sizeWnd.cy = nHeight; 
    POINT ptPos = {0}; 
    ptPos.x = rcWindow.left; 
    ptPos.y = rcWindow.top; 
    POINT ptSrc = {0,0}; 

    ::UpdateLayeredWindow(m_hWnd, NULL, &ptPos, &sizeWnd, hDcTemp, &ptSrc, 0, &blend, ULW_ALPHA); 

    if(hDcTemp) 
     ::DeleteDC(hDcTemp); 

    if(hBitmapTemp) 
     ::DeleteObject(hBitmapTemp); 
} 

這裏是我如何捕獲的窗口的位圖,並將其保存到代碼文件: (注意:它工作的 「正常」 的窗口,例如計算器)

bool MyLayeredWindow::SaveBitmapFile(__in const HWND& hWnd, __in const wstring& sFileName) 
{ 
    HDC hDC = ::GetDC(hWnd); 

    // get bitmap of DC 
    HBITMAP hBmp = (HBITMAP)::GetCurrentObject(hDC, OBJ_BITMAP); 

    // get info of bitmap 
    BITMAPINFO stBmpInfo; 
    stBmpInfo.bmiHeader.biSize = sizeof(stBmpInfo.bmiHeader); 
    stBmpInfo.bmiHeader.biBitCount = 0; 
    GetDIBits(hDC, hBmp, 0, 0, NULL, &stBmpInfo, DIB_RGB_COLORS); 

    // init info size 
    ULONG iBmpInfoSize; 
    switch(stBmpInfo.bmiHeader.biBitCount) 
    { 
    case 24: 
     iBmpInfoSize = sizeof(BITMAPINFOHEADER); 
     break; 
    case 16: 
    case 32: 
     iBmpInfoSize = sizeof(BITMAPINFOHEADER)+sizeof(DWORD)*3; 
     break; 
    default: 
     iBmpInfoSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1 << stBmpInfo.bmiHeader.biBitCount); 
     break; 
    } 

    // copy header 
    PBITMAPINFO pstBmpInfo = NULL; 
    if(iBmpInfoSize != sizeof(BITMAPINFOHEADER)) 
    { 
     pstBmpInfo = (PBITMAPINFO)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, iBmpInfoSize); 
     PBYTE pbtBmpInfoDest = (PBYTE)pstBmpInfo; 
     PBYTE pbtBmpInfoSrc = (PBYTE)&stBmpInfo; 
     ULONG iSizeTmp = sizeof(BITMAPINFOHEADER); 

     while(iSizeTmp--) 
      *((pbtBmpInfoDest)++) = *((pbtBmpInfoSrc)++); 
    } 

    // create file 
    HANDLE hFile = CreateFile(sFileName.c_str(), GENERIC_WRITE, 0 , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL); 

    // init bmp file header 
    BITMAPFILEHEADER stBmpFileHder = {0}; 
    stBmpFileHder.bfType = 0x4D42; // 'BM' 
    stBmpFileHder.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + iBmpInfoSize + pstBmpInfo->bmiHeader.biSizeImage; 
    stBmpFileHder.bfReserved1 = 0; 
    stBmpFileHder.bfReserved2 = 0; 
    stBmpFileHder.bfOffBits = sizeof(BITMAPFILEHEADER) + iBmpInfoSize; 

    // write header to file 
    DWORD dRet; 
    WriteFile(hFile, (LPCVOID)&stBmpFileHder, sizeof(BITMAPFILEHEADER), &dRet, NULL); 

    // allocate size for rest of bmp (body) 
    PBYTE pBits = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, stBmpInfo.bmiHeader.biSizeImage); 

    // get bmp bits 
    HBITMAP hBmpOld; 
    HBITMAP hTmpBmp = CreateCompatibleBitmap(hDC, pstBmpInfo->bmiHeader.biWidth, pstBmpInfo->bmiHeader.biHeight); 
    hBmpOld = (HBITMAP)SelectObject(hDC, hTmpBmp); 
    GetDIBits(hDC, hBmp, 0, pstBmpInfo->bmiHeader.biHeight, (LPSTR)pBits, pstBmpInfo, DIB_RGB_COLORS); 

    // write bmp info 
    WriteFile(hFile, (LPCVOID)pstBmpInfo, iBmpInfoSize, &dRet, NULL); 

    // write bmp bits 
    WriteFile(hFile, (LPCVOID)pBits, pstBmpInfo->bmiHeader.biSizeImage, &dRet, NULL); 

    // release handles and free memory 
    SelectObject(hDC, hBmpOld); 
    DeleteObject(hTmpBmp); 
    CloseHandle(hFile); 
    GlobalFree(pstBmpInfo); 
    GlobalFree(pBits); 
    ReleaseDC(hWnd, hDC); 

    return true; 
} 

謝謝!

+0

該方法通常是錯誤的,只能偶然使用。一個窗口沒有位圖,你需要使用BitBlt或PrintWindow。對分層窗口使用CAPTURE_BLT選項。 http://msdn.microsoft.com/en-us/library/windows/desktop/dd183402%28v=vs.85%29.aspx –

+0

BitBlt和PrintWindow似乎不適用於我的分層窗口。它返回一個空白(黑色\透明)DC。 –

回答

0

因爲我沒有得到任何更好的答案,我只是給那畫我的分層窗口,到臨時HDC一個「畫」功能。

含義我不復制現有的位圖,而是創建一個相同,使用相同的繪圖功能。

我還是很想得到這個問題的一個更好的答案。