设置窗体属性showinTaskbar=false
WindowState ws;
WindowState wsl;
NotifyIcon notifyIcon;
#region Constructions
public MainWindow()
{
InitializeComponent();//显示托盘。
icon();
//保证窗体显示在上方。
wsl = WindowState;
}
#endregion
private
void icon()
{
this.notifyIcon
= new NotifyIcon();
this.notifyIcon.BalloonTipText
= "Hello, 文件监视器";
//设置程序启动时显示的文本
this.notifyIcon.Text
= "文件监视器";//最小化到托盘时,鼠标点击时显示的文本
this.notifyIcon.Icon
= new System.Drawing.Icon("Downloads.ico");//程序图标
this.notifyIcon.Visible
= true;
notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
this.notifyIcon.ShowBalloonTip(1000);
}
private
void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
this.Show();
WindowState = wsl;
}
private
void Window_StateChanged(object sender, EventArgs e)
{
ws = WindowState;
if (ws
== WindowState.Minimized)
{
this.Hide();
}
}
可以给notifyIcon添加右键菜单:
主窗体中拖入一个ContextMenu控件contextMenu1,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中contextMenu1作为上下文菜单。
(可以在子菜单中添加行为)