close


image

資源檔案加入一張圖片

image

請先下載Icon: http://findicons.com/icon/583421/forms

image

程式碼:

   1: public partial class Form1 : Form
   2: {
   3:     NotifyIcon notifyIcon;
   4:  
   5:     private void Initial()
   6:     {
   7:         notifyIcon = new NotifyIcon();
   8:         notifyIcon.BalloonTipText = "Program is Running...";               //設定通知欄提示的文字
   9:         notifyIcon.Text = "NotifyIcon Sample";                             //設定通知欄在滑鼠移至Icon上的要顯示的文字
  10:         notifyIcon.Icon = (System.Drawing.Icon)Properties.Resources.forms; //決定一個Logo
  11:         notifyIcon.Click += (sender, e) =>                                 //設定按下Icon的事件
  12:         {
  13:             notifyIcon.Visible = false;
  14:             this.ShowInTaskbar = true;
  15:             this.Show();
  16:         };
  17:  
  18:         //設定右鍵選單
  19:         ContextMenu contextMenu = new ContextMenu();
  20:         MenuItem menuItem = new MenuItem();
  21:         menuItem.Checked = true;
  22:         menuItem.Index = 1;
  23:         menuItem.Text = "顯示";
  24:         menuItem.Click += (sender, e) =>                            //設定按下事件
  25:         {
  26:             MessageBox.Show("開始...");
  27:         };
  28:         contextMenu.MenuItems.Add(menuItem);
  29:         notifyIcon.ContextMenu = contextMenu;
  30:     }
  31:     public Form1()
  32:     {
  33:         InitializeComponent();
  34:         Initial();
  35:     }
  36:  
  37:     private void button1_Click(object sender, EventArgs e)
  38:     {
  39:         this.ShowInTaskbar = false;
  40:         this.Hide();
  41:         notifyIcon.Visible = true;
  42:         notifyIcon.ShowBalloonTip(1, "測試", "測試內容", ToolTipIcon.Info);
  43:     }

44: }

執行畫面:

image

自訂選單:

image

參考資料: http://www.dotblogs.com.tw/holiestar/archive/2014/01/08/139305.aspx

全站熱搜
創作者介紹
創作者 me1237guy 的頭像
me1237guy

天天向上

me1237guy 發表在 痞客邦 留言(0) 人氣()