C#:实现托盘(任务栏图标与托盘图标互斥)
实现托盘(任务栏图标与托盘图标互斥),并且在点击任务栏图标时实现的最小化与点击最小化按钮分离。
具体如下:
1、向窗体上添加如下控件:MenuStrip menuStrip1, NotifyIcon ni_frmMain,Timer timer1, ContentMenuStrip cms_notify。其中notify中包含显示、退出等。
2、实现的代码:
//字段:
//当前子Form
private CurrentForm childForm = null; //上次窗体的状态
private FormWindowState lastFormState; //是否点击的是最小化按钮
private bool isMinBoxHited; //构造函数内
this.ni_frmMain.Visible = false; #region 托盘相关代码 #region 私有方法 处理窗体的 显示 隐藏 关闭(退出) /// <summary>
/// 显示
/// </summary>
private void ShowMainForm()
{
this.Show();
this.WindowState = this.lastFormState;
this.Activate(); this.ShowInTaskbar = true; //任务栏中显示窗体图标
this.ni_frmMain.Visible = false;//图盘中隐藏图标
} /// <summary>
/// 隐藏
/// </summary>
private void HideMainForm()
{
this.Hide(); this.ShowInTaskbar = false; //任务栏中显示窗体图标
this.ni_frmMain.Visible = true;//图盘中隐藏图标
} /// <summary>
/// 关闭(退出)
/// </summary>
private void ExitMainForm()
{
if (MessageBox.Show("您确定要退出主程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
this.ni_frmMain.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
} #endregion #region 右键菜单处理,显示 隐藏 退出 /// <summary>
/// 显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmi_notifyShow_Click(object sender, EventArgs e)
{
ShowMainForm();
} /// <summary>
/// 隐藏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmi_notifyHide_Click(object sender, EventArgs e)
{
HideMainForm();
} /// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmi_notifyExit_Click(object sender, EventArgs e)
{
ExitMainForm();
} #endregion #region 私有方法 双击托盘上图标时,显示窗体 private void ni_frmMain_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
ShowMainForm();
}
} #endregion #region 点最小化按钮时,最小化到托盘 private void frmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
this.ShowInTaskbar = true; //任务栏中窗体图标显示
this.ni_frmMain.Visible = false;
this.lastFormState = this.WindowState;
}
} #endregion #region 窗体关闭时最小化到托盘
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
//e.Cancel = true;
//this.ShowInTaskbar = false; //任务栏中窗体图标消失
//this.ni_frmMain.Visible = true;
//HideTipForm();
} #endregion #region 图标闪烁 开启 关闭 /// <summary>
/// 开启
/// </summary>
private void StartFlicker()
{
this.timer1.Enabled = true;
this.timer1.Start();
} /// <summary>
/// 关闭
/// </summary>
private void CloseFlicker()
{
this.timer1.Stop();
this.timer1.Enabled = false;
this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
} private int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (i < 1)
{
this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
i++;
return;
}
else
{
this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore1;
i = 0;
}
}
#endregion #region 区别 任务栏中点击窗体图标(最小化|恢复)与点击最小化按钮 private int WM_SYSCOMMAND = 0x112;
private long SC_MINIMIZE = 0xF020;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt64() == SC_MINIMIZE && m.LParam.ToString() != "0") //m.LParam.ToString() != "0" 表示任务栏中点击窗体图标(最小化|恢复)
{
HideMainForm(); //这里直接将窗体图盘化//this.isMinBoxHited = true; //点击的是最小化按钮 通过中间变量不可行
return;
}
}
base.WndProc(ref m);
} #endregion #endregion
C#:实现托盘(任务栏图标与托盘图标互斥)的更多相关文章
- Windows 托盘区域显示图标
NOTIFYICONDATA structure 这个结构体包含了向通知区域(底部任务栏右下角区域,下面都称为托盘)显示的信息.需要使用函数Shell_NotifyIcon. 结构体成员 typede ...
- WIN7 清除任务栏图标缓存
如果任务栏上锁定程序如果换了位置,如:剪切走了.图标会变成白色图标. 解决方法: rem 关闭Windows外壳程序explorer taskkill /f /im explorer.exe rem ...
- Delphi产生任务栏图标【TNotifyIconData】
一.新建一个应用程序:File->New Applicaton 在Interface部分要放在Uses Message之后,定义一个消息常量:const WM_NID=WM_USER+1000; ...
- Delphi产生任务栏图标【TNotifyIconData】(转载)
一.新建一个应用程序:File->New Applicaton 在Interface部分要放在Uses Message之后,定义一个消息常量:const WM_NID=WM_USER+1000; ...
- C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
本文参考C++改写 https://blog.csdn.net/dpsying/article/details/20139651 (该文章的坐标理解的有误解,会导致功能无效) SendMessage ...
- Win7任务栏图标大小调整为等宽
打开注册表,找到HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics,新建DWORD,输入MinWidth,大图标设为56,小图标设定为36: 参 ...
- Win10如何隐藏Windows Defender任务栏图标
导读 Windows 10 至发布以来就内置集成了 Windows Defender 安全防护应用,但有许多用户平常压根儿就没注意到它的存在.微软为了使安全防护功能更加明显,Windows 10 周年 ...
- C# 动态绘制任务栏图标的实现
通常我们在做一个应用时会遇到这样的需求:将收到的消息条数显示到任务栏,比如如下的效果 怎么实现呢? 答案是采用WindowsAPICodePack实现,具体参见:Windows 7 任务栏开发 之 覆 ...
- c#无标题窗体点击任务栏图标正常最小化或还原
FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还原的. protecte ...
随机推荐
- Vue.2.0.5-Class 与 Style 绑定
Class 与 Style 绑定 数据绑定一个常见需求是操作元素的 class 列表和它的内联样式.因为它们都是属性 ,我们可以用v-bind 处理它们:只需要计算出表达式最终的字符串.不过,字符串拼 ...
- 使用duplicate target database ... from active database复制数据库
使用duplicate target database ... from active database复制数据库 source db:ora11auxiliary db:dupdb 1.修改监听文件 ...
- 如何用Java解析CSV文件
首先看一下csv文件的规则: csv(Comma Separate Values)文件即逗号分隔符文件,它是一种文本文件,可以直接以文本打开,以逗号分隔.windows默认用excel打开.它的格式包 ...
- G面经prepare: Android Phone Unlock Pattern
1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...
- [原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- SQL中索引的原理
(一)深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引 ...
- 建立一个Hello World级别的Spring项目
package com.sevenhu.domain; import org.springframework.context.ApplicationContext; import org.spring ...
- 【皇甫】☀PPT里的小玩意
第三次写博客了,感觉写的蛮有趣的,在写的同时,回顾了知识点,又上手操作了一遍,印象更加深刻了,尽管今天写的和那些像JAVA啦,HTML啦,C#啦,没多大关系(个人理解),但确实我们经常能用到的.比如说 ...
- ThinkPHP 中实现 Rewrite 模式
ThinkPHP中默认的URL地址是形如这样的:http://localhost/Myapp/index.php/Index/index/ Myapp是我的项目文件名,默认的访问地址是上面这样的.为了 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...