重新激活托盘中运行的应用程序的最佳方法是什么?
我有一个 delphi 应用程序,它运行时最小化为托盘图标。双击托盘图标时,应用程序将打开一个非模式用户界面表单。
我已向应用程序添加了逻辑来检测它是否已经在运行。如果它没有运行,它会启动并将自身最小化到托盘中。
如果它已经在运行,我希望它将控制权传递给其自身的第一个实例并打开非模式窗体,然后退出(第二个实例)。最好的方法是什么?
TIA 右
I have a delphi app that runs minimized to a tray icon. When the tray icon is double clicked the app opens a non-modal user interface form.
I have added logic to the app to detect whether it is already running. If it isn't running, it starts up and miminizes itself to the tray.
If it is already running, I want it to pass control to the first instance of itself and open the non-modal form, and then exit (the second instance). What's the best way to do this?
TIA
R
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
推荐检测另一个实例的方法给定的应用程序是让该应用程序创建一个命名互斥体或在众所周知的位置锁定文件,以便当您尝试创建相同的互斥体或锁定同一文件时,第二个实例将触发错误。一旦您知道有另一个实例正在运行,您就可以找到该实例的进程句柄,并向其发送一条消息以在其最小化时进行恢复。
The recommended method of detecting another instance of a given application is for that application to create a named mutex or lock a file in a well known location, so that the second instance will trigger an error when you try to create the same mutex or lock the same file. Once you know there's another instance running, you can find the process handle for that instance and send it a message to restore if its minimized.
微软的方式并不是完美无缺的,所以我更喜欢老派:
现在,第一个实例的 WM_KNOCK_KNOCK 消息处理程序执行唤醒例程。
当您在 Shell_NotifyIcon 包装器(Application.Restore,也许?)中收到 WM_LBUTTONUP (或者可能是 WM_LBUTTONDBLCLK)时,我几乎不知道您到底做了什么。正如 Chris Thornton 所说,不存在“最小化到托盘”这样的状态,它是人为的。
后备:如果断言失败,请注意哪些代码仅依赖于类函数
ClassName
,因此可以轻松地从FormCreate
中移出并在应用程序创建它之前调用。Microsoft way is not flawless, so i do prefer old school:
Now, WM_KNOCK_KNOCK message handler of first instance performs wakeup routine.
i have little clue what exactly you do when you receive WM_LBUTTONUP (or perhaps WM_LBUTTONDBLCLK) in your Shell_NotifyIcon wrapper (Application.Restore, maybe?). As, Chris Thornton said, there is no such state as 'minimized to tray', it is artifical.
Fallback: if assertion fails, note what code depends only on class function
ClassName
so could be easily moved out ofFormCreate
and invoked before Application creates it.