主要功能:
C#让窗体最小化至任务栏,同时在系统托盘区的图标点击左键能显示窗体,并使窗体闪烁。
首先:
创建窗体应用程序,并添加控件NotifyIcon及ContextMenuStrip控件
NotifyIcon:点击notifyIcon1属性,为控件属性Icon添加图标
contextMenuStrip1属性,进入Items添加或右键"编辑项".添加4个toolStripMenuItem,设置其Text为"显示窗体"、"隐藏窗体"、"开始闪烁"、"退出"
代码:
点击窗体"关闭"按钮时,取消关闭操作且窗体隐藏,任务栏图标仍然显示:
//窗体关闭前发生事件 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; //取消关闭操作 表现为不关闭窗体 this.Hide(); //隐藏窗体 } }
1.左键点击图标时,显示窗体.
2.当鼠标右键点击图标时,显示"显示窗体""隐藏窗体""闪烁""退出"菜单栏,并有相对应的功能
//"显示窗体"单击事件 private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); //窗体显示 this.WindowState = FormWindowState.Normal; //窗体状态默认大小 this.Activate(); } //"隐藏窗体"单击事件 private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide(); //隐藏窗体 } //"退出"单击事件 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { //点击"是(YES)"退出程序 if (MessageBox.Show("确定要退出程序?", "安全提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes) { notifyIcon1.Visible = false; //设置图标不可见 this.Close(); //关闭窗体 this.Dispose(); //释放资源 Application.Exit(); //关闭应用程序窗体 } }
鼠标左键图标显示窗体功能
//鼠标左键图标事件 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { //点击鼠标"左键"发生 if (e.Button == MouseButtons.Left) { this.Visible = true; //窗体可见 this.WindowState = FormWindowState.Normal; //窗体默认大小 this.notifyIcon1.Visible = true; //设置图标可见 } }
图标闪烁
闪烁的效果是加上一个空白的图标,正常图标与空白图标的切换进而实现闪烁的效果。
property是vs的一个资源管理功能,可以存储系统图标及一些常量
private Icon timg = Properties.Resources.timg; private Icon blank = Properties.Resources.blank;//透明的图标 private bool _status = true; private bool _isBlink = false;
右键菜单控制图标是不是显示
private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (_isBlink == false) { _isBlink = true; timer1.Enabled = true; timer1.Start(); } else { _isBlink = false; timer1.Stop(); //气泡提示 notifyIcon1.ShowBalloonTip(5000, "提示", "关闭闪烁效果!", ToolTipIcon.Info); } }
定时器中修改图标的状态,定时反转图标
private void timer1_Tick(object sender, EventArgs e) { if (_status) notifyIcon1.Icon = timg; else notifyIcon1.Icon = blank; _status = !_status; }
完整代码:
public partial class Form1 : Form { private Icon timg = Properties.Resources.timg; private Icon blank = Properties.Resources.blank;//透明的图标 private bool _status = true; private bool _isBlink = false; public Form1() { InitializeComponent(); } //窗体关闭前发生事件 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; //取消关闭操作 表现为不关闭窗体 this.Hide(); //隐藏窗体 } } //"显示窗体"单击事件 private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); //窗体显示 this.WindowState = FormWindowState.Normal; //窗体状态默认大小 this.Activate(); } //"隐藏窗体"单击事件 private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide(); //隐藏窗体 } //"退出"单击事件 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { //点击"是(YES)"退出程序 if (MessageBox.Show("确定要退出程序?", "安全提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes) { notifyIcon1.Visible = false; //设置图标不可见 this.Close(); //关闭窗体 this.Dispose(); //释放资源 Application.Exit(); //关闭应用程序窗体 } } //鼠标左键图标事件 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { //点击鼠标"左键"发生 if (e.Button == MouseButtons.Left) { this.Visible = true; //窗体可见 this.WindowState = FormWindowState.Normal; //窗体默认大小 this.notifyIcon1.Visible = true; //设置图标可见 } } private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (_isBlink == false) { _isBlink = true; timer1.Enabled = true; timer1.Start(); } else { _isBlink = false; timer1.Stop(); //气泡提示 notifyIcon1.ShowBalloonTip(5000, "提示", "关闭闪烁效果!", ToolTipIcon.Info); } } private void timer1_Tick(object sender, EventArgs e) { if (_status) notifyIcon1.Icon = timg; else notifyIcon1.Icon = blank; _status = !_status; } }