简体   繁体   中英

How to exclude certain window from screen capture in Win32 API?

I want to capture the desktop and exclude a window of my application of being captured.

My window is being created as follows:

m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_LAYERED,
                        g_lpszClassName, NULL, WS_THICKFRAME,
                        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL,
                        _WPModule.GetHInstance(), NULL);

And I capture the screen as follows:

HWND    hWndCapture     = ::GetDesktopWindow();
HDC     hdcScreen       = ::GetDC(hWndCapture);
HDC     hdcMem          = ::CreateCompatibleDC(hdcScreen);

::BitBlt(
    hdcMem,
    0,
    0,
    Width, //width of region of interest
    Height, //height of region of interest
    hdcScreen,
    X, //left staring point for capture
    Y, //top staring point for capture
    SRCCOPY);

I have found the following link Excluding certain windows from screen capture but it does not give a solution for IE 8 and onward. I did not find any other solution posted for this issue.

I'm aware this question is pretty old, but I ran into the same problem and it was very, very hard to find any information at all regarding this.

Since Windows 10 version 2004 (build 10.0.19041), the SetWindowDisplayAffinity API has been expanded to include a flag called WDA_EXCLUDEFROMCAPTURE (0x00000011). This will remove the window from images captured with BitBlt

The window is displayed only on a monitor. Everywhere else, the window does not appear at all. One use for this affinity is for windows that show video recording controls, so that the controls are not included in the capture.

Introduced in Windows 10 Version 2004. See remarks about compatibility regarding previous versions of Windows.

For versions before 2004, it will use the existing WDA_MONITOR flag.

I have tested this with a screen capture of the desktop and I am unsure what would happen if you were to use a window DC.

So I guess a possible solution would be:

// get window handle
m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_LAYERED,
                        g_lpszClassName, NULL, WS_THICKFRAME,
                        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL,
                        _WPModule.GetHInstance(), NULL);

BOOL result = SetWindowDisplayAffinity(m_hWnd, WDA_EXCLUDEFROMCAPTURE);

// do bitblt stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM