Copying content from a hidden or clipped window in XP?

What you need is the PrintWindow function that’s available in Win32 API since Windows XP. If you need it to work with older versions of Windows, you can try WM_PRINT, although I’ve never been able to make it work.

There’s a nice article here that shows how to use PrintWindow, and here’s the relevant code snippet from that article:

// Takes a snapshot of the window hwnd, stored in the memory device context hdcMem
HDC hdc = GetWindowDC(hwnd);
if (hdc)
{
    HDC hdcMem = CreateCompatibleDC(hdc);
    if (hdcMem)
    {
        RECT rc;
        GetWindowRect(hwnd, &rc);

        HBITMAP hbitmap = CreateCompatibleBitmap(hdc, RECTWIDTH(rc), RECTHEIGHT(rc));
        if (hbitmap)
        {
            SelectObject(hdcMem, hbitmap);

            PrintWindow(hwnd, hdcMem, 0);

            DeleteObject(hbitmap);
        }
        DeleteObject(hdcMem);
    }
    ReleaseDC(hwnd, hdc);
}

I should have some Python code that uses wxPython to achieve the same thing. Drop me a note if you want it.

Leave a Comment