Capture screenshot as HBITMAP with layered/transparent windows
Here's the code to capture desktop into a bitmap:
HBITMAP CaptureDesktop(void)
{
//Capture desktop into bitmap
HBITMAP hBitmap = NULL;
RECT rcDt;
HWND hDtWnd = ::GetDesktopWindow();
HDC hDtDC = GetDC(hDtWnd);
if(hDtDC && GetClientRect(hDtWnd, &rcDt))
{
//Create mem DC & bitmap
int w = rcDt.right - rcDt.left;
int h = rcDt.bottom - rcDt.top;
HDC hMemDC = CreateCompatibleDC(hDtDC);
hBitmap = CreateCompatibleBitmap(hDtDC, w, h);
if(hMemDC && hBitmap)
{
//Select our bitmap
HGDIOBJ hOldBmp = SelectObject(hMemDC, hBitmap);
//Copy desktop to mem DC
BitBlt(hMemDC, 0, 0, w, h, hDtDC, 0, 0, SRCCOPY);
//Select old bmp
SelectObject(hMemDC, hOldBmp);
}
//Release DCs
DeleteDC(hMemDC);
ReleaseDC(hDtWnd, hDtDC);
}
return hBitmap;
}
But it has one essential flaw -- it doesn't capture any layered/transparent windows (with WS_EX_LAYERED style on). How can I do that?

