一个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;
}

解决方案 »

  1.   

    没发现类似的问题,你用的什么环境,vs2003 or vs2005?
      

  2.   

    谢谢您的回复~
    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);
      

  3.   

    呵呵……
    单步调试一下,确实有这个问题,不过要处理也不麻烦,代码如下: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
    }