實現步驟
- 創建 NotifyIcon 控件并設置屬性;
- 編寫 NotifyIcon 響應控制事件;
- 在主窗體的Load事件中將 NotifyIcon 添加到系統托盤;
- 程序退出時,移除系統托盤的 NotifyIcon;
NotifyIcon 控件,通常用于在系統托盤中顯示圖標,通過使用它就可以我們想要的效果。
屬性 | 描述 |
---|
Icon | 在系統托盤中顯示的圖標 |
Text | 鼠標懸停在圖標時顯示的文本 |
Visible | 指定是否可見 |
常用方法
方法 | 描述 |
---|
ShowContextMenu | 在系統托盤上下文菜單中顯示指定的菜單 |
添加控件(拖拽方式)
將NotifyIcon和一個ContextMenuStrip控件。拖到主窗體中可以修改控件名稱
- NotifyIcon 托盤圖標
- ContextMenuStrip 托盤圖標右擊彈出的菜單
設置控件
點擊 ContextMenuStrip 右上方的三角圖標 -> 編輯項,彈出項信合編輯器
添加右健菜單信息
添加主窗體事件
在最小化或關閉主窗體時,顯示在任務欄托盤區域,實現了單擊關閉時,不真正關閉程序,而是將主界面隱藏HIDE掉,同時開始顯示托盤菜單。
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.mainNotifyIcon.Visible = true;
this.Hide();
return;
}
}
實現雙擊托盤打開主程序
private void mainNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.Visible)
{
this.WindowState = FormWindowState.Minimized;
this.mainNotifyIcon.Visible = true;
this.Hide();
}
else
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
private async void toolStripMenuItemQuit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你確定要退出?", "系統提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
this.mainNotifyIcon.Visible = false;
this.Close();
this.Dispose();
System.Environment.Exit(System.Environment.ExitCode);
}
}
代碼方式添加
using System;
using System.Windows.Forms;
namespace Fountain.WinForm.NotifyDemo
{
public partial class FormMain : Form
{
private NotifyIcon notifyIcon = new NotifyIcon();
private ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
this.InitializeNotifyMenu();
this.notifyIcon.Text = this.Text;
this.notifyIcon.Visible = true;
this.notifyIcon.Icon = this.Icon;
this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;
this.notifyIcon.DoubleClick += notifyIcon_DoubleClick;
}
private void InitializeNotifyMenu()
{
try
{
contextMenuStrip.Items.Clear();
ToolStripMenuItem showMenuItem = new ToolStripMenuItem("顯示界面");
showMenuItem.Tag = "顯示";
showMenuItem.Click += new EventHandler(ShowMenuItem_Click);
contextMenuStrip.Items.Add(showMenuItem);
ToolStripMenuItem sboutMenuItem = new ToolStripMenuItem("關于");
sboutMenuItem.Tag = "關于";
sboutMenuItem.Click += new EventHandler(AboutMenuItem_Click);
contextMenuStrip.Items.Add(sboutMenuItem);
ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("退出");
exitMenuItem.Tag = "退出";
exitMenuItem.Click += new EventHandler(ExistMenuItem_Click);
contextMenuStrip.Items.Add(exitMenuItem);
}
catch(Exception exception)
{
throw new Exception(exception.Message);
}
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
try
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
catch (Exception objException)
{
throw new Exception(objException.Message);
}
}
private void ShowMenuItem_Click(object sender, EventArgs e)
{
try
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
catch (Exception objException)
{
throw new Exception(objException.Message);
}
}
private void AboutMenuItem_Click(object sender, EventArgs e)
{
try
{
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
private void ExistMenuItem_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("你確定要退出程序嗎?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
this.notifyIcon.Visible = false;
this.notifyIcon.Dispose();
this.Dispose();
Application.Exit();
}
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
e.Cancel = true;
this.Hide();
this.notifyIcon.Dispose();
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
private void FormMain_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
this.notifyIcon.Visible = true;
}
}
}
}
系統開機自啟動應用程序
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace Fountain.WinForm.NotifyDemo
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
string applictionName = Process.GetCurrentProcess().MainModule.ModuleName;
string applictionPath = Process.GetCurrentProcess().MainModule.FileName;
#region 當前登陸用戶的注冊表啟動項
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
registryKey.SetValue(applictionName, applictionPath);
#endregion
#region 所有用戶的注冊表啟動項
#endregion
}
}
}