在NotifyIcon单击时切换表单可见性,而在其他位置单击时隐藏它 [英] Toggle form visibility on NotifyIcon click and hide it on click elsewhere
本文介绍了在NotifyIcon单击时切换表单可见性,而在其他位置单击时隐藏它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在系统托盘中有一个应用程序.我想在用户单击 notifyIcon
时使它可见(如果尚不可见).如果已经可见,则应将其隐藏.同样,当用户单击除表单以外的其他任何位置时,表单也应隐藏(如果可见).
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).
我的代码如下:
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);
}
}
}
该代码的问题在于,在单击调用之前调用了 onDeactivated
,这隐藏了表单,并
查看全文