Invisible forms
Another interesting trick, which bothered me until I found about it, is the next one. Let’s say we’re developing an application which is going to live in Windows’ notification area. There are a lot of tutorials about this on the Internet, so I won’t go into that. But there’s a little detail which bugged me a lot: whenever I ALT+TABbed, on the list of running applications I saw mine, even though it was invisible. And I only wanted my app to be visible to ALT+TAB when,… well, when it would be really visible.
After quite some Googling, I found something like this:
private const int GWL_EXSTYLE = (-20);
private const int WS_EX_TOOLWINDOW = 0x80;
private const int WS_EX_APPWINDOW = 0x40000;
[DllImport("user32", CharSet=CharSet.Auto)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32", CharSet=CharSet.Auto)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
private void
frmMain_Activated(object sender, EventArgs e){
SetWindowLong(this.Handle, GWL_EXSTYLE, (GetWindowLong(this.Handle,
GWL_EXSTYLE) | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW);
}
Colorized by: CarlosAg.CodeColorizer
As you can see, this Win32 API code does only one thing: to apply a given style to the current window. Calling the API at the form’s Activated event ensures that the application will be invisible for its very beginning. By the way, I’ve tested this code with an application that has two forms, and the starting one only contains the contextual menu and the notification area icon, which is why it should never be visible.
The application itself is (yet another) small utility to periodically change your desktop image, and it’s living on all my machines on an unrefined form for quite some time. It’s almost done, at least 1.0, but I really need some free time to make her look good and take her for a public spin.




