如何显示“发送至”和“打开方式”上下文菜单?
前段时间我问过如何在Delphi中弹出某个文件的Windows上下文(右键)菜单。但是,即使一切正常(几乎)正常,“发送到...”和“打开方式...”子菜单也没有任何项目,即使当我在资源管理器中右键单击相同的文件名时,它们也会出现工作正常。 (例如,“发送到...”有“桌面(创建快照)”、“电子邮件收件人”等)。
我怎样才能让这些菜单出现?
Some time ago I asked about how to pop up the Windows context (right-click) menu for a certain file in Delphi. However, even if everything works (almost) OK, the 'Send to...' and 'Open with...' submenus don't have any items, even if when I right-click in Explorer on the same file name they work OK. (For example, 'Send to...' has 'Desktop (create shotcut)', 'Email recipient' etc.).
How can I make those menus appear?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Raymond Chen 几年前在有关托管上下文菜单的多部分系列文章中讨论了这个确切的问题。他的文章的关键主题是,当您阅读文档时,您必须记住您的程序正在扮演 shell 的角色,而文档通常是从实现 shell 的代码的角度编写的。上下文菜单接口,而不是调用它们的代码。
早在第 2 部分中,Chen 就观察到发送到并且打开菜单显示为空。 第 5 部分解决了您对这些子菜单的直接担忧。答案是您需要处理菜单消息。有一个与您的菜单关联的窗口句柄 - 您提供给
TrackPopupMenuEx
的句柄(Issam 对您问题的回答)。当菜单处于活动状态时,该窗口将接收消息,并且需要将它们转发到实现菜单命令的对象。该窗口有一个窗口过程,您需要在其中调用菜单界面的HandleMenuMsg
和HandleMenuMsg2
方法。文章中有一个例子。本系列文章的其他部分涵盖了当您说您的菜单“几乎”有效时毫无疑问提到的其他内容。 第 3 部分讨论属性对话框的显示位置。 第 7 部分 有关调用默认动词的信息。我建议您阅读整个系列。不要担心一切都是用 C++ 编写的;它几乎都是接口方法和API函数,所以它应该可以毫不费力地翻译成Delphi。
Raymond Chen wrote about this precise problem a few years ago in a many-part series about hosting a context menu. The key theme to his articles was that as you read the documentation, you have to remember that your program is playing the part of the shell, whereas the documentation is normally written from the perspective of the code implementing the context-menu interfaces, as opposed to the code calling them.
As early as part 2, Chen observed that the send-to and open-with menus appear empty. Part 5 addresses your immediate concern about those submenus. The answer is that you need to handle menu messages. There is a window handle associated with your menu — the handle you provided to
TrackPopupMenuEx
(HND
in Issam's answer to your question). That window will receive messages while the menu is active, and it needs to forward them to the objects implementing the menu commands. The window has a window procedure, and that's where you need to call the menu interface'sHandleMenuMsg
andHandleMenuMsg2
methods. The article has an example.Other parts of the article series cover the other things that you no-doubt alluded to when you said your menu "almost" works. Part 3 talks about where the property dialog appears. Part 7 s about invoking the default verb. I suggest you read the entire series. Don't fear that everything's in C++; it's almost all interface methods and API functions, so it should translate to Delphi without much trouble.