实现托盘(任务栏图标与托盘图标互斥),并且在点击任务栏图标时实现的最小化与点击最小化按钮分离。
具体如下:
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