How can I display the 'Send to' and 'Open With' context menus?
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?
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's HandleMenuMsg
and HandleMenuMsg2
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.
精彩评论