我有一个名为"Restoring.exe"的示例WinForm应用程序.在最小化窗口的同时,它将移动到系统托盘并隐藏在任务栏中.如果单击系统托盘中的通知图标,则窗口将显示在前面.
public void notifyicon_MouseClick(object sender, System.EventArgs e)
{
notifyicon.Visible = false;
Show();
Activate();
TopMost = true;
BringToFront();
this.WindowState = FormWindowState.Normal;
}
Run Code Online (Sandbox Code Playgroud)
但我的实际要求是,在第二次单击应用程序时,需要从系统托盘恢复应用程序.
为此,我尝试了以下代码
Program.cs中:
static void Main()
{
if (IsServiceManagerAlreadyRunning())
{
Form1 form1 = new Form1();
form1.Restore();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Run Code Online (Sandbox Code Playgroud)
Form1.cs中:
public void Restore()
{
notifyicon.Visible = false;
Show();
Activate();
TopMost = true;
BringToFront();
this.WindowState = FormWindowState.Normal;
}
Run Code Online (Sandbox Code Playgroud)
我的实际问题是,如果应用程序已经运行,则"恢复"方法正在命中,并且其中列出的所有操作都在运行,并且窗口显示在前面.但在完成这些操作后,窗口再次进入系统托盘.不坐在前面.
有谁能请为此提供解决方案?
添加Microsoft.VisualBasic.dll对项目的引用并将此类添加到项目中:
using System;
using Microsoft.VisualBasic.ApplicationServices;
using System.Windows.Forms;
namespace Sample
{
public class ApplicationController : WindowsFormsApplicationBase
{
private Form mainForm;
public ApplicationController(Form form)
{
//We keep a reference to main form
//To run and also use it when we need to bring to front
mainForm = form;
this.IsSingleInstance = true;
this.StartupNextInstance += this_StartupNextInstance;
}
void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
{
//Here we bring application to front
e.BringToForeground = true;
mainForm.ShowInTaskbar = true;
mainForm.WindowState = FormWindowState.Minimized;
mainForm.Show();
mainForm.WindowState = FormWindowState.Normal;
}
protected override void OnCreateMainForm()
{
this.MainForm = mainForm;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的Program.cs使用中ApplicationController运行程序:
using System;
using System.Windows.Forms;
namespace Sample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//create a controller and Pass an instance of your application main form
var controller = new Sample.ApplicationController(new YourMainForm());
//Run application
controller.Run(Environment.GetCommandLineArgs());
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在您的应用程序是单实例,当您单击您的exe文件时,而不是运行另一个实例,将现有实例放在前面.
把NotifyIcon你的主窗体上,然后隐藏窗口,当你点击最小化按钮和显示窗口,当你点击图标,通知您可以处理这些事件:
//When click on notify icon, we bring the form to front
private void notifyIcon_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
}
//here we check if the user minimized window, we hide the form
private void ApplicationMainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
}
}
//when the form is hidden, we show notify icon and when the form is visible we hide it
private void ApplicationMainForm_VisibleChanged(object sender, EventArgs e)
{
this.notifyIcon1.Visible = !this.Visible;
}
Run Code Online (Sandbox Code Playgroud)