- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个名为“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
,然后当你点击最小化按钮时隐藏窗口,当你点击通知图标时显示窗口,你可以处理这些事件:
//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/
我想在同一运行时间内拥有多种货币。默认节点模板中插入了 Balances 托盘,但如果我正确的话,它只能处理一种货币。 如何多次重复使用托盘? 最佳答案 Pallet 可以实例化,这样您就可以在同一运
我正在尝试使用一段代码打开和关闭计算机的 CD 托盘。我一直在使用 MCI 命令,并将 winmm.lib 包含在我的项目配置的附加依赖项中。我还包含了 windows.h 和 mmsystem.h。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我正在使用electron-react-boilerplate开发 Electron 应用程序(使用electron-builder打包应用程序)。 我想创建托盘,但是它需要图标路径或 native
有没有办法选择在 Java 中打印时使用哪个出纸盒/托盘?有一个属性 MediaTray 允许您选择输入托盘,但我找不到允许您选择输出的属性。 最佳答案 除了MediaTray , 有一个 Desti
我是一名优秀的程序员,十分优秀!