Invisible forms

Wed 25 January, 2006

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:

//Makes the application invisible to ALT+TAB
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.

Comments »

The URI to TrackBack this entry is: http://codecruncher.blogsome.com/2006/01/25/5/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>