Input, control and automation

Tuesday, 22 December 2015

C#: Making a Windows Forms application that minimizes to the notification area

02:38 Posted by Roxton , No comments
Below is a quick guide to making a C# Windows Forms application minimize to the notification area instead of the taskbar. This is something I find I have to do from time to time, mostly for little utilities that I want to run in the background without taking up prime screen space.

The first step is to add a NotifyIcon to the project. If you're using Visual Studio you can just drop one in from the toolbox; if you're not I'm assuming you know what you're doing. Once you've added the NotifyIcon (let's call it myIcon) you need to set some of its properties. The most important of these is its Icon property - if you don't give the app an image to display then it won't be shown when minimized to the notification area and will be completely hidden from the user. It's surprisingly hard to make .ico files - hardly any of the usual image-fiddling programs support them out of the box - but for now I'm going to assume that you can pinch one from another application or use one of the many online conversion tools.

The two other NotifyIcon properties you'll want to consider at the moment are Text and Visible. The former is the text that will be displayed when the user hovers over the icon in the notification area; the latter is whether or not the icon should be visible. For now, set Text to something like "Click to restore application". Set Visible to true or false depending on whether you want the icon to appear in the notification area when the application isn't minimized.

Now we can change the form's behaviour. The resize event is triggered whenever the form is minimized or maximized. To add a method for this event, you can either go to the properties tab of the main form, click the events button and then double-click the resize field, or add the following line of code to your Form.Designer.cs file:
this.Resize += new System.EventHandler(this.MyForm_Resize);
If you opted for the Visual Studio clickery you'll be automatically taken to your Form.cs file, where a MyForm_Resize method will have been created for you. If you didn't, you can create the method there yourself. In either case, this is what you want it to look like:
private void MyForm_Resize(object sender, EventArgs 
{   //Hide this in the system tray instead of in the taskbar
     if (WindowState==FormWindowState.Minimized)
     {
          myIcon.Visible = true;
          Hide();     //Hide the application from the taskbar
     }
     else
     {
          myIcon.Visible = false;
     }
}
This code gets called whenever the form is resized, minimized, restored or maximized. It checks the state of the window to see whether or not it has been minimized: if it has, it shows the icon in the notification area and hides it from the taskbar; if it's not minimized, then it hides the icon from the notification area.

If you want to go ahead and run that, you'll see that minimizing the application does indeed remove it from the taskbar and displays the icon in the notification area. However, there's currently no way of restoring the application to its original state - if you click the icon in the notification area nothing happens. For that, we need to add one last method - this time to the click event of myIcon. To do this, you can either add a line to Form.Designer.cs as we did before, or select myIcon in the designer, go to its properties tab and click events.

If you do this, you'll see that there are a number of choices: Click, DoubleClick, MouseClick, MouseDoubleClick, and so on. The difference between Click and MouseClick is not something we need to get into at the moment; for our purposes they are interchangeable. Let's create a method for the Click event:
private void myIcon_Click(object sender, EventArgs e)
{
     Show();     //Restore the app to the taskbar
     WindowState = FormWindowState.Normal;    //Un-minimize the app window
}
This method will be called when the user clicks the icon in the notification area. Show() makes the application appear in the taskbar again, and the next line un-minimizes the form (or restores it, or normalizes it, or whatever term we want to use). Note that we don't need to fiddle about with hiding the notification icon here, because the method we wrote before (MyForm_Resize) will get called as soon as we change the WindowState.

 And that's it, really. Very straightforward, very simple. Go forth and make apps minimize to the notification area. If you want to see an example of this in action, you can have a look at my Mouse Mover utility over on Github.

0 comments:

Post a Comment