How to Exclude Your Windows App from Screen Capture and Recall

 
 
  • Gérald Barré

Some applications are displaying sensitive information. With Recall, Windows can take a screenshot of your screen regularly and store sensitive information.

You can exclude your application from this feature, and screen capture more generally, by using SetWindowDisplayAffinity and the WDA_EXCLUDEFROMCAPTURE flag. This is a Windows API function that allows you to set the display affinity of a window, which determines how the window interacts with screen capture and recording software.

  1. Add NuGet package Microsoft.Windows.CsWin32

  2. Add a NativeMethods.txt file to your project with the following code:

    NativeMethods.txt
    SetWindowDisplayAffinity
  3. Add the following code to your project:

    C#
    unsafe
    {
        // WPF: new WindowInteropHelper(Application.Current.MainWindow).Handle
        // WinForms: this.Handle
        var windowHandle = (HWND)Handle;
        Windows.Win32.PInvoke.SetWindowDisplayAffinity(
            window,
            Windows.Win32.UI.WindowsAndMessaging.WINDOW_DISPLAY_AFFINITY.WDA_EXCLUDEFROMCAPTURE);
    }

To test the flag is working, you can use the Snipping Tool or any other screen capture software. If the flag is set correctly, the application window should not be captured.

Note that it is possible to enable and disable the WDA_EXCLUDEFROMCAPTURE flag on demand. So, you can use it to hide sensitive information only when needed.

#Additional resources

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub