以下範例修改自mdsn Sample
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NotificationIconSample" Height="300" Width="300" Loaded="Window_Loaded"
>
{
using System;
using System.Windows;
using System.Windows.Forms; // NotifyIcon control
using System.Drawing; // Icon
public partial class MainWindow : Window
{
NotifyIcon notifyIcon;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Configure and show a notification icon in the system tray
this.notifyIcon = new NotifyIcon();
this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!";
this.notifyIcon.Text = "Hello, NotifyIcon!";
this.notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico");
this.notifyIcon.ShowBalloonTip(1000);
//TODO:初始化 notifyIcon菜單
System.Windows.Forms.ContextMenu notifyIconMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem notifyIconMenuItem = new System.Windows.Forms.MenuItem();
notifyIconMenuItem.Index = 0;
notifyIconMenuItem.Text = "結束(E&xit)";
notifyIconMenuItem.Click += new EventHandler(notifyIconMenuItem_Click);
notifyIconMenu.MenuItems.Add(notifyIconMenuItem);
this.notifyIcon.ContextMenu = notifyIconMenu;
this.notifyIcon.Visible = true;
}
void notifyIconMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}