在Windows窗体应用程序单实例

问题描述:

我有一个名为Restoring.exe的样品WinForm应用程序。同时尽量减少它的窗口,它会移动到系统托盘,并会在任务栏被墙根。如果我点击系统托盘中的通知图标,窗口会走到前面。

I have an sample WinForm application named "Restoring.exe". While minimizing its window, it will move to system tray and will be hided in task bar. 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;
        }



但是我实际的要求是,在单击应用程序第二次,需要恢复从系统托盘中的应用程序。

对于这一点,我想下面的代码

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 in to 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?

制作单实例:

添加到 Microsoft.VisualBasic.dll中引用您的项目,此类添加到您的项目:

Add a reference to Microsoft.VisualBasic.dll to your project and add this class to your project:

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;
        }
    }
}



在的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());
        }
    }
}

现在您的应用程序是单实例。当你点击你的exe文件,而不是运行另一个实例,使现有的实例来正面

Now your application is Single Instance and when you click on your exe file, instead of running another instance, brings the existing instance to front.

使用NotifyIcon的:

将一个的NotifyIcon 主窗体,然后隐藏窗口,当你点击最小化按钮和显示窗口当你点击通知图标,你可以处理这些事件:

Put a NotifyIcon on your main form and then to hide window when you click minimize button and to show windows when you click on notify icon and you can handle these events:

//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;
}