激活隐藏进程的窗口

发布于 2024-12-05 11:08:49 字数 307 浏览 0 评论 0原文

如果用户尝试启动我的应用程序的另一个实例,我想激活已经运行的进程的窗口。

为了查找进程,我调用了 Process.GetProcessesByName(),它为我提供了应用程序正在运行的实例的 System.Diagnostics.Process 实例。但是,我使用以下命令从任务栏隐藏了我的进程。

Form.ShowInTaskbar = false

这导致 Process.MainWindowHandle 为零,因此我无法访问正在运行的进程的当前窗口。

还有其他方法可以激活已运行进程的窗口吗?

If the user tries to start another instance of my application, I want to activate the window of the process which is already running.

To find the process, I call Process.GetProcessesByName(), which gives me the System.Diagnostics.Process instance of the running instance of my application. However, I have hidden my process from the taskbar using

Form.ShowInTaskbar = false

This causes the Process.MainWindowHandle to be zero, so I can not access the current window of the running process.

Is there another way I can activate the window of the already running process?

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

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

发布评论

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

评论(1

鹤仙姿 2024-12-12 11:08:49

是的,ShowInTaskBar 属性很特殊。 Form 类还有几个属于同一类别的其他属性。这些属性由本机 CreateWindowEx() winapi 调用中指定的样式标志实现。 ShowInTaskBar 的 WS_EX_APPWINDOW 标志。

当您更改这些属性时,就会出现问题,必须重新创建窗口。 Winforms 会自动为您完成此操作,但它有几个副作用。其中之一是 Handle 属性值发生变化。使得 Process 类无法找到 MainWindowHandle。

你必须以另一种方式找到窗户。对于 Winforms 表单来说,让 EnumWindows 工作绝对不容易,你无法获得可猜测的窗口类名称。不更改 ShowInTaskBar 属性当然是更好的方法。还可以考虑使用 WindowsFormsApplicationBase 类,它通过 OnStartupNextInstance 方法使此操作变得简单。

Yes, the ShowInTaskBar property is special. There are several other properties of the Form class that are in the same category. These properties are implemented by style flags specified in the native CreateWindowEx() winapi call. The WS_EX_APPWINDOW flag for ShowInTaskBar.

Which is a problem when you change these properties, the window has to be recreated. Winforms does this automatically for you but it has several side-effects. One of which is that the Handle property value changes. Making it impossible for the Process class to find the MainWindowHandle back.

You'll have to find the window back another way. Making EnumWindows work is definitely not easy for Winforms forms, you can't get a guessable window class name. Not changing the ShowInTaskBar property is certainly the better approach. Also consider using the WindowsFormsApplicationBase class, it makes this trivial with the OnStartupNextInstance method.

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