Get screenshot of startmenu

2019-06-17 10:30发布

问题:

I am using bitblt to to capture a window. If the aero theme is enabled, The background of the captured image is black. If I disable the DWM and capture the window then the captured image is very good.

Here is part of my code.

HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(desktopDC);
HDC windowDC = User32.INSTANCE.GetDC(window);

HWND window= User32Extra.INSTANCE.FindWindow(null, "Start menu");

GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, desktopDC, 0, 0, WinGDIExtra.SRCCOPY );
GDI32Extra.INSTANCE.BitBlt(hdcMemDC,windowBounds.left, windowBounds.top, windowWidth, windowHeight, windowDC, windowBounds.left+windowBounds1.right-windowBounds.right+(windowExtraGap/2), windowBounds.top+windowBounds1.bottom-windowBounds.bottom+(windowExtraGap/2), WinGDIExtra.SRCCOPY);

How to capture the start Menu with proper background?

Are there any other methods to get the proper image of aero window?

回答1:

use desktop DC and cut to window

RECT rc, rc2;
GetClientRect(hWnd, &rc);
GetWindowRect(hWnd, &rc2);
int width = rc2.right - rc2.left;
int height = rc2.bottom - rc2.top;
HDC hdcScreen = GetDC(NULL); //!!!! Get desktop DC

HDC     hBmpFileDC = CreateCompatibleDC(hdcScreen);
HBITMAP hBmpFileBitmap = CreateCompatibleBitmap(hdcScreen, width, height);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBmpFileDC, hBmpFileBitmap);
BitBlt(hBmpFileDC, 0, 0, width, height, hdcScreen, rc2.left, rc2.top, SRCCOPY | CAPTUREBLT);
HGDIOBJ prev = SelectObject(hBmpFileDC, hOldBitmap);

SaveBitmap(szLogFilename, hBmpFileBitmap);

DeleteDC(hBmpFileDC);
DeleteObject(hBmpFileBitmap);

another variant

RECT rc;
GetClientRect(hWnd, &rc);

int width = rc.right - rc.left;
int height = rc.bottom - rc.top;

HDC hdcScreen = GetDC(hWnd);
////////////////////////////
PrintWindow(hWnd, hdcScreen, 0);
PrintWindow(hWnd, hdcScreen, PW_CLIENTONLY);
////////////////////////////    
HDC     hBmpFileDC = CreateCompatibleDC(hdcScreen);
HBITMAP hBmpFileBitmap = CreateCompatibleBitmap(hdcScreen, width, height);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBmpFileDC, hBmpFileBitmap);
BitBlt(hBmpFileDC, 0, 0, width, height, hdcScreen, 0, 0, SRCCOPY | CAPTUREBLT);
HGDIOBJ prev = SelectObject(hBmpFileDC, hOldBitmap);

SaveBitmap(szLogFilename, hBmpFileBitmap);

DeleteDC(hBmpFileDC);
DeleteObject(hBmpFileBitmap);

before call any capture method I call PrintWindow. It acts window to redraw itself. And as a result screen capture will have correct picture. The most stable result I got with double call of PrintWindow.