[C#, .NET] Run a program in tray (minimized window and no-shown in taskbar) with right clicked pop-up menu

程式執行後以最小化的方式執行,即不會出現在工作列上,而會出現在tray裡頭。而在 tray icon 上可以點滑鼠右鍵來彈出功能表單(menu),進行各功能選擇(如結束等)。

In Form1.Designer.cs

namespace SampleProgram
{
  partial class SampleClass
  {
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SampleClass));
      this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
      this.notifyiconMnu = new System.Windows.Forms.ContextMenu();
      this.menuItem1 = new System.Windows.Forms.MenuItem();
      this.SuspendLayout();
      //
      // notifyIcon1
      //
      this.notifyIcon1.ContextMenu = this.notifyiconMnu;
      this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
      this.notifyIcon1.Text = "SBPNotify";
      this.notifyIcon1.Visible = true;
      this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
      //
      // notifyiconMnu
      //
      this.notifyiconMnu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem1});
      //
      // menuItem1
      //
      this.menuItem1.Index = 0;
      this.menuItem1.Text = "EXIT";
      this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
      //
      // SBP
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(592, 373);
      this.ControlBox = false;
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.Name = "SBP";
      this.ShowInTaskbar = false;
      this.Text = "ServerBatcherProcess";
      this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
      this.ResumeLayout(false);

    }

    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.ContextMenu notifyiconMnu;
    private System.Windows.Forms.MenuItem menuItem1;
  }
}

In Form1.cs

namespace SampleProgram
{
  public partial class SampleClass : Form
  {
    public SampleClass()
    {
      InitializeComponent();
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
      if (this.WindowState == FormWindowState.Minimized)
      {
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;
      }
      else if (this.WindowState == FormWindowState.Normal)
      {
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
      }

      // Activate the form.
      this.Activate();
    }

    private void menuItem1_Click(object sender, EventArgs e)
    {
      //隱藏tray icon  
      notifyIcon1.Visible = false;
      //關閉系統  
      this.Close();
    }
  }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料