Jump to content
  • Advertisement
Sign in to follow this  
TheTroll

[.net] Taskbar and hiding forms

This topic is 3549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is not an question post but an informational post. This is using C# and .Net 2.0 or later. We have an application that we want to start in the taskbar without showing a form first. The form must be running because it is the core of our application. So first we add our NotifyIcon control to our form, name it notifiyIcon. Next we add the following statements to the form constructor.
     this.Opacity = 0;
     this.Shown += new EventHandler(Settings_Shown);


this.Opacity = 0; makes the form transparent so we don't get a "flash" when the application starts. this.Shown gives use a hook into the OnShown event which fires only once when the form is shown for the first time (the form is being shown even though they can't see it). Next we need to prevent the form from being shut down by the X control in the top right or by double clicking the top left.
protected override void OnClosing(CancelEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }


Now make sure you give them a menu item or button that calls Application.Exit() so they can actually shut the application down because you will not be able to close this form any other way. So now we add our Shown method
void Settings_Shown(object sender, EventArgs e)
{
   this.Close();
   this.Opacity = 1;
}


That is about it. The advantages of this approach are the the form never flickers, it can not be accessed via the Alt-Tab and will not show up in the menu bar. Hope it helps someone out. If you have any questions, feel free to ask. theTroll

Share this post


Link to post
Share on other sites
Advertisement
One for minor thing. When the application exits you may end up with the notifiyIcon still in the system tray. To make sure that doesn't happen. Add the following.


private void ExitApplication()
{
notifyIcon.Visible = false;
Application.Exit();
}



Hope that helps.
theTroll

Share this post


Link to post
Share on other sites
Thanks for the info TheTroll, I have a similar application that has a notifyIcon and you can start my app by running it on startup so it needed to minimize to the tray as an icon without showing the form. I had the same problem with the form showing for a brief moment.

My issue with using opacity like you have is that I believe it's something not supported in older OS's like 98.

I have a solution that is similar but should work in any case.

When my program is launched before I run Application.Run(Form) I set the window state to FormWindowState.Minimized;

using (Global.MainForm = new frmMain())
{
Global.MainForm.WindowState = FormWindowState.Minimized;

Application.Run(Global.MainForm);
}

private void MainForm_Shown(object sender, EventArgs e)
{
this.Hide();
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.SetTopLevel(true);
this.BringToFront();
this.Focus();
this.Activate();
this.WindowState = FormWindowState.Normal;
}

private void MainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.Hide();
}


Like you I use the Form_Shown() event but inside that I simply call this.Hide(); to stop it from showing. Then in notifyIcon1_MouseDoubleClick() I simply show the form again using this.Show() and set the window state to FormWindowState.Normal.

It should work the same without needing to set the opacity of the window.

Share this post


Link to post
Share on other sites
One more thing worth mentioning is that I needed my application to prompt to quit but only if the user attempted to quit the program. If the computer was shutdown I couldn't have the prompt showing. So here is how I dealt with that scenario.

public enum CloseMethodType
{
Unknown,
XButton
}

public CloseMethodType CloseType = CloseMethodType.Unknown;

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_CLOSE = 0xF060;

protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
case WM_SYSCOMMAND:
if (msg.WParam.ToInt32() == SC_CLOSE)
CloseType = CloseMethodType.XButton;
break;
}

base.WndProc(ref msg);
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (CloseType == CloseMethodType.XButton)
{
if (MessageBox.Show("Really Quit?", "Quitting", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
CloseType = CloseMethodType.Unknown;
return;
}
}
}

Share this post


Link to post
Share on other sites
The conventional way to implement this behaviour is to add the NotifyIcon to an Component class and use this Component class as the startup object for the application. (Instantiate and dispose it within the static Main method.) Then you can simply show and hide the Form normally and don't have to use Opacity as a nasty hack.

Also, I should note that:

notifyIcon.Visible = false;

shouldn't really be necessary to hide the icon. Insure that your form is disposing correctly. (It will not be if you exit the application by stopping the debugger, for example.) If the problem persists, try calling:

notifyIcon.Dispose()

upon shutdown, though from my experience this hasn't been necessary either.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!