C# 程序启动最小化至任务栏及闪烁
2017-09-11 16:59
246 查看
主要功能:
C#让窗体最小化至任务栏,同时在系统托盘区的图标点击左键能显示窗体,并使窗体闪烁。
首先:
创建窗体应用程序,并添加控件NotifyIcon及ContextMenuStrip控件
NotifyIcon:点击notifyIcon1属性,为控件属性Icon添加图标
contextMenuStrip1属性,进入Items添加或右键"编辑项".添加4个toolStripMenuItem,设置其Text为"显示窗体"、"隐藏窗体"、"开始闪烁"、"退出"
代码:
点击窗体"关闭"按钮时,取消关闭操作且窗体隐藏,任务栏图标仍然显示:
1.左键点击图标时,显示窗体.
2.当鼠标右键点击图标时,显示"显示窗体"\"隐藏窗体"\"闪烁"\"退出"菜单栏,并有相对应的功能
鼠标左键图标显示窗体功能
图标闪烁
闪烁的效果是加上一个空白的图标,正常图标与空白图标的切换进而实现闪烁的效果。
property是vs的一个资源管理功能,可以存储系统图标及一些常量
右键菜单控制图标是不是显示
定时器中修改图标的状态,定时反转图标
完整代码:
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; }
}
相关文章推荐
- VC++ 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)收藏
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- VC++ 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- VC++ 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- MFC窗口程序启动时最小化到托盘、不在任务栏中显示、显示时在右小角
- C# winForm 开机自动启动 并启动后最小化到任务栏 右侧通知栏并交互操作
- 如何使对话框程序启动以及主窗口最小化时不在任务栏上显示
- C#控制台程序启动后最小化或者隐藏小黑板
- C# winForm启动最小化到任务栏右侧通知栏并交互操作
- C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示 很多程序是只需要后台运行的,甚至不需要自己的应用界面。NotifyIcon提供了程序在任务栏的显示功能 程序下载链接如下
- 如何使对话框程序启动以及主窗口最小化时不在任务栏上显示
- C# WinForm实现任务栏程序图标闪烁
- Windows XP中当最小化程序时,无法在任务栏显示图标的问题
- 用C#给程序加启动画面并只允许一个应用程序实例运行