2012-11-26 65 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一个“画”功能。

含义我不复制现有的位图,而是创建一个相同,使用相同的绘图功能。

我还是很想得到这个问题的一个更好的答案。