On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

Tuesday, May 29, 2012

In this post I want to show, how to bring an application to the front using C#. The presented method should work, no matter if the program currently has the focus or is minimized, granting the application the input focus and set it as the top most window on the screen.
By default in C# forms have the property TopMost and the function BringToFront(), but these just move the form forward in relation to other forms of the application. Therefore, we have to dig a bit deeper and to use the Win32 API.

For above needs we need the function SetForegroundWindow(), which expects the handle of the target window. It grants the application the input focus, if the application is minimized though, it is now shown. The reason is, that minimized applications do not have a window which could be shown.

To create this window we use the function ShowWindowAsync(). It expects 2 arguments, the first one is again the window handle, the second one an integer value determining how the window is shown. For an overview over all possibilities I want to refer to the MSDN, I only use SW_RESTORE, which shows the window in its original size.

We now simply add a request asking whether the window is minimized, which is possible via the function IsIconic().

The following program uses the previously described functions to bring the program after elapse of the timer event to the front:

Monday, May 28, 2012

This topic is about creating global hotkeys for a C# application. Once registered, the hotkeys are active system wide - meaining: Also when another program is active at the moment, the pressing of the hotkey is still made known to our application. This way, for example a running inactive application can be called to the front.
We define a hotkey with the API function RegisterHotKey(). This expects 4 parameters:

The handle of the window, which is supposed to get the message.

The ID of the hotkey, important when multiple hotkeys are registered per application. If NULL is handed over, the hotkey is just registered as the only hotkey for the application.

The control keys, which have to be pressed for activating the hotkey (e.g. Alt, Ctrl ...).

The key, which has to be pressed for activating the hotkey (e.g. "X").

Analogously to that there exists the function UnregisterHotKey(), which expects the window handle and ID of the hotkey, with which the function can be deleted (for example at the termination of the program).
When the hotkey is pressed, the application gets the message WM_HOTKEY. To receive this, the function WndProc() has to be overridden.
The following sample code creates 2 hotkeys (Shift + Ctrl + X and Shift + Ctrl + Y), I think the method should become clear this way: