首先要先準備一張icon格式的圖片,就是要當作右下角顯示的圖。
然後把圖放在跟執行檔同一個目錄下,也可以在專案中另外建一個Image資料夾來放,只是路徑要改一下而已。
一開始要先宣告Notifyicon來用
- System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
在這裡碰到一個問題是找不到Forms可以用,所以要在方案總管中的參考加入參考System.Windows.Forms
接著只要在MainWindow()裡面加入這兩行,把Icon圖片抓進來,並且把顯示開關打開就可以了
- nIcon.Icon = new Icon("NotifyIcon.ico");
- nIcon.Visible = true;
如果圖片跟執行檔放在同一個目錄的話就不用打路徑,但如果圖片是放在專案另外一個的資料夾,就把路徑改一下就好(假如圖檔是放在上兩層之後的Image/Icon/NotifyIcon.ico)
- nIcon.Icon = new Icon("../../Image/Icon/NotifyIcon.ico");
這時候如果專案沒有加入參考System.Windows.Drawing也記得要加一下
程式執行之後就會看到右下角的圖示了!
然後除了顯示icon之外,如果想要去點他來隱藏或是關掉程式也可以。
在MainWindow()加入Click事件
- nIcon.DoubleClick += new System.EventHandler(NotifyIcon1_Click);
然後就可以在NotifyIcon1_Click()函式裡面寫點擊後要做的事情,例如可以點右下角圖示來關閉程式。
- private void NotifyIcon1_Click(object sender, System.EventArgs e)
- {
- this.Close();
- }
有時候程式視窗被隱藏起來,找不到地方可以關掉,就可以用這個小撇步來關。
另外如果想要對圖示按右鍵跑出選單的功能也可以做
先宣告ContextMenu(選單)跟MenuItem(選單選項)
- System.Windows.Forms.ContextMenu nIconMenu = new System.Windows.Forms.ContextMenu();
- System.Windows.Forms.MenuItem nIconMenuItem = new System.Windows.Forms.MenuItem();
在MainWindow()裡面加入選項(option 1),按下選項要做的事情,然後加入選單上
- nIconMenuItem.Index = 0;
- nIconMenuItem.Text = "option 1";
- nIconMenuItem.Click += new System.EventHandler(nIconMenuItem1_Click);
- nIconMenu.MenuItems.Add(nIconMenuItem);
最後把Menu加到Icon上面
- nIcon.ContextMenu = nIconMenu;
這樣在右下角Icon上面按右鍵就會出現選單跟選項了
在選項中有加入nIconMenuItem1_Click監聽按下去要做的事情,所以在nIconMenuItem1_Click()中就可以寫按下選單中的該選項要做的事情
- private void nIconMenuItem1_Click(object sender, System.EventArgs e)
- {
- // Item1 click
- }
如果要再多加選項(option 2)的話,就是在宣告一個MenuItem(選單選項)來用
最後附上完整程式碼:
MainWindow.xaml.cs
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Drawing;
- using System.IO;
- namespace Application
- {
- public partial class MainWindow : Window
- {
- System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
- System.Windows.Forms.ContextMenu nIconMenu = new System.Windows.Forms.ContextMenu();
- System.Windows.Forms.MenuItem nIconMenuItem1 = new System.Windows.Forms.MenuItem();
- System.Windows.Forms.MenuItem nIconMenuItem2 = new System.Windows.Forms.MenuItem();
- public MainWindow()
- {
- InitializeComponent();
- nIcon.Icon = new Icon("../../Image/Icon/NotifyIcon.ico");
- nIcon.Visible = true;
- nIcon.DoubleClick += new System.EventHandler(NotifyIcon1_Click);
- nIconMenuItem1.Index = 0;
- nIconMenuItem1.Text = "option 1";
- nIconMenuItem1.Click += new System.EventHandler(nIconMenuItem1_Click);
- nIconMenu.MenuItems.Add(nIconMenuItem1);
- nIconMenuItem2.Index = 1;
- nIconMenuItem2.Text = "option 2";
- nIconMenuItem2.Click += new System.EventHandler(nIconMenuItem2_Click);
- nIconMenu.MenuItems.Add(nIconMenuItem2);
- // put the menu on icon
- nIcon.ContextMenu = nIconMenu;
- }
- private void NotifyIcon1_Click(object sender, System.EventArgs e)
- {
- this.Close();
- }
- private void nIconMenuItem1_Click(object sender, System.EventArgs e)
- {
- MessageBox.Show("Option 1 pressed!");
- }
- private void nIconMenuItem2_Click(object sender, System.EventArgs e)
- {
- MessageBox.Show("Option 2 pressed!");
- }
- }
- }
(END)
沒有留言:
張貼留言