I have a sample WinForm application named "Restoring.exe". While minimizing its window, it will move to system tray and will be hidden in the taskbar. If I click on the notification icon in system tray, the window will come to the front.
public void notifyicon_MouseClick(object sender, System.EventArgs e)
{
notifyicon.Visible = false;
Show();
Activate();
TopMost = true;
BringToFront();
this.WindowState = FormWindowState.Normal;
}
But my actual requirement is, while clicking the application 2nd time, need to restore the application from system tray.
For this, I tried the below code
Program.cs:
static void Main()
{
if (IsServiceManagerAlreadyRunning())
{
Form1 form1 = new Form1();
form1.Restore();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Form1.cs:
public void Restore()
{
notifyicon.Visible = false;
Show();
Activate();
TopMost = true;
BringToFront();
this.WindowState = FormWindowState.Normal;
}
My actual issue is, if the application is already running, the 'Restore' method is hitting and all the actions listed in that is running and the window is appearing into front. But after completed those actions, the window again goes to the system tray. Not sitting in front.
Could anyone please provide a solution for this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…