windows 在 NotifyIcon 单击上切换表单可见性并在其他地方单击时将其隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7309098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Toggle form visibility on NotifyIcon click and hide it on click elsewhere
提问by blejzz
I have an 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).
我有一个在系统托盘中的应用程序。我想让它在用户点击 时可见notifyIcon
,如果它已经不可见。如果它已经可见,则应将其隐藏。此外,当用户单击表单之外的任何其他地方时,表单应该隐藏(如果它可见)。
My code looks like this:
我的代码如下所示:
protected override void OnDeactivated(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 notifyIcon1_MouseClicked
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.
代码的问题在于它onDeactivated
在单击调用之前被调用,它隐藏了表单,notifyIcon1_MouseClicked
而不仅仅是重新显示它。如果我能检测到焦点是否由于点击notifyIcon
或其他地方而丢失,它将解决问题。
I've done my research and found a similar thread, but the solution just detected if the mouse position is over the tray when onDeactivated
gets called: C# toggle window by clicking NotifyIcon (taskbar icon)
我已经完成了我的研究并发现了一个类似的线程,但是该解决方案只是onDeactivated
在调用时检测到鼠标位置是否在托盘上:C# toggle window by click NotifyIcon (taskbar icon)
UPDATE:
The solution I posted only detects if the user's mouse is over the tray icons in the taskbar, so if you click on any other tray the onDeactivated
event won't get fired.
I want to get the same functionality as the system volume app.
更新:我发布的解决方案仅检测用户的鼠标是否位于任务栏中的托盘图标上,因此如果您单击任何其他托盘,onDeactivated
则不会触发该事件。我想获得与系统音量应用程序相同的功能。
回答by Hans Passant
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.
重复单击图标现在可以可靠地切换窗口可见性。