【问题标题】:C# Minimize to system tray on closeC# 关闭时最小化到系统托盘
【发布时间】:2012-11-17 11:37:40
【问题描述】:

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

    }

我正在调用该方法来形成关闭事件。但问题是它没有最小化到托盘。它只是关闭表格。

【问题讨论】:

  • 何时关闭或最小化表单?
  • 您是否也在取消/停止关闭事件?如果应用程序在执行后仍然关闭,则此代码将无济于事。
  • @HonzaBrestan 试图在我单击关闭按钮时最小化它的系统托盘。
  • 能否也提供关闭事件代码?

标签: c# system-tray


【解决方案1】:

e.Cancel = true; 代码将始终取消事件,即使您关闭计算机也是如此,但这里有一个代码可以帮助您:

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

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

【讨论】:

  • 这个解决方案要好得多,因为 arun kumar non ascii 的解决方案是一个永无止境的故事:D
  • 我的经验是,如果你不设置notifyIcon的图标图像,那么它不会显示在windows 8.1的系统托盘中。
  • 好答案。之后以编程方式关闭表单需要 Application.Exit() 而不是 this.Close()。
【解决方案2】:

在表单关闭事件中编写一个事件。

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

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

【讨论】:

  • 需要在构造函数中添加一行:this.FormClosing += this.Form1_FormClosing;
【解决方案3】:
   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);
    }
 }

}

【讨论】:

  • 我确信代码会起作用,但如果对正在发生的事情发表一两条评论会更有帮助!
  • 对于 notifyIcon1_MouseDoubleClick,您应该使用 this.Show() 而不是创建一个新的表单实例。您可能希望表单控件保持其值。
【解决方案4】:

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

这是通过FormClosingEventArgsCancel 属性完成的。

另外,考虑在某处使用bool允许在某些情况下关闭Form,例如如果您使用File > Exit 菜单或其他东西:

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

【讨论】:

  • 您可以使用 FormClosingEventArgs 选择何时关闭,而不是使用 allowClosing: if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; }
【解决方案5】:

关闭时最小化设置 WindowStateMinimized

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

【讨论】:

    【解决方案6】:

    您需要使用 FormClosing-Event。

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

    【讨论】:

      【解决方案7】:

      您可以将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...
                  }
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        • 2011-11-29
        相关资源
        最近更新 更多