HOWTO: 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:
  1. Get the parent folder.


  2. Get the IShellFolder of the parent folder.


  3. Get the pidl to the file name with respect to the parent folder's IshellFolder.


  4. Get the ContextMenu interface for the parent folder.


  5. Call the QueryContextMenu with the pidl to the file name. This action populates a HMENU with the context menu items for the file name.


  6. 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 Download Center. Click the file name below to download the file:

CtxMenu.exe
Release Date: Jan. 15, 1999

For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.

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: December 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.