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

22

嗨,在我的 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);
        }

    }

我正在调用方法来形成关闭事件。但问题是它没有最小化到托盘,而只是关闭了窗体。


当窗体关闭或最小化时? - Danpe
你也取消/停止关闭事件了吗?如果应用程序在执行此操作后仍然关闭,那么这段代码将不会有太大帮助。 - Honza Brestan
@HonzaBrestan 当我点击关闭按钮时,尝试将其最小化到系统托盘。 - Rakesh
可能是重复问题:https://dev59.com/Z0_Ta4cB1Zd3GeqPAGAx - Jester
1
你能提供结束事件的代码吗? - Honza Brestan
7个回答

53

e.Cancel = true; 这段代码会一直取消事件,即使你关机了,但是这里有一个帮助你的代码:

e.Cancel = false;

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

这将允许以编程方式关闭表单。


1
这个解决方案更好,因为Arun Kumar Non ASCII的解决方案是一个永无止境的故事:D - Ismoh
我发现如果你不设置notifyIcon的图标图像,那么它在Windows 8.1中不会显示在系统托盘中。 - Fer
一个很好的跟进:https://dev59.com/c3I-5IYBdhLWcg3wNla1#3233612 - user736893
很好的答案。在程序中自动关闭表单需要使用Application.Exit()而不是this.Close()。 - kuklei

22
在“Form Closing”事件中编写事件。
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
        e.Cancel = true;                         
        Hide();
 }

使用自定义菜单栏编写通知图标以进行显示。


7
需要在构造函数中添加以下行: this.FormClosing += this.Form1_FormClosing; - temple

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

}


2
我相信这段代码会运行,但如果加上一两个注释来解释正在发生的事情,将会更有帮助! - leo
对于 notifyIcon1_MouseDoubleClick 事件,你应该使用 this.Show() 而不是创建一个新的窗体实例。你可能希望你的窗体控件保持它们的值。 - prospector

2

您需要取消 FormClosing 事件,然后调用您的 MinimizeToTray() 函数。

这可以通过 FormClosingEventArgsCancel 属性来实现。

另外,考虑在某些情况下使用一个 bool 来允许关闭 Form,例如如果您正在使用 File > Exit 菜单或其他操作:

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

不必使用allowClosing,您可以选择在FormClosingEventArgs中关闭的时间:if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; } - Trent Stewart

1

在关闭时最小化,将WindowState设置为Minimized

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

0

您可以像以下C#示例一样处理FormClosing事件,例如Microsoft Form Closing Event

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...
            }
        }
}

0
你需要使用 FormClosing 事件。
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
    e.Cancel = true;
    MinimizeToTray();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接