NotifyIcon 类
指定可在通知区域创建图标的组件。 此类不能被继承。
private NotifyIcon _notifyIcon = null;
private void Init()
{
_notifyIcon = new NotifyIcon();
_notifyIcon.BalloonTipText = "Hello,模拟键盘";
_notifyIcon.Text = "模拟键盘";
//_notifyIcon.Icon = new System.Drawing.Icon(@"Appicon.ico");//程序图标
_notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);//当前程序图标
_notifyIcon.Visible = true;
//打开菜单项
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
open.Click += new EventHandler(Show);
//隐藏菜单项
System.Windows.Forms.MenuItem hide = new System.Windows.Forms.MenuItem("Hide");
hide.Click += new EventHandler(Hide);
//退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
exit.Click += new EventHandler(Close);
//关联托盘控件
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open,hide, exit };
_notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
//双击图标
_notifyIcon.MouseDoubleClick += OnMouseDoubleClickHandler;
Hide(null,null);
}
private void OnMouseDoubleClickHandler(object sender, EventArgs e)
{
this.Show(null,null);
}
private void Show(object sender, EventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
this.ShowInTaskbar = true;
this.Activate();
}
private void Hide(object sender,EventArgs e)
{
this.Visibility = System.Windows.Visibility.Hidden;
}
private void Close(object sender, EventArgs e)
{
System.Windows.Application.Current.Shutdown();
}