在MFC中启动外部exe,如果已经打开,如何将exe置于前面?

发布于 2024-08-20 11:18:35 字数 244 浏览 1 评论 0原文

我必须在 MFC 应用程序中打开一个外部程序,该程序可以工作,但如果我已经单击了按钮,我不希望它打开另一个实例,而只是将其放在前面。文档说 SW_SHOWNORMAL 参数可以做到这一点,但它对我不起作用。被调用的程序是否也必须为此进行设置?

ShellExecute(NULL, "open", "C:\Test\blahblah.exe", NULL, NULL, SW_SHOWNORMAL);

谢谢, CP

I have to open up a external program in my MFC application, which works but if I have already clicked button, I don't want it to open another instance but just bring it to front. The docs say the SW_SHOWNORMAL parameter does this, but it is not working for me. Does the called program have to setup for this also?

ShellExecute(NULL, "open", "C:\Test\blahblah.exe", NULL, NULL, SW_SHOWNORMAL);

Thanks,
CP

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

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

发布评论

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

评论(1

冧九 2024-08-27 11:18:35

该文档有点不清楚,这可能让您感到困惑。当以可执行程序为目标时,ShellExecute() 将始终启动一个新进程。当文档提到恢复最小化窗口时,它指的是如果您正在定位一个文档(如 Word 文档),并且注册用于显示该文档的应用程序已经在运行。

最好的选择是:

  • 修改外部程序以支持 Singleton 模式,并在尝试启动第二个副本时将其自身置于前面和中心,或者
  • 使用 FindWindow() 在 MFC 应用程序中尝试查找外部应用程序,然后仅在外部应用程序不存在时运行 ShellExecute(),否则激活现有窗口。

The documentation is a bit unclear, which might have been what confused you. When targetting an executable program, ShellExecute() will always launch a new process. What the documentation is referring to when it mentions restoring a minimized window is if you are targetting a document (like a Word doc), and the application registered to display the document is already running.

Your best bet is to either:

  • Modify the external program to support the Singleton pattern, and to bring itself front and center if you try to launch a second copy, or
  • Use FindWindow() in your MFC application to try to locate the external application, and then only run ShellExecute() if it doesn't already exist, otherwise activating the existing window.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文