2012-10-31 16:46:51
分类: 嵌入式
与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();
- }