How can i copy the visual content of a window and

2019-09-05 09:12发布

I've read something about GetDIBits or BitBlt but i do not understand them.

That could be because i don't understand how Windows actually handles graphics on windows. It would be perfect if someone could refer me to a page where i could learn about these things! :)

3条回答
来,给爷笑一个
2楼-- · 2019-09-05 09:48

I solved the problem using this code in the windows WM_PAINT. It now shows the exact same content as the target window.

PAINTSTRUCT ps;
HDC hdc = BeginPaint(MainWindow, &ps);

HDC TargetDC = GetDC(TargetWindow);

RECT rect;
GetWindowRect(TargetWindow, &rect);

BitBlt(hdc,0,0,rect.right-rect.left,rect.bottom-rect.top,TargetDC,0,0,SRCCOPY);

EndPaint(MainWindow, &ps);
查看更多
疯言疯语
3楼-- · 2019-09-05 09:48

What you want is to:

  1. Get a HWND to the window of which you want the pixels.
  2. Create a memory DC of the correct size (check this out).
  3. Send a WM_PRINTCLIENT or WM_PAINT to the window whilst supplying your memory DC (not all controls/windows implement this though)
  4. Copy the contents of your memory DC to screen

Alternatively for step 3 you can use the DWM or get hacky using the clipboard:

void CopyWndToClipboard(CWnd *pWnd)
{
    CBitmap     bitmap;
    CClientDC   dc(pWnd);
    CDC         memDC;
    CRect       rect;

    memDC.CreateCompatibleDC(&dc);

    pWnd->GetWindowRect(rect);

    bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height());

    CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
    memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);

    pWnd->OpenClipboard() ;
    EmptyClipboard() ;
    SetClipboardData(CF_BITMAP, bitmap.GetSafeHandle()) ;
    CloseClipboard() ;

    memDC.SelectObject(pOldBitmap);
    bitmap.Detach();
}
查看更多
三岁会撩人
4楼-- · 2019-09-05 10:11

You might have some luck with sending the window a WM_PRINTCLIENT message. This may not work well for windows that use DirectX or OpenGL.

You may have some issues using WM_PRINTCLIENT on systems that have Aero enabled (i.e. when the DWM is active). If the system DOES have DWM active then it may offer ways of getting at the window backing store but I have not looked into doing that in any great depth before.

查看更多
登录 后发表回答