gpt4 book ai didi

c# - 具有最小化到托盘的单实例 Windows 窗体应用程序

转载 作者:太空狗 更新时间:2023-10-29 23:13:44 25 4
gpt4 key购买 nike

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

public void notifyicon_MouseClick(object sender, System.EventArgs e)
{
notifyicon.Visible = false;
Show();
Activate();
TopMost = true;
BringToFront();
this.WindowState = FormWindowState.Normal;
}

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

为此,我尝试了下面的代码

程序.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;
}

我的实际问题是,如果应用程序已经在运行,则“恢复”方法正在运行并且其中列出的所有操作都在运行并且窗口出现在前面。 但在完成这些操作后,窗口再次进入系统托盘。没有坐在前面。

有人可以为此提供解决方案吗?

最佳答案

制作单实例

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

然后在您的 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 文件时,不会运行另一个实例,而是将现有实例置于最前面。

使用 NotifyIcon

在你的主窗体上放置一个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;
}

关于c# - 具有最小化到托盘的单实例 Windows 窗体应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33021137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com