How to prevent/disable/protect screen capture of windows application (Winform,WPF,Win32,MFC etc.)
Microsoft has been developed an API named SetWindowDisplayAffinity to support the window content protection. This feature enables ap...
Microsoft has been developed an API named SetWindowDisplayAffinity to support the window content protection. This feature enables applications to protect application content from being captured or copied through a specific set of public operating system features and APIs. Unlike other security features or an implementation of DRM there is no guarantee that this API can strictly protect window content, for example where someone takes a photograph of screen using mobile camera. But this API is feasible and easy to use.
BOOL SetWindowDisplayAffinity(HWND hWnd,DWORD dwAffinity);
There are two types of DWORD values WDA_MONITOR and WDA_NONE. We have to set WDA_MONITOR to protect the application contents.
It's basically C++ API but we can use this for other windows C# application like Winform or WPF by extern the C++ API's. (Sample code C#)
using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsAppPrtScrProtector { public partial class Form1 : Form { const uint WDA_NONE = 0; const uint WDA_MONITOR = 1; [DllImport("user32.dll")] public static extern uint SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { SetWindowDisplayAffinity(this.Handle, WDA_MONITOR); } } }
Note:
For WPF you have to create Handler by your self for SetWindowDisplayAffinity's first
parameter like bellow:
For WPF you have to create Handler by your self for SetWindowDisplayAffinity's first
parameter like bellow:
private void ContentControl_Loaded(object sender, RoutedEventArgs e){ IntPtr hwnd = new WindowInteropHelper(this).Handle; SetWindowDisplayAffinity(hwnd, WDA_MONITOR); }
We can use this API with C++ application easily. Here bellow is the code: (Sample code C++)
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } SetWindowDisplayAffinity(hWnd, WDA_MONITOR); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; }
Happy coding. :)
