0

I am trying to minimize my winapp to system tray. I have downloaded a sample project from codeproject. But it goes to systary on Form.Resize event. Code -

    private void Form_Resize(object sender, EventArgs e)
    {
        notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
        notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

        if (FormWindowState.Minimized == this.WindowState)
        {
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(500);
            this.Hide();    
        }
        else if (FormWindowState.Normal == this.WindowState)
        {
            notifyIcon1.Visible = false;
        }
    }

Is it necessary to handle it on resize event? Can i do it on button click event?

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • 1
    Windows form does not allow any event to get notification of windows status changed (minimized, maximized or restored). If you would like to hide your window on minimized and that time you want to show notify icon. then there is only simple method to get notification is your window minimized or not. But, if you want to show notify icon even window is not minimized then simply you can place these code into form load. –  Mar 12 '14 at 06:09

1 Answers1

1

You can do this in your button. For obvious reasons you cannot rely on the WindowState in your button, because it can only be clicked when the window is no minimized to tray anyway.

private void button1_Click(object sender, EventArgs e)
{
    notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
    notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(500);
    this.Hide();
} 

This should work to "minimize" to tray. Although it should really be called hide-on-button-click-to-tray.

nvoigt
  • 75,013
  • 26
  • 93
  • 142