簡體   English   中英

C#最小化到關閉時的系統托盤

[英]C# Minimize to system tray on close

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

    }

我正在調用該方法來形成關閉事件。 但是問題是它沒有最小化托盤。 它只是關閉窗體。

e.Cancel = true; 即使您關閉計算機電源,代碼也始終會取消該事件,但是以下代碼可以幫助您:

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

它將允許以編程方式關閉表單。

在窗體關閉事件中編寫一個事件。

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

並使用“自定義”菜單條編寫要顯示的通知圖標。

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

}

您應該取消FormClosing事件,然后調用MinimizeToTray()函數。

這是通過FormClosingEventArgsCancel屬性完成的。

另外,考慮在某些情況下使用bool 允許在某些情況下關閉Form ,例如,如果您使用的是File > Exit菜單或其他方法:

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

關閉時最小化WindowState設置為Minimized

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

您需要使用FormClosing-Event。

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

您可以按照以下C#示例處理FormClosing事件,例如micsoft表單關閉事件

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM