C#切换上的NotifyIcon点击表格的知名度和隐藏在其他地方点击 [英] c# toggle form visibility on notifyIcon click and hide it on click elsewhere

查看:178
本文介绍了C#切换上的NotifyIcon点击表格的知名度和隐藏在其他地方点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它是在系统托盘中。我想,当用户点击NotifyIcon的,如果它是不可见已经使其可见。如果已经可见它应该是隐藏的。此外,当用户点击其他地方除了形式应隐藏(如果它是可见)的形式。



我的代码如下所示:

 保护覆盖无效OnDeactivate( EventArgs五)
{
showForm(假);
}
公共无效showForm(布尔显示){
如果(显示){
展();
激活();
的WindowState = FormWindowState.Normal;
}其他{
隐藏();
的WindowState = FormWindowState.Minimized;
}
}
私人无效notifyIcon1_MouseClicked(对象发件人,MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
$ { b $ b如果(的WindowState = FormWindowState.Normal!)
{
showForm(真);
}
}
}



与代码的问题是这onDeactivated被点击呼叫,隐藏的形式和的onclick不仅仅是重新显示它之前被调用。如果我能如果焦点丢失由于点击检测到的NotifyIcon或其他地方它会解决这个问题。



我已经做了我的研究,发现了类似的线程,但解决方案只需检测,如果鼠标位置结束时onDeactivated被称为流浪:的单击C#切换窗口的NotifyIcon(任务栏图标)



更新:
的解决方案,我只贴检测,如果用户鼠标在在任务栏托盘图标,如果你点击任何其他托盘上的onDeactivated事件不会被解雇。结果
我想获得相同的功能系统卷的应用程序。


< DIV CLASS =h2_lin>解决方案

简单明了的时候,窗户是去年隐藏。而忽略了鼠标点击如果近期发生。像这样的:

  INT lastDeactivateTick; 
布尔lastDeactivateValid;

保护覆盖无效OnDeactivate(EventArgs的发送){
base.OnDeactivate(E);
lastDeactivateTick = Environment.TickCount;
lastDeactivateValid = TRUE;
this.Hide();
}

私人无效notifyIcon1_MouseClick(对象发件人,MouseEventArgs E){
如果(lastDeactivateValid&安培;&安培; Environment.TickCount - lastDeactivateTick< 1000)回报;
this.Show();
this.Activate();
}



单击该图标反复现在可以可靠地切换窗口的知名度。


i have a application which is in system tray. i want to make it visible when the user clicks on the notifyIcon, if it's not visible already. If it is already visible it should be hidden. Also when the user clicks anywhere else except on the form the form should hide (if it's visible).

My code looks like this:

  protected override void OnDeactivate(EventArgs e)
  {
        showForm(false);
  }      
  public void showForm(bool show) {
      if(show) {
           Show();   
           Activate();
           WindowState = FormWindowState.Normal;
      } else {
           Hide();
           WindowState = FormWindowState.Minimized;              
      } 
  }  
  private void notifyIcon1_MouseClicked(object sender, MouseEventArgs e)
  {
     if (e.Button == MouseButtons.Left)
        {
            if (WindowState != FormWindowState.Normal)
            {
                showForm(true);
            }
        }
   }

The problem with the code is that onDeactivated gets called before the click call, which hides the form and onclick than just re-shows it. If i could detect if the focus was lost due to a click on notifyIcon or elsewhere it would solve the problem.

I've done my research and found a similar thread, but the solution just detected if the mouse position is over the stray when onDeactivated gets called: C# toggle window by clicking NotifyIcon (taskbar icon)

UPDATE: The solution i posted only detects if the users mouse is over the tray icons in the taskbar, so if you click on any other tray the onDeactivated event wont get fired.
I want to get the same functionality as the system volume app.

解决方案

Simply keep track of the time when the window was last hidden. And ignore the mouse click if that happened recently. Like this:

    int lastDeactivateTick;
    bool lastDeactivateValid;

    protected override void OnDeactivate(EventArgs e) {
        base.OnDeactivate(e);
        lastDeactivateTick = Environment.TickCount;
        lastDeactivateValid = true;
        this.Hide();
    }

    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) {
        if (lastDeactivateValid && Environment.TickCount - lastDeactivateTick < 1000) return;
        this.Show();
        this.Activate();
    }

Clicking the icon repeatedly now reliably toggles the window visibility.

这篇关于C#切换上的NotifyIcon点击表格的知名度和隐藏在其他地方点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆