实现托盘(任务栏图标与托盘图标互斥),并且在点击任务栏图标时实现的最小化与点击最小化按钮分离。

具体如下:

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#:实现托盘(任务栏图标与托盘图标互斥)的更多相关文章

  1. Windows 托盘区域显示图标

    NOTIFYICONDATA structure 这个结构体包含了向通知区域(底部任务栏右下角区域,下面都称为托盘)显示的信息.需要使用函数Shell_NotifyIcon. 结构体成员 typede ...

  2. WIN7 清除任务栏图标缓存

    如果任务栏上锁定程序如果换了位置,如:剪切走了.图标会变成白色图标. 解决方法: rem 关闭Windows外壳程序explorer taskkill /f /im explorer.exe rem ...

  3. Delphi产生任务栏图标【TNotifyIconData】

    一.新建一个应用程序:File->New Applicaton 在Interface部分要放在Uses Message之后,定义一个消息常量:const WM_NID=WM_USER+1000; ...

  4. Delphi产生任务栏图标【TNotifyIconData】(转载)

    一.新建一个应用程序:File->New Applicaton 在Interface部分要放在Uses Message之后,定义一个消息常量:const WM_NID=WM_USER+1000; ...

  5. C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)

    本文参考C++改写 https://blog.csdn.net/dpsying/article/details/20139651  (该文章的坐标理解的有误解,会导致功能无效) SendMessage ...

  6. Win7任务栏图标大小调整为等宽

    打开注册表,找到HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics,新建DWORD,输入MinWidth,大图标设为56,小图标设定为36: 参 ...

  7. Win10如何隐藏Windows Defender任务栏图标

    导读 Windows 10 至发布以来就内置集成了 Windows Defender 安全防护应用,但有许多用户平常压根儿就没注意到它的存在.微软为了使安全防护功能更加明显,Windows 10 周年 ...

  8. C# 动态绘制任务栏图标的实现

    通常我们在做一个应用时会遇到这样的需求:将收到的消息条数显示到任务栏,比如如下的效果 怎么实现呢? 答案是采用WindowsAPICodePack实现,具体参见:Windows 7 任务栏开发 之 覆 ...

  9. c#无标题窗体点击任务栏图标正常最小化或还原

    FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还原的. protecte ...

随机推荐

  1. 单源最短路径——Dijkstra算法学习

    每次都以为自己理解了Dijkstra这个算法,但是过没多久又忘记了,这应该是第4.5次重温这个算法了. 这次是看的胡鹏的<地理信息系统>,看完之后突然意识到用数学公式表示算法流程是如此的好 ...

  2. ON_NOTIFY_REFLECT : Message Reflection for Windows Controls

    转自: https://msdn.microsoft.com/en-us/library/eeah46xd.aspx TN062: Message Reflection for Windows Con ...

  3. 重写NSLog,Debug模式下打印日志和当前行数

    在pch文件中加入以下命令,NSLog在真机测试中就不会打印了 //重写NSLog,Debug模式下打印日志和当前行数 #if DEBUG #define NSLog(FORMAT, ...) fpr ...

  4. Linux启动时卡住

    该系统本是oracle rac的测试环境,在删除oracle软件后重启时系统卡住(没有按照oracle官方要求删除oracle软件).如下图: 处理过程: 1.使用单用户模式登陆 先在GRUB启动菜单 ...

  5. Silverlight TabItem选中,未选中样式设置

    <Application.Resources> <LinearGradientBrush x:Key="TabItemBackground" EndPoint=& ...

  6. Jquery 知识表

  7. 实验十四_访问CMOS RAM

    编程:以"年/月/日 时:分:秒"的格式,显示当前的日期,时间. 注意:CMOS RAM中存储着系统的配置信息,除了保存时间信息的单元外,不要向其他的单元写入内容,否则将引起一些系 ...

  8. Leetcode: Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...

  9. 最近兰州的js风格写个插件和一个template engine

    /* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...

  10. 认识javascript

    javascript小知识 www.phonegap.com(跨平台开发框架) Cocos2d-Html5(WebGL渲染  javascript语言) creatjs.com(融合了flash动画的 ...