Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:20:29+00:00 2026-06-07T16:20:29+00:00

In my .NET Framework 4 app, I display files, folders and other stuff that

  • 0

In my .NET Framework 4 app, I display files, folders and other stuff that is not part of the file system. For files and folders, I managed to display the Windows Shell context menu for that item, thanks to this code on MSDN. (It still fails to display the Carbonite Shell Extension but I digress.)

My struggle is in adding my own submenu to the Shell context menu. All of my research alludes to so-called Shell Extensions, which, if I understand correctly, is a system-wide change. I only want to add to the Shell context menu when it is accessed from within my app.

Grasping at straws, admittedly, I tried to add the following in the above class’ ShowContextMenu method, just after it calls QueryContextMenu:

var mii = new MENUITEMINFO();
mii.cbSize = Marshal.SizeOf(mii);
mii.fMask = MIIM.SUBMENU | MIIM.STRING | MIIM.FTYPE | MIIM.ID;
mii.fType = MFT.BYPOSITION;
mii.wID = 0;
mii.hSubMenu = subMenu.Handle;
mii.dwTypeData = "Test";
var success = InsertMenuItem(pMenu, 0, true, ref mii);

but with no “success”. (subMenu is a System.Windows.Forms.ContextMenuStrip that I created earlier.)

My questions are:

  1. Am I correct that what I am attempting is distinct from creating a Shell Extension?

  2. Is this still considered to be dangerous activity from managed code, as per this post?

  3. How does one actually do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:20:31+00:00Added an answer on June 7, 2026 at 4:20 pm

    OK, looks like it is possible to add custom menu items to the Windows Shell context menu, just in your app (no need to do anything that affects the entire Windows system). Below is what I did. Keep in mind that this answer should be taken in the context of the MSDN code first mentioned. Here’s the link again:

    ShellContextMenu class on MSDN

    In that class, specifically, the method ShowContextMenu, is a call to QueryContextMenu, whose purpose I learned is to populate the menu with items–in this case, the appropriate Windows Shell menu items for the file/folder whose context menu is to be displayed. After that call, I added the following code to add a submenu and a separator:

    var mii = new MENUITEMINFO();
    mii.fMask = MIIM.SUBMENU | MIIM.STRING | MIIM.FTYPE | MIIM.ID | MIIM.STATE | MIIM.BITMAP;
    mii.fState = MFS.ENABLED;
    mii.fType = MFT.STRING;
    mii.wID = 0;    // Application-defined value that identifies the menu item.
    mii.hSubMenu = **subMenu**.Handle;
    mii.dwTypeData = Program.ShortTitle;
    mii.cch = mii.dwTypeData.Length;
    var bmp = new System.Drawing.Icon(ac.Properties.NeutralResources.MyAppIcon, 16, 16).ToBitmap();
    this.hMyAppSubmenuIcon = bmp.GetHbitmap();
    mii.hbmpItem = this.hMyAppSubmenuIcon;
    mii.cbSize = Marshal.SizeOf(typeof(MENUITEMINFO));
    var success = InsertMenuItem(pMenu, 0, true, ref mii);
    
    mii = new MENUITEMINFO();
    mii.fMask = MIIM.FTYPE;
    mii.fType = MFT.SEPARATOR;
    mii.cbSize = Marshal.SizeOf(mii);
    success = InsertMenuItem(pMenu, 1, true, ref mii);
    

    subMenu is of type System.Windows.Forms.ContextMenu. In this way, the entire context menu is somewhat of a hybrid, consisting of both managed and unmanaged menu items. So far, I don’t see a problem with this; it just means that handling the selection of the two types of menu items must be done differently, as described below…

    After inserting the managed item(s) to the menu, the ShowContextMenu method calls TrackPopupMenu. For Shell menu items, the already-written class takes care of handling their selection. For my own menu items, I had to take extra steps because TrackPopupMenu is a Windows API function so it doesn’t work very well with the managed submenu items’ Click event. You can hook up Click event handlers to your submenu items but when they are selected from the Shell context menu, their Click event doesn’t fire. I did still wire up their Click even because sometimes I just display my managed menu on its own.

    To take action in response to the managed menu item selection from the Shell context menu, I used the return value of TrackPopupMenu, which is the resource ID of the menu item that was selected. That’s where things got a little roundabout. When creating the managed context menu, each menu item has an index. You can use this with the Windows API function GetMenuItemID, which returns the resource ID of the menu item. I inherited from the managed ContextMenu class and encapsulated a Dictionary that helped me to map from this resource ID to the menu item, to be later used just after the call to TrackPopupMenu. For my purposes, that’s all I needed in order to invoke the handler because in my app, I use a command pattern and I stored the command object in the menu item’s Tag property when creating the menu. (Once the dictionary gave me the correct menu item, I was able to execute the corresponding handler by extracting the command object from the Tag).

    This has been running fine for several days now. If anyone see holes, like unmanaged resources that I should have cleaned up, please mention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET Compact Framework app that can runs on three windows machines
Class file Conflicts in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ is preventing me from building the solution.
I am developing a Windows Mobile 6.5 (.NET Compact Framework 3.5) app that will
I have an app using the ADO.NET entity framework (the VS2008 version, not the
I have a .NET Framework 2.0 app and would like to use the Windows
I have a WinForm .NET 2.0 framework app that works fine in Vista, Win
I'm writing a Windows Forms app, VS2010, NET Framework 4.0, coding in VB. I'm
I have a .NET 4 WinForms app that uses the ADO.NET Entity Framework. Some
I have an app that was created using .net Framework 3.5. However, I now
Few years ago I've developed a windows service app (VB.NET framework 1.1) which was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.