HOWTO: SAMPLE : Displaying Context Menu Given the Path to a File
ID: Q198288
|
The information in this article applies to:
-
Microsoft Win32 Software Development Kit (SDK)
on the following platforms: Win95
-
Microsoft Windows NT 4.0
-
Microsoft Windows versions 95, 98
SUMMARY
The CtxMenu.exe sample shows the steps involved in displaying a
context menu, given the path to a file name. This is the same menu that is
displayed by Explorer when the context menu for a file is invoked.
Displaying a context menu given the path to a file name is complicated.
There are two cases to be considered:
- Case 1: When the path to the file name is the root, such as C:\ or D:\.
- Case 2: Any path other than the root.
In either Case 1 or Case 2, to display a context menu, the following steps must be performed:
- Get the parent folder.
- Get the IShellFolder of the parent folder.
- Get the pidl to the file name with respect to the parent folder's
IshellFolder.
- Get the ContextMenu interface for the parent folder.
- Call the QueryContextMenu with the pidl to the file name. This action populates a HMENU with the context menu items for the file name.
- Display the context menu using TrackPopupMenu.
For the two cases above, the main difference is in obtaining the pidl to
the file name with respect to the parent folder.
Case 1: When the File Name Is a Root
In the case when the given file name is a root, the parent folder is "My
Computer." To obtain the IShellFolder of MyComputer, we need to get the
pidl to My Computer by using SHGetSpecialFolderLocation for CSIDL_DRIVES. We can get the IShellFolder of MyComputer by passing this pidl to the desktop folder's BindToObject. Once the IShellFolder of MyComputer is obtained, ParseDisplayName can be used to get the pidl of the given file name with respect to MyComputer's IShellFolder. Then the context menu interface of IShellFolder of MyComputer can be obtained by calling GetUIObjectOf with IID_IContextMenu. Once the context menu interface is obtained, QueryContextMenu can be used to get all the context menu items.
Case 2: When the File Name Is Not a Root Folder
In the case when the given file name is not a root folder, then the file
name is parsed to obtain the parent folder. The path to the parent folder
is passed to desktop folder's ParseDisplayName to get the pidl. Using this
pidl, the IShellFolder of the parent folder can be obtained by using the
desktop IShellFolder's BindToObject. Then ParseDisplayName can be used to
get the pidl of the given file name with respect to its parent's
IShellFolder. Now the context menu interface of IShellFolder of the parent
folder can be obtained by calling GetUIObjectOf with IID_IContextMenu. Once
the context menu interface is obtained, QueryContextMenu can be used to get
all the context menu items.
MORE INFORMATION
The following file is available for download from the Microsoft Software
Library:
CtxMenu.exe
Release Date: Jan. 15, 1999
For more information about downloading files from the Microsoft Software
Library, please see the following article in the Microsoft Knowledge Base:
How to Obtain Microsoft Support Files from Online Services
Additional query words:
Keywords : kbfile kbsample kbNTOS400 kbSDKWin32 kbWinOS95 kbWinOS98
Version : WINDOWS:95,98; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbhowto
Last Reviewed: January 16, 1999