简体   繁体   中英

C# Minimize to system tray on close

Hi In my c# application I am trying to minimize application to systems tray, when the form is closed. Here is the code I have tried.

   public void MinimizeToTray()
    {
        try
        {
            notifyIcon1.BalloonTipTitle = "Sample text";
            notifyIcon1.BalloonTipText = "Form is minimized";

            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                notifyIcon1.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

and I am calling the method to form closing event. But the problem is its not minimizing to tray. Its just closing the form.

e.Cancel = true; code will be always cancelling the event even if you shut the computer down, but here is a code that helps you:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (e.CloseReason == CloseReason.UserClosing)
     {
          myNotifyIcon.Visible = true;
          this.Hide();
          e.Cancel = true;
     }
 }

It will allow closing the form programmaticaly.

Write a event in Form Closing event.

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
        e.Cancel = true;                         
        Hide();
 }

And write using Custom menu strip for notification icon for to show.

   namespace MinimizeTrayNotification
     {
      public partial class Form1 : Form
       {
    public Form1()
    {
        InitializeComponent();
    }

    private void MinimzedTray()
    {
        notifyIcon1.Visible = true;
        notifyIcon1.Icon = SystemIcons.Application;

        notifyIcon1.BalloonTipText = "Minimized";
        notifyIcon1.BalloonTipTitle = "Your Application is Running in BackGround";
        notifyIcon1.ShowBalloonTip(500);

    }

    private void MaxmizedFromTray()
    {
        notifyIcon1.Visible = true;
        notifyIcon1.BalloonTipText = "Maximized";
        notifyIcon1.BalloonTipTitle = "Application is Running in Foreground";
        notifyIcon1.ShowBalloonTip(500);


    }



    private void Form1_Resize(object sender, EventArgs e)
    {
        if(FormWindowState.Minimized==this.WindowState)
        {
        MinimzedTray();
        }
      else  if (FormWindowState.Normal == this.WindowState)
        {

            MaxmizedFromTray();
        }
        }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.WindowState = FormWindowState.Normal;
        Form1 frm = new Form1();
        frm.Show();
        MaxmizedFromTray();


    }

    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
    {
        this.WindowState = FormWindowState.Normal;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Hide();

        }


    }

    private void notifyIcon1_Click(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Normal;
        notifyIcon1.BalloonTipText = "Normal";
        notifyIcon1.ShowBalloonTip(500);
    }
 }

}

You should cancel the FormClosing event and then call your MinimizeToTray() function.

This is done through the Cancel property of the FormClosingEventArgs .

Also, consider using a bool somewhere to allow closing the Form in some conditions, such as if you're using a File > Exit menu or something:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(!allowClosing)
    {
        e.Cancel = true;
        MinimizeToTray();
    }
}

To minimize when closing set WindowState to Minimized

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
    e.Cancel = true;
    WindowState = FormWindowState.Minimized;
}

You need to use the FormClosing-Event.

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
    e.Cancel = true;
    MinimizeToTray();
}

You can handle FormClosing Event such as micsoft Form Closing Event as Following example of C#

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Determine if text has changed in the textbox by comparing to original text.
        if (textBox1.Text != strMyOriginalText)
        {
            // Display a MsgBox asking the user to save changes or abort.
            if (MessageBox.Show("Do you want to save changes to your text?", "My Application",
                MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // Cancel the Closing event from closing the form.
                e.Cancel = true;
                // Call method to save file...
            }
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM