The program presented here allows you to minimize any open Window to the system tray with just one click. After reading this article you will know how to call unmanaged code from a .NET application using P/Invoke.

All the functionality of this application is achieved using Windows API functions so you should be familiar with basic winapi programming. Consequently you should know what P/Invoke is and how it works.

P/Invoke or Platform Invoke allows you to call unmanaged functions from managed code. To do that you need to declare a method and specify the name of the DLL file which contains the method using DllImport attribute. For example here is a declaration needed to call ShowWindow function from a C# application:

extern keyword is used to indicate that the method is implemented externally. Perhaps the most difficult task with P/Invoke is to know how to substitute unmanaged types with managed types. This is a great website which has lots of winapi function declarations. There are many tutorials about P/Invoke so I won't go into more details here. See references for the links.

When you run the application it hides the main form and an icon is shown in the system tray. When you right-click the icon a list of all visible Windows is displayed. If you click one of them then that Window will disappear and a new icon will appear in the system tray. The icon is the same as the icon of the executable that launched the Window. Clicking on the icon will bring the Window back. Apart from this there are two commands available to minimize all Windows to tray or to show all minimized Windows. Both commands have hotkeys associated with them and they can be changed from the options Window. To open the options Window just double-click the main icon. Other options include adding the program to the start-up program list and ignoring Windows without a title.

When you right-click the icon a list of all visible Windows appears. To enumerate all Windows you should call winapi function called EnumWindows. The function takes two parameters. The first one is a pointer to a callback function. The code snippet below shows how this function works:

When a user clicks on one of the Windows we need to hide it. ShowWindow() API is used to achieve this. Just pass the handle to the Window as a first parameter and SW_HIDE constant (which is equal to zero) as a second parameter.

The Window is hidden so we need to retrieve the icon of the executable file that created this Window. To achieve this we need to get the path of the executable first. These are the required steps:

Get the process id that created the specified Window using GetWindowThreadProcessId() function

Get a handle of the process by OpenProcess() function

Get the executable path by calling GetModuleFileNameEx() function

These steps were suggested by Mark Salsbery here: retrieve executable path by hwnd. All these functions are winapi functions imported by dllimport attribute. Here is the actual implementation ported to C#.

To retrieve the path to the executable file you can also use GetProcessImageFileName API but it returns a path in device form and works on XP and higher. GetWindowModuleFileName seemed to be another option but it works only if the specified Window is owned by the calling process.

Now when we have the path to the executable, we can extract the icon that is displayed by explorer using the SHGetFileInfo() function. You need to pass the path of the executable and an instance of SHFILEINFO structure that will receive the result. After you have retrieved a handle to the icon you must call DestoyIcon() API to prevent a memory leak.

At this point we have the necessary icon so we just create and show new NotifyIcon and setup its click event handler. When the click event occurs, the corresponding Window is shown and NotifyIcon is destroyed.

When an icon is clicked we need to display the corresponding Window. First check if there exists a Window with the specified handle and then call ShowWindow() again. This time the second parameter depends on the state of the window. If the Window was maximized (minimized) while hiding it, it will be shown maximized (minimized).

This program allows to customize hotkeys for two commands: 'all to tray' and 'show all'. To add hotkey functionality to your application, first you need to register it. RegisterHotKey() is a function that is responsible for doing it. It takes four parameters:

Handle to the window that will receive WM_HOTKEY messages generated by the hot key

ID of the hotkey

Combination of modifier keys (Alt, Ctrl, Shift etc.)

The key itself that should be pressed. The code below registers hotkey for 'All to tray' command

To handle the WM_HOTKEY message generated by the hotkey in a .NET application you should override WndProc function which processes Windows messages. Don't forget to call WndProc method of the base class so that other messages get handled.

When the application exits or hotkey changes we don't need the existing hotkey any more so we should unregister it. It is easier then registering the hotkey and is achieved by calling UnregisterHotKey() function. Just pass the handle of the window and ID of the hotkey:

You can add the program to start-up from the options Window. To add program to start-up you need to navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run key, create a new string and set its value equal to the application's path. Removing the program from start-up is easier: you just remove the value. The code snippet below shows how to do it:

If you try to launch the second instance of the application you will get a message box saying that it is already running. This is achieved using mutex class. Mutex allows to share resources between threads. When the first instance of the program is launched it creates a new mutex. When a second instance is launched it checks the existence of the mutex. If it exists then it exits. When the first instance quits it releases the existing mutex.

Comments and Discussions

The link to pinvoke site is wrongly written as www.pinvoke.com.I tried the site but just to find free icon downloads. A little googling gives me result www.pinvoke.net[^] for all the possible interop functions listing.