DEV Community

Cover image for Hiding a Window from Screen Capture on Windows with SetWindowDisplayAffinity
Dipu Singh
Dipu Singh

Posted on Edited on Fully Autonomous

Hiding a Window from Screen Capture on Windows with SetWindowDisplayAffinity

While building a teleprompter for video creators, we needed a window that the user could see but that recorders like OBS Studio would not capture. Windows has a built-in API for exactly this. Here's how it works and the gotchas we ran into.

The API

BOOL SetWindowDisplayAffinity(HWND hWnd, DWORD dwAffinity);
Enter fullscreen mode Exit fullscreen mode

Three values matter:

Value Effect
WDA_NONE (0x0) Normal window, captured as usual
WDA_MONITOR (0x1) Window shows as black in captures
WDA_EXCLUDEFROMCAPTURE (0x11) Window is removed from captures entirely; content behind it shows instead

WDA_EXCLUDEFROMCAPTURE was added in Windows 10 version 2004 (build 19041). On older builds the call fails, so fall back to WDA_MONITOR or WDA_NONE.

C++ example

#include <windows.h>

void ExcludeFromCapture(HWND hwnd) {
    if (!SetWindowDisplayAffinity(hwnd, WDA_EXCLUDEFROMCAPTURE)) {
        // Older Windows: fall back to a black box rather than leaking content
        SetWindowDisplayAffinity(hwnd, WDA_MONITOR);
    }
}
Enter fullscreen mode Exit fullscreen mode

C# (WinForms / WPF) example

using System.Runtime.InteropServices;

static class CaptureGuard
{
    const uint WDA_EXCLUDEFROMCAPTURE = 0x11;
    const uint WDA_MONITOR = 0x1;

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity);

    public static bool Exclude(IntPtr hwnd) =>
        SetWindowDisplayAffinity(hwnd, WDA_EXCLUDEFROMCAPTURE)
        || SetWindowDisplayAffinity(hwnd, WDA_MONITOR);
}
Enter fullscreen mode Exit fullscreen mode

In WPF, get the handle with new WindowInteropHelper(this).Handle after the window is loaded (SourceInitialized is a good place).

Gotchas

  1. Top-level windows only. The affinity applies to the top-level window. Child windows inherit it, but you can't exclude just one control.
  2. Call it after the window is created. Setting it before the HWND exists does nothing.
  3. Layered/transparent windows work, which is handy for overlays with adjustable opacity.
  4. It's not DRM. It blocks the desktop-duplication and window-capture APIs most recorders use. A phone camera pointed at the monitor still sees everything, and so could a kernel-level tool. Treat it as a privacy/UX feature, not a security boundary.
  5. Test with each recorder you care about. OBS Studio, Xbox Game Bar, Camtasia and Loom are the recorders we support, but behavior can change between versions, so retest after updates.

What we built with it

We used this for TelePrompter Pro, a Windows teleprompter that sits on the same screen you're recording without showing up in the video. The API call itself is one line; most of the work was the scrolling engine, script library and hotkeys.

If you've used SetWindowDisplayAffinity for something else, such as hiding notes during screen shares or protecting sensitive panels, I'd like to hear about it in the comments.

Top comments (0)