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.

Running a process only once

We all  know that usually the starting point for every WinForms application is the Main() method. Here’s a little trick to ensure that an application of ours runs only once,  making an active effort to stop any other instance that could be called if there’s another one already running. Here’s how:

/// <summary>
///Application’s entry point
/// </summary>
[STAThread]
static void Main() 
{
if(Process.GetProcessesByName(
   Process.GetCurrentProcess().ProcessName).GetUpperBound(
0)==0)
   {
      Application.Run(
new frmMain());
   }
}

Colorized by: CarlosAg.CodeColorizer

 

The key of all this is the Process class. It belongs to the Systems.Diagnostics namespace, and one of its capabilities is obtaining the name of the current process (via Process.GetCurrentProcess().ProcessName, which returns an string), and another one is obtaining all running process with a given name (Process.GetProcessesByName, which returns an array of Process objects).  The if() in that line searches for an already running process with the same name as the one we’re trying to start in the list of all currently running processes and if it’s not found, which is determined by comparing the length of the array containing the found processes with 0, the application starts.

A New Hope

Mon 23 January, 2006

Well, after a while thinking about it, I’ve left Blogger. For the (main) Spanish version of this blog I’ve hired a domain of my own (Picacódigos) and some hosting. There I use a .NET blogging engine, the excellent open-source dasBlog, but for this I’ve chosen WordPress as the engine and the excellent Irish guys at Blogsome for the free hosting.

The idea is making Codecruncher a much more technically focused blog than in previous incarnations, although that doesn’t mean I won’t write about any other things. I hope with these new tools to be able to write longer articles, for once.

That is, at least, the idea.