C#关闭时最小化到系统托盘

时间:2012-11-29 11:44:37

标签: c# system-tray

您好,在我的c#应用程序中,当表单关闭时,我正在尝试最小化应用程序到系统托盘。这是我试过的代码。

   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);
        }

    }

我正在调用形成结束事件的方法。但问题是它不能最小化托盘。它只是关闭表格。

7 个答案:

答案 0 :(得分:33)

即使关闭计算机,

e.Cancel = true;代码也会一直取消该事件,但这里有一段可以帮助您的代码:

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

它将允许以programmaticaly形式关闭表单。

答案 1 :(得分:15)

在表单结束事件中写一个事件。

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

使用自定义菜单条写入要显示的通知图标。

答案 2 :(得分:2)

   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);
    }
 }

}

答案 3 :(得分:1)

您应取消FormClosing事件,然后调用MinimizeToTray()功能。

这是通过Cancel的{​​{1}}属性完成的。

另外,在某些情况下,考虑使用某个FormClosingEventArgs允许关闭bool,例如,如果您使用Form菜单或其他内容:

File > Exit

答案 4 :(得分:1)

将关闭 WindowState 设置为最小化时最小化

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

答案 5 :(得分:0)

您需要使用FormClosing-Event。

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

答案 6 :(得分:0)

您可以处理FormClosing事件,例如micsoft Form Closing Event,如下面的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...
            }
        }
}