与C++相比c#提供的NotifyIcon控件实现托盘图标容易了许多。
托盘程序的制作:
1.拖拽NotifyIcon控件到窗体上,并设置NotifyIcon的Icon(很重要!否则运行后看不到效果)
2.控制窗体最小化到托盘。
- private void MinToIcon()
- {
- this.Hide();
- this.ShowInTaskbar = false;
- this.notifyIconCtrl.Visible = true;
- }
- private void ShowDlg()
- {
- this.Show();
- this.ShowInTaskbar = true;
- this.notifyIconCtrl.Visible = false;
- }
3.创建菜单项:"显示主窗体","退出" ,将菜单挂到NotifyIcon上。
- private MenuItem showDlgMenu = new MenuItem("显示窗体");
- private MenuItem exitMenu = new MenuItem("退出");
- this.showDlgMenu.Click += new EventHandler(showDlgMenu_Click);
- this.exitMenu.Click += new EventHandler(exitMenu_Click);
- this.notifyIconCtrl.ContextMenu = new System.Windows.Forms.ContextMenu();
- this.notifyIconCtrl.ContextMenu.MenuItems.Add(this.showDlgMenu);
- this.notifyIconCtrl.ContextMenu.MenuItems.Add(this.exitMenu);
- private void btnMinToIcon_Click(object sender, EventArgs e)
- {
- MinToIcon();
- }
- void showDlgMenu_Click(object sender, EventArgs e)
- {
- ShowDlg();
- }
- void exitMenu_Click(object sender, EventArgs e)
- {
- this.Dispose(true);
- Application.ExitThread();
- }
阅读(4088) | 评论(0) | 转发(0) |