Continuing from my previous post, I will explain how to hide your windows form application from Desktop & Taskbar, thus making it completely invisible using a keyboard shortcut. We will use global keyboard hook to achieve this functionality. For those reading the post directly must read my previous post on Easy Way To Achieve Global Keyboard Hook in .NET which explains the design & usage of KeyboardHook class. Below is the code that you would have to add in Program.cs class of your Windows Form Project.
To achieve global keyboard shortcut, we make use of KeyboardHook class by simply calling two methods- one for registering and other for unhooking or unregistering keyboard hook as in line no. 15 and 21 above. Note that HideOrUnhideWindow method will be called each time the user presses keyboard shortcut. When this method is called first time, we get the handle to main windows form and then set firstRun boolean variable to false so that in future runs of above method, we don’t need to get handle again. IsFormHidden boolean variable simply tells the form is currently in which state, depending on that we call ShowWindowAsync method which requires first argument as handle to window and second argument to tell what to do with that window. We use P/Invoke to call method as signature is shown in line no. 44 to 45. Rest of code is pretty easy and well commented. You can download the project from Hiding Windows Form Project – Visual Studio 2008.