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 ...
随机推荐
- Linux 基本收集
ifconfig eth0 192.168.1.223 切换到root账号开始是$符号输入su输入root密码转换成# 就变成了root账号 dr 查看盘符ls /etc/ 查看etc文件夹下面的文件 ...
- WHERE谓词对索引使用的影响
本篇博文只测试WHERE谓词对multi-column index使用的影响,主要篇幅是SQL代码+截图.详细内容请参考<Inside the SQL Server Query Optimize ...
- Excel将秒转换成标准的时间格式HH:MM:SS
Excel将秒转换成标准的时间格式HH:MM:SS 比如120秒,转换成00:02:00 Excel公式为: =TEXT(A1/86400,"[hh]:mm:ss") A1为秒数据 ...
- Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)
控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...
- Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- __int64和long long输入输出
__int64 num; scanf("%I64d", &num); printf("%I64d\n", num); long long num; sc ...
- 转:python webdriver API 之鼠标事件
前面例子中我们已经学习到可以用 click()来模拟鼠标的单击操作,而我们在实际的 web 产品测试中 发现,有关鼠标的操作,不单单只有单击,有时候还要和到右击,双击,拖动等操作,这些操作包含在Act ...
- php js表单登陆验证
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 封装pdo单例模式类
<?php /** * MyPDO * @author Jason.Wei <jasonwei06@hotmail.com> * @license http://www.sunblo ...
- 1029 C语言文法
program -> external_declaration | program external_declaration <程序> -> <外部声明> ...