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 ...
随机推荐
- Jboss消息 异常
org.jboss.mq.SpyJMSException: Could not update message: 1 msg=13 hard STORED PERSISTENT queue=QUEUE. ...
- 【关于HBITMAP, DC, MEM DC, Clipboard】将HBITMAP拷贝到Clipboard(Windows Clipboard & OLE Clipboard)
参考: Programming Windows with MFC, 2nd. Chapter 18, 19. 建议把这两章学习完(至少到OLE drag-and-drop之前要学习完)再来尝试OLE ...
- NSAttributedString
1. 使用这个类,必须先导入CoreText框架. 2. 给UILabel设置attributedText了会导致给UILabel中text,font,textColor,shadowCo ...
- iOS UICollectionView之二(垂直滚动)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- [chrome插件] 利滚利计算器 银行存款 基金理财 余额宝收益计算
利滚利计算在数学上是否有一个简单的公式,我不知道,但作为程序员,这个算法实现起来就是小菜一碟.作为插件发布,是因为程序确实简单,也方便Chome浏览器使用者安装.如果你也想使用一下Chrome插件,参 ...
- Java基础之一组有用的类——为标记定义自己的模式(ScanString)
控制台程序. Scanner类提供了一种方式,用来指定如何识别标记.这需要使用next()方法的两个重载版本.其中的一个版本接受Pattern类型的参数.另一个版本接受String类型的参数,用来指定 ...
- Java基础之一组有用的类——使用公历日历(TryCalendar)
控制台程序. 公历是西方使用的日历,用GregorianCalendar类的对象来表示.GregorianCalendar对象封装了时区信息.日期和时间数据.GregorianCalendar对象有7 ...
- transient关键字的含义
transient java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当 ...
- PostgreSQL Replication之第十一章 使用Skytools(2)
11.2 剖析 skytools Skytools 不只是一个单一的脚本,而是一个提供各种不同服务的工具的集合.一旦我们安装了Skytools,更详细地查一下这些组件的细节是有意义的: • londi ...
- acm算法模板(4)
杂乱小模板 状态压缩dp小技巧 x&-x是取x的最后一个1的位置. x-=x&-x是去掉x的最后一个1. 读入外挂 int nxt_int(){// neg or pos cha ...