簡易的常駐程式及選單範例

摘要:簡易的常駐程式及選單範例

因為這方面的教學已經很多,此篇只是做個記錄,讓將來有需要用到的時後不需要再到處google

開發讓程式一執行就只出現在 右下角的通知區域(Windows Tray) 的步驟:

  1. 建立 Windows Form 應用程式
  2. 刪除 Form1.cs (因不需要視窗介面,所以,將其刪除)
  3. 加入 WindowsTray.cs
  4. 於 WindowsTray.cs 中加入底下程式:
    
    using System;
    using System.Windows.Forms;
    using System.ComponentModel;
    
    namespace WindowForm
    {
        class WindowsTray
        {
            //NotifyIcon 要放在一個 Container 物件內
            private Container container1;
            //通知區的Icon物件
            private NotifyIcon notifyIcon1;
    
            //通知區的選單
            private ContextMenuStrip contextMenuStrip1;
            //選單一:離開
            private ToolStripMenuItem miExit;
            //選單二:呈現提示
            private ToolStripMenuItem miShowMsg;
    
            #region Windows Form 設計工具產生的程式碼
            private void InitializeComponent()
            {
                this.container1 = new Container();
                this.notifyIcon1 = new NotifyIcon(this.container1);
                this.contextMenuStrip1 = new ContextMenuStrip(this.container1);
                this.miExit = new ToolStripMenuItem();
                this.miShowMsg = new ToolStripMenuItem();
                // 
                // notifyIcon1
                // 
                this.notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                this.notifyIcon1.BalloonTipText = "通知訊息";
                this.notifyIcon1.BalloonTipTitle = "自訂標題";
                this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
                this.notifyIcon1.Icon = new System.Drawing.Icon("Twitter.ico");
                this.notifyIcon1.Text = "常駐程式";
                this.notifyIcon1.Visible = true;
                // 
                // contextMenuStrip1
                // 
                this.contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
                this.miShowMsg,
                this.miExit});
                this.contextMenuStrip1.Name = "contextMenuStrip1";
                // 
                // miExit
                // 
                this.miExit.Text = "離開";
                this.miExit.Click += new System.EventHandler(this.miExit_Click);
                // 
                // miShowMsg
                // 
                this.miShowMsg.Text = "顯示通知";
                this.miShowMsg.Click += new System.EventHandler(this.miShowMsg_Click);
            }
    
            #endregion
    
            public NotifyIconUI()
            {
                InitializeComponent();
            }
            
            private void miExit_Click(object sender, EventArgs e)
            {
                MessageBox.Show("即將關閉程式");
                this.notifyIcon1.Visible = false;
                Application.Exit();
            }
    
            private void miShowMsg_Click(object sender, EventArgs e)
            {
                this.notifyIcon1.ShowBalloonTip(3000);
            }
        }
    }
    
  5. 於 Program.cs 修改程式碼如下:
    
    //Application.Run(new Form1()); → 刪除即可
    NotifyIconUI ni = new NotifyIconUI();
    Application.Run();

程式執行畫面如下:

XP - Windows 7