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