3

I want to run my program with the icon showing in the system tray but without the mainform showing from the start.

Edit:

  lMainForm := new MainForm; 
  lMainForm.ShowInTaskbar := true;
  Application.Run(lMainForm);

Didn't work. As soon as Application.Run is executed, mainform is displayed along with the icon in the system tray.

ThN
  • 3,235
  • 3
  • 57
  • 115
  • By 'system tray', do you mean the notification area? You need to create a NotificationIcon item for that (IIRC the name), associate it with the "hidden" form, and give it a context menu/click handler to allow you to interact with the program. – Andrew Barber Jan 06 '12 at 21:53

3 Answers3

3

You do it by overriding the SetVisibleCore() method. Like this:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
        }
        base.SetVisibleCore(value);
    }

Beware that the Load event won't fire. Be sure to move any code you have there to either the constructor (preferred) or this override.

This code suppresses the window only once. You can call Show() or setting Visible = true later to make the window show up as normal. You'd typically do so in the Click event handler of a context menu item for the NotifyIcon.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

The problem you have at the moment is that you are calling the Application.Run overload that takes the main form as a parameter. This will show the main form which you do not want.

Instead you should call one of the other Application.Run overloads.

For example you can call the no parameter overload of Application.Run. Make sure that you have created and installed your notify icon before you do so. And also create, but do not show your main form.

When you are ready to show your main form, in response to an action on the notify icon, call lMainForm.Show. You will also wish to arrange that clicking the close button on the form merely hides the form rather than closing it. I'm assuming that you want your main form instance to persist hidden in the background.

So the top-level of your program would look like this:

//create and show the notify icon here
lMainForm := new MainForm; 
lMainForm.ShowInTaskbar := true;
lMainForm.Visible := false;//I believe this is the default in any case
Application.Run;

You'll need to add an item to the notify icon menu that closes the application. Implement this with:

Application.Exit;

If you need more fine grained control over the application lifetime then you may be better off using the Application.Run overload that receives an ApplicationContext.

Since I don't have Prism at hand I have checked this out with C#/WinForms and I hope it transfers alright to Prism!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Have you tried:

lMainForm.WindowState := System.Windows.Forms.FormWindowState.Minimized;

or

lMainForm.Hide(); // call on application start
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • Minimising is no good. You need to set Visible to False to get it out of the taskbar. – David Heffernan Jan 06 '12 at 21:49
  • @ David, I set the Visible to false and the mainform is still displayed or shown. I don't want the mainform in the taskbar or displayed but its icon in the system tray, which is working. BTW, your avatar is very funny lol. – ThN Jan 06 '12 at 21:54