简体   繁体   English

在另一个应用程序的窗口中显示一个弹出菜单

[英]Show a popup menu in another application's window

How can a Delphi XE application show a popup menu inside another application's window? Delphi XE应用程序如何在另一个应用程序的窗口内显示弹出菜单? The idea is for a helper-type app, running in the background. 这个想法是为了在后台运行一个助手类型的应用程序。 On a registered hotkey the application needs to display a popup menu near the text caret or mouse cursor. 在注册的热键上,应用程序需要在文本插入符号或鼠标光标附近显示一个弹出菜单。

Applications that do that are common, here's a menu created by AutoHotkey and displayed in a text editor: 常见的应用程序,这是由AutoHotkey创建并显示在文本编辑器中的菜单:

附加到另一个应用程序窗口的弹出菜单

I guess what I'm asking is: how can I display a popup menu at an arbitrary screen location, without it being attached to a Delphi control? 我想我要问的是:如何在不附加到Delphi控件的情况下在任意屏幕位置显示弹出菜单?

Create a TPopupMenu with the appropriate menu items. 用适当的菜单项创建一个TPopupMenu When you need to show it simply call Popup passing the top left position in screen coordinates. 当您需要显示它时,只需调用Popup即可在屏幕坐标中传递左上角的位置。

PopupMenu1.Popup(X, Y);

@DavidHeffernan answered your question, but you might not have asked the right question. @DavidHeffernan回答了您的问题,但您可能没有提出正确的问题。

Let's take the example you gave: the user is running some arbirary application, and you want to be able to detect a hotkey, display a menu, and then take some action based on the menu item chosen (and maybe even the user's context, such as the word under the cursor). 让我们以您提供的示例为例:用户正在运行某个应用程序,并且您希望能够检测到一个热键,显示一个菜单,然后根据所选菜单项(甚至可能是用户的上下文,例如作为光标下方的单词)。 This is more complicated than simply displaying a menu at arbitrary screen coordinates. 这比简单地在任意屏幕坐标处显示菜单要复杂得多。

My recommendation is to use AutoHotKey instead of trying to replicate this in some other programming language. 我的建议是使用AutoHotKey而不是尝试以其他某种编程语言来复制它。 In case you're not aware of this, it is possible for your code to run AutoHotKey scripts. 如果您不知道这一点,您的代码可能会运行AutoHotKey脚本。 IIRC, you can compile AHK scripts, so you wouldn't need to install AHK, just the compiled scripts. IIRC,您可以编译AHK脚本,因此您无需安装AHK,只需安装已编译的脚本即可。 AHK may not be the most elegant of solutions, but it has depth and maturity. AHK可能不是最优雅的解决方案,但它具有深度和成熟度。

If this is not possible, then I suggest you research Windows Hooks and DLL Injection. 如果这不可能,那么建议您研究Windows Hooks和DLL注入。 Unless you can find some preexisting code or framework, this will entail quite a bit of work. 除非您找到一些预先存在的代码或框架,否则将需要进行大量工作。

The reason for this complexity? 这种复杂的原因? To augment another program smoothly (without running into problems with focus, etc.) you want to have your code run as part of that other program. 为了平稳地扩充另一个程序(而不会遇到焦点问题等),您希望将代码作为另一个程序的一部分运行。 The mechanics of this can be done via DLL injection. 这种机制可以通过DLL注入来完成。 However, that's only the first step. 但是,这仅仅是第一步。 Once your code is running in the right context, then your code has to inter-operate with the "host" program. 一旦您的代码在正确的上下文中运行,那么您的代码就必须与“主机”程序进行互操作。 This can be tricky (it helps if you have deep experience with Windows messaging and the Windows API). 这可能很棘手(如果您对Windows消息传递和Windows API有深入的了解,这将很有帮助)。 If you want this to work smoothly with any arbitrary program, it gets even harder. 如果您希望此程序与任意程序一起顺利运行,则将变得更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM