具有最小化到纸盒的单实例Windows窗体应用程序 [英] Single Instance Windows Forms Application with Minimize to Tray

查看:69
本文介绍了具有最小化到纸盒的单实例Windows窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Restoring.exe的WinForm应用程序示例。在最小化其窗口的同时,它将移至系统托盘并将其隐藏在任务栏中。如果我单击系统任务栏中的通知图标,则该窗口将显示在最前面。

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

但是我的实际要求是,第二次单击该应用程序时,需要从系统托盘中还原应用程序。

为此,我尝试了以下代码

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?

推荐答案

制作单个实例



添加对<$ c的引用$ c> Microsoft.VisualBasic.dll 到您的项目中,并将该类添加到您的项目中:

Making Single Instance

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 ,然后在单击最小化按钮时隐藏窗口,并在单击通知图标时显示窗口,您可以处理这些事件:

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

这篇关于具有最小化到纸盒的单实例Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆