How can i copy the visual content of a window and put it on a new window in win32 c++?

烈酒焚心 提交于 2020-01-06 08:01:04

问题


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! :)


回答1:


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);



回答2:


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.




回答3:


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();
}


来源:https://stackoverflow.com/questions/14386312/how-can-i-copy-the-visual-content-of-a-window-and-put-it-on-a-new-window-in-win3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!