最大化系统托盘中的应用程序?

发布于 2024-10-10 17:56:04 字数 206 浏览 0 评论 0原文

我编写了一个小 WPF 应用程序,当“关闭”时最小化到系统托盘(客户要求)。双击可将其弹出,或者右键单击可提供上下文菜单以退出。

但是,如果应用程序最小化,并且用户导航到“开始”->“所有程序”->“应用程序”,它将启动一个新实例。

如果用户这样做而不是启动新实例,我需要做什么(在 C# 中)才能让应用程序最大化正在运行的实例?

谢谢!

I wrote a little WPF app that when 'closed' minimizes to the system tray (customer requirement). Double clicking pops it back up, or right click gives a context menu to exit.

But if the app is minimized, and the users navigate to Start->All Programs->The Application it starts a new instance.

What (in C#) do I need to do to get the app to maximize the running instance if the user does this rather than fire up a new instance?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2024-10-17 17:56:04

Jon Skeet 的答案讨论了使用互斥锁来做到这一点

互斥是正确的选择。很多
比使用进程名称更不容易脆弱
等等

但是,您需要确保
互斥量不会被垃圾回收。在
服务的情况(即事件
驱动而非“主要”
运行至完成的方法),
最明智的做法是
可能把它放在静态中
变量。

服务运行时释放互斥体
停止,所以你不需要等待
最终确定或类似的东西。

马修·布林德利 (Matthew Brindley) 在 的同一问题中给出了这个示例他的回答

[STAThread]
static void Main() 
{
   using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
   {
      if(!mutex.WaitOne(0, false))
      {
         MessageBox.Show("Instance already running");
         return;
      }

      Application.Run(new Form1());
   }
}

要最大化其他应用程序,您需要向其发送最大化消息。请参阅这篇关于消息发送的文章

This answer from Jon Skeet discusses using a mutex to do it

Mutex is the way to go. It's a lot
less fragile than using process names
etc.

However, you need to make sure the
Mutex isn't garbage collected. In the
case of a service (which is event
driven rather than having a "main"
method which runs to completion), the
most sensible way of doing this is
probably to put it in a static
variable.

Dispose of the mutex when the service
stops, so you don't need to wait for
finalization or anything like that.

Matthew Brindley gives this example in the same question for his answer

[STAThread]
static void Main() 
{
   using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
   {
      if(!mutex.WaitOne(0, false))
      {
         MessageBox.Show("Instance already running");
         return;
      }

      Application.Run(new Form1());
   }
}

To maximize the other application you'll need to send it the message to maximize. See this article on message sending

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文