I am using Microsoft Visual Studio 2010 and Windows 7 Professional. This is my code to copy window image to clipboard:
void PrintWindowEx( HWND hWnd )
{
HDC hDCMem = CreateCompatibleDC(NULL);
RECT rect;
GetWindowRect(hWnd, &rect);
HBITMAP hBmp = NULL;
HDC hDC = GetDC(hWnd);
hBmp = CreateCompatibleBitmap(hDC, rect.right - rect.left, rect.bottom - rect.top);
HGDIOBJ hOld = SelectObject(hDCMem, hBmp);
PrintWindow(hWnd, hDCMem, 0);
SelectObject(hDCMem, hOld);
DeleteObject(hDCMem);
OpenClipboard(hWnd);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBmp);
CloseClipboard();
ReleaseDC(hWnd, hDC);
}
It works fine with all windows except Google Chrome main window. I thought that it was because Chrome uses direct rendering, so I have disabled hardware acceleration in chrome://settings
. Still does not work. Then I realized that working with messages can be limited due to restrictions in Chrome Sandbox, so I started Chrome with --no-sandbox
command line option. Still does not work.
How can I get this to work? For Chrome and any other windows like Chrome. BitBlt()
is not acceptable because window some parts of the window may be overlapped by another windows, window can be on other desktop, etc. Is there any working solution? Maybe with DLL loading to another process or something like that.
UPD: It does redraw after RedrawWindow()
; so I can take screenshot (some parts - left part (width ~20px) and right part are not copied). So, does it support WM_PRINT
or not? How can I take full screenshot of the window?