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
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.