Posts Windows Form application in Kiosk mode...
Post
Cancel

Windows Form application in Kiosk mode...

Someone asked how to make sure their application has focus and how to protect against it losing focus while running.  There are actually many applications that could require this sort of capability, one of which would be a kiosk.  Many years ago, I used somewhat of a hack to accomplish this task.  It was very simple and worked very well.  Just replace Explorer.exe as the shell with your application.  There were a few other tricks you could do trick Windows 9x into thinking the screen saver was running so you could prevent the task manager from being displayed.  I did a little checking and the shell replacement trick can still be done today.  You can modify the following registry setting:

[HKEY_LOCAL_MACHINE]\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell = "sol.exe"

This would replace Explorer.exe with sol.exe... now that would be useful. ;-)  Now this trick would work for many scenarios that require the user to only run a single application on the machine, but what if you only want this to happen on a more temporary basis.  Surely there must be some way to do this.

There are probably a few ways to accomplish this and I've put together an example showing at least one way of doing so.  The sample is designed to run as a full screen application and has a single button so you can exit the program.  It has no border and starts up maximized.  By setting the form this way, it will fill the screen.  Next, I use SetWindowsPos to force the window to stay in the foreground (except for the shell, which has priority).  Next, I hook into the the lower level portions of windows to trap keyboard handling prior to other applications being able to see them.  If I see a key combination I want to suppress, I can just swallow the event and all the other applications would be none the wiser.

I've done a bit of research using Google to gather some of this information, enhanced and transformed it a bit and now have what I believe is a much better implementation of this than any of the other examples I could find.  For example, you don't necessarily have to call upon Enable to re-enable the keys... the class implements IDisposable; so if you forget or would like to use Dispose, your still safe.

[disclaimer] I haven't really done a whole lot of testing with this code, so venture forth accordingly.

Download the code.

This post is licensed under CC BY 4.0 by the author.