大网数据广告活动
域与目录服务

Implementing the Context Menu COM Object

· 52CE 我爱测

A context menu extension is a COM object implemented as an in-proc server.

A context menu extension is a COM object implemented as an in-proc server. The context menu extension must implement the IShellExtInit and IContextMenu interfaces. A context menu extension is instantiated when the user displays the context menu for an object of a class for which the context menu extension has been registered.

Implementing IShellExtInit

After the context menu extension COM object is instantiated, the IShellExtInit::Initialize method is called. IShellExtInit::Initialize supplies the context menu extension with an IDataObject object that contains data pertinent to the directory object that the context menu applies to.

The IDataObject contains data in the CFSTR_DSOBJECTNAMES format. The CFSTR_DSOBJECTNAMES data format is an HGLOBAL that contains a DSOBJECTNAMES structure. The DSOBJECTNAMES structure contains data about the directory object that the property sheet extension applies to.

The IDataObject also contains data in the CFSTR_DS_DISPLAY_SPEC_OPTIONS format. The CFSTR_DS_DISPLAY_SPEC_OPTIONS data format is an HGLOBAL that contains a DSDISPLAYSPECOPTIONS structure. The DSDISPLAYSPECOPTIONS contains configuration data for use by the extension.

If any value other than S_OK is returned from IShellExtInit::Initialize, the context menu extension will not be used.

The pidlFolder and hkeyProgID parameters of the IShellExtInit::Initialize method are not used.

Implementing IContextMenu

After IShellExtInit::Initialize returns, the IContextMenu::QueryContextMenu method is called to obtain the menu item or items that the context menu extension will add. The QueryContextMenu implementation is fairly straightforward. The context menu extension adds its menu items using the InsertMenuItem or similar function. The menu command identifiers must be greater than or equal to idCmdFirst and must be less than idCmdLast. QueryContextMenu must return the greatest numeric identifier added to the menu plus one. The best way to assign menu command identifiers is to start at zero and work up in sequence. If the context menu extension does not need to any menu items, it should simply not add any items to the menu and return zero from QueryContextMenu.

IContextMenu::GetCommandString is called to retrieve textual data for the menu item, such as help text to be displayed for the menu item. It is possible that the context menu host will use Unicode strings while the extension uses ANSI strings. Because of this, the GCS_HELPTEXTA, GCS_HELPTEXTW, GCS_VERBA and GCS_VERBW cases must be handled individually. Implementation of this method is optional.

IContextMenu::InvokeCommand is called when one of the menu items installed by the context menu extension is selected. The context menu performs or initiates the desired actions in response to this method.

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。