how to disable "PRINT SCREEN" button while running my Application in WPF?

Kishore Kumar picture Kishore Kumar · Oct 8, 2010 · Viewed 9.1k times · Source

How can I disable Print Screen functionality while my WPF application is running?

The use-case is that my client wants to avoid unnecessary replication of valuable patient-centric data from the outside world and they provide the physical security to keep people from taking data through non-digital means.

Answer

Jodrell picture Jodrell · Mar 14, 2011

Okay, it is possible, and could indeed be useful if your application is deployed in an environment where a camera is not available to the user.

First of all, I used the RegisterHotKey and UnregisterHotKey API calls, documented here http://pinvoke.net/default.aspx/user32.RegisterHotKey as described in this rather old article here http://msdn.microsoft.com/en-us/magazine/cc163713.aspx.

I registered the IDHOT_SNAPDESKTOP hotkey in the Window_Load event and unregistered it in the Window_Closed. Trying to do this in the constructor gave me problems getting a consistent handle with the WindowInteropHelper(this) method.

If you'd like to do more than just ignore the keys you can set up a windows message handler, making a kind of WndProc using,

HwndSource source = HwndSource.FromHwnd(<handle>);
source.AddHook(<WndProc>);

making the handle as described above, and the WndProc implementation yourself.

As yet, I don't know how to "not" handle the hot key and get windows to perform its normal behaviour except, of course, by unregistering the hotkeys.

Its not very elegant or "WPF" but it worked for me.


As @ghord comments

The use of EnsureHandle() looks useful for getting a handler in the constructor.