一个notifyIcon和contextMenu关联
目的是实现最小化到系统托盘,然后左键单击托盘图标还原窗口,右键单击弹出contextMenu
但是在点击contextMenu上面菜单项的同时(没有先后)也会触发notifyIcon的click事件(窗口还原)
请问怎么解决这个问题?谢谢
代码如下:
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true ,Application.ProductName, out createdNew);
if(createdNew)
{
Application.Run(new Form1());
m.ReleaseMutex();
}
}
//处理一个log文件
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//文件读写
}
catch(Exception e1)
{
MessageBox.Show(e1.Message,"Log");
}
} //contextMenu的“退出”菜单项
private void menuItem1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
//contextMenu的“处理log”菜单项
private void menuItem4_Click(object sender, System.EventArgs e)
{
button1_Click(this.button1, null);
}
private void Form1_SizeChanged(object sender, System.EventArgs e)
{
if (this.WindowState==FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
}
//单击notifyIcon
private void notifyIcon1_Click(object sender, System.EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
//contextMenu的“还原”菜单项
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
目的是实现最小化到系统托盘,然后左键单击托盘图标还原窗口,右键单击弹出contextMenu
但是在点击contextMenu上面菜单项的同时(没有先后)也会触发notifyIcon的click事件(窗口还原)
请问怎么解决这个问题?谢谢
代码如下:
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true ,Application.ProductName, out createdNew);
if(createdNew)
{
Application.Run(new Form1());
m.ReleaseMutex();
}
}
//处理一个log文件
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//文件读写
}
catch(Exception e1)
{
MessageBox.Show(e1.Message,"Log");
}
} //contextMenu的“退出”菜单项
private void menuItem1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
//contextMenu的“处理log”菜单项
private void menuItem4_Click(object sender, System.EventArgs e)
{
button1_Click(this.button1, null);
}
private void Form1_SizeChanged(object sender, System.EventArgs e)
{
if (this.WindowState==FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
}
//单击notifyIcon
private void notifyIcon1_Click(object sender, System.EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
//contextMenu的“还原”菜单项
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
解决方案 »
- 我有两个窗体A和B,我想把B窗体的combox的值赋值给A窗体中的combox,要怎么实现
- 请问哪位大侠知道ZedGraph这个开源图表组件,用键盘上下键控制显示的图片缩小和放大呢?
- vs2003或者2005里每种模板的区别是什么,怎样应用
- 如何用程序对文件夹进行压缩与解压缩
- 如何固定DataGrid每一列的宽度
- $$$$$$$$$$$$高分求 工资管理系统!!!!!!!!
- IIS配置word导入的工程出错
- 为什么我的C#的WEB项目打不开,新手上路
- 我要学c#.net,该从那开始??难吗??谢谢指教啊~~~~~
- 如何根据button事件引起paint事件
- C#的应用程序,如何实现框架与引用JS文件和样式文件!
- C#调用非托管C++的DLL时出现的问题
vs2003
下面是窗体事件绑定代码:
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "MainForm";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "处理log";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "-";
//
// menuItem1
//
this.menuItem1.Index = 3;
this.menuItem1.Text = "Exit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "ForLog";
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
单步调试一下,确实有这个问题,不过要处理也不麻烦,代码如下:private bool bMouseLeftPressed = false;// Add notify-icon mouse-down event
private void ntfDemo_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMouseLeftPressed = ( e.Button == MouseButtons.Left );
}// In notify-icon click event
if( bMouseLeftPressed )
{
// Restore window here
}