2017/02/07

[C#] WPF Notify Icon

開發Windows的WPF程式有個方法可以讓程式也加入右下角的工具列圖示中,就是用Notifyicon來做。

首先要先準備一張icon格式的圖片,就是要當作右下角顯示的圖。

然後把圖放在跟執行檔同一個目錄下,也可以在專案中另外建一個Image資料夾來放,只是路徑要改一下而已。
一開始要先宣告Notifyicon來用
  1. System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();

在這裡碰到一個問題是找不到Forms可以用,所以要在方案總管中的參考加入參考System.Windows.Forms

接著只要在MainWindow()裡面加入這兩行,把Icon圖片抓進來,並且把顯示開關打開就可以了
  1. nIcon.Icon = new Icon("NotifyIcon.ico");
  2. nIcon.Visible = true;

如果圖片跟執行檔放在同一個目錄的話就不用打路徑,但如果圖片是放在專案另外一個的資料夾,就把路徑改一下就好(假如圖檔是放在上兩層之後的Image/Icon/NotifyIcon.ico)
  1. nIcon.Icon = new Icon("../../Image/Icon/NotifyIcon.ico");

這時候如果專案沒有加入參考System.Windows.Drawing也記得要加一下

程式執行之後就會看到右下角的圖示了!

然後除了顯示icon之外,如果想要去點他來隱藏或是關掉程式也可以。
在MainWindow()加入Click事件
  1. nIcon.DoubleClick += new System.EventHandler(NotifyIcon1_Click);

然後就可以在NotifyIcon1_Click()函式裡面寫點擊後要做的事情,例如可以點右下角圖示來關閉程式。
  1. private void NotifyIcon1_Click(object sender, System.EventArgs e)
  2. {
  3. this.Close();
  4. }

有時候程式視窗被隱藏起來,找不到地方可以關掉,就可以用這個小撇步來關。
另外如果想要對圖示按右鍵跑出選單的功能也可以做
先宣告ContextMenu(選單)跟MenuItem(選單選項)
  1. System.Windows.Forms.ContextMenu nIconMenu = new System.Windows.Forms.ContextMenu();
  2. System.Windows.Forms.MenuItem nIconMenuItem = new System.Windows.Forms.MenuItem();

在MainWindow()裡面加入選項(option 1),按下選項要做的事情,然後加入選單上
  1. nIconMenuItem.Index = 0;
  2. nIconMenuItem.Text = "option 1";
  3. nIconMenuItem.Click += new System.EventHandler(nIconMenuItem1_Click);
  4. nIconMenu.MenuItems.Add(nIconMenuItem);

最後把Menu加到Icon上面
  1. nIcon.ContextMenu = nIconMenu;

這樣在右下角Icon上面按右鍵就會出現選單跟選項了

在選項中有加入nIconMenuItem1_Click監聽按下去要做的事情,所以在nIconMenuItem1_Click()中就可以寫按下選單中的該選項要做的事情
  1. private void nIconMenuItem1_Click(object sender, System.EventArgs e)
  2. {
  3. // Item1 click
  4. }

如果要再多加選項(option 2)的話,就是在宣告一個MenuItem(選單選項)來用
最後附上完整程式碼:
MainWindow.xaml.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Drawing;
  18. using System.IO;
  19.  
  20. namespace Application
  21. {
  22. public partial class MainWindow : Window
  23. {
  24. System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
  25. System.Windows.Forms.ContextMenu nIconMenu = new System.Windows.Forms.ContextMenu();
  26. System.Windows.Forms.MenuItem nIconMenuItem1 = new System.Windows.Forms.MenuItem();
  27. System.Windows.Forms.MenuItem nIconMenuItem2 = new System.Windows.Forms.MenuItem();
  28.  
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. nIcon.Icon = new Icon("../../Image/Icon/NotifyIcon.ico");
  33. nIcon.Visible = true;
  34. nIcon.DoubleClick += new System.EventHandler(NotifyIcon1_Click);
  35.  
  36. nIconMenuItem1.Index = 0;
  37. nIconMenuItem1.Text = "option 1";
  38. nIconMenuItem1.Click += new System.EventHandler(nIconMenuItem1_Click);
  39. nIconMenu.MenuItems.Add(nIconMenuItem1);
  40.  
  41. nIconMenuItem2.Index = 1;
  42. nIconMenuItem2.Text = "option 2";
  43. nIconMenuItem2.Click += new System.EventHandler(nIconMenuItem2_Click);
  44. nIconMenu.MenuItems.Add(nIconMenuItem2);
  45.  
  46. // put the menu on icon
  47. nIcon.ContextMenu = nIconMenu;
  48. }
  49.  
  50. private void NotifyIcon1_Click(object sender, System.EventArgs e)
  51. {
  52. this.Close();
  53. }
  54.  
  55. private void nIconMenuItem1_Click(object sender, System.EventArgs e)
  56. {
  57. MessageBox.Show("Option 1 pressed!");
  58. }
  59.  
  60. private void nIconMenuItem2_Click(object sender, System.EventArgs e)
  61. {
  62. MessageBox.Show("Option 2 pressed!");
  63. }
  64.  
  65. }
  66. }


(END)

沒有留言:

張貼留言