Copying content from a hidden or clipped window in XP?

前端 未结 10 2092
傲寒
傲寒 2020-12-01 11:52

I need to copy the content of a window (BitBlt) which is hidden, to another window. The problem is that once I hide the source window, the device context I got isn\'t painte

10条回答
  •  清歌不尽
    2020-12-01 12:02

    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.

提交回复
热议问题