gpt4 book ai didi

c# - 当只允许该程序的一个实例时从系统托盘恢复窗口

转载 作者:太空狗 更新时间:2023-10-30 00:45:54 26 4
gpt4 key购买 nike

好的,标题很长,应该能说明我面临的问题。

这是最小化到图标托盘时的代码:

void MainFormResize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
this.ShowInTaskbar = false;
}
}

当程序已经打开并在系统托盘中时,仍然有人想打开它的另一个实例,然后:

    private static void Main(string[] args)
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "IPADcommunicator", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
IntPtr handle = FindWindow(null,"IPADcommunicator");
SetForegroundWindow(handle);
ShowWindow(handle,5);

break;
}
}
...

但是,它不能正常工作。主窗口未恢复。我在谷歌上搜索了很多,但没有找到解决该问题的方法。提前致谢!

最佳答案

在不可见窗口上调用 SetForegroundWindow() 是行不通的。还有许多其他可能的失败模式,当您开始传递 null 时,FindWindow() 是一种悲惨的模式。

不要自己发明这个,.NET 已经对单实例应用程序提供了很好的内置支持。您甚至可以在第二个副本开始并通过命令行时收到通知。这就是你想要的,只需恢复窗口而不是破解 API。您需要的代码 is here .

关于c# - 当只允许该程序的一个实例时从系统托盘恢复窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4592852/

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