Wednesday, December 19, 2007

Embedding a Screensaver in a WinForm (Think "Application Level Privacy Screen")

Mark Schmidt's Abode - Embedding Screensaver's in a Windows Form

"Awhile ago I created the screensaver Vista Sidebar gadget and promised I would put some code up. As promised, here it is. It's actually quite easy to create something like this, and truth be told I actually had the code lying around for a few years since I wrote it for my 2nd book. Rather than confuse you with any sidebar code, I just created a simple Windows Form that hosts the screensaver UserControl (full source code can be downloaded below).

The hardest part (which is actually somewhat easy) is telling Windows to launch the screensaver within a certain window rather than fullscreen. You've seen this window in the control panel and may have wondered how it's done. To do this, you simply launch the screensaver (.scr file) and pass a command line parameter (/p for preview) and your window handle. The window handle can be accessed in a Windows Form or UserControl through the Handle property.

Below is the code that embeds a screensaver in my custom UserControl. ...

...

"

When I saw this example the first use case that came to mind was to use it to create a application level privacy screen.

Say you have a LOB WinForm application that contains sensitive data. Normally you might force a system wide screensaver policy or an application level timeout. Well an application should live in the app. IMHO I don't think it's polite for a user LOB app to take over the entire system (like forcing a system screensaver setting, etc).

Or maybe you have multiple monitors and you run the LOB app on one and other stuff on the other. And from a security point of view you want to LOB app to "hide" without affecting other app's, yet you don't want the LOB app to log off (and maybe lose work in process, etc).

So I'm thinking an application level privacy screen... And you might as well make it cool by using a Windows Screensaver... :)

Put a screensaver in a UserControl, hook into the app's mouse and keyboard events and if the app times out force that control to the top, fill the form and launch the screensaver. Security, privacy and coolness.  ;)

No comments: