locked
Custom Project System adding context menu item to solution explorer RRS feed

  • Question

  • Hello All!

    I have a MPF custom project system and I want to add a new menu item to context menu which opens when we right click on solution explorer. Later, I want to open a new window in click event of that menu item. Brief instructions are needed. Or maybe a simple example.

    If more information is needed, ask please and I will share.

    Thanks!

    Thursday, June 18, 2015 7:29 AM

Answers

  • Looks okay to me.

    Only thing I can think of offhand is to ensure the cmdset guid and command id is defined the same in our C# code.

    Meaning guidMVP_WebForms_ViewCmdSet  =     a8a2e358-61a6-4f67-a986-eeab677e9cfd

    and cmdidMyCommand = 0x0100

    The only other thing that comes to mind is that perhaps this is being handled/ignored by the project's IOleCommandTarget or IVsUIHierarchy implementation of Exec or ExecCommand.

    Normally, I'd recommend adding your menu handlers there anyway, as this appears to be a command specific to your project type.

    Sincerely,


    Ed Dore

    Monday, June 29, 2015 9:38 PM

All replies

  • Hi hakdag,

    For how to add context menu item in the solution explorer's items, please see my code sample in this another thread:

    Solution Explorer Custom Context Menu for Specific File Types

    The basic idea is to create a VSPackage project for your custom project system, modify the vsct file, set the menu button's parent as the solution explorer's context item node. Then handle the menu command's callback event.

    To open a new window in the menu command's click event, just create a Winform/WPF form, or hold the form in a ToolWindow, for more information, see the Tool Windows documentation here:

    https://msdn.microsoft.com/en-us/library/bb165073.aspx

    Some of the VS2013 SDK samples could also give you some help:

    http://blogs.msdn.com/b/vsx/archive/2014/05/30/vs-2013-sdk-samples-released.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 19, 2015 5:51 AM
  • Hello Caillen,

    Thank you for your answer. I examined links you have written. I did pretty much the same thing. I want to imply that I am developing a project system using MPF. Main file of a project system is a package class which inherits from ProjectPackage.

    This is my package file.

        [PackageRegistration(UseManagedResourcesOnly = true)]
        [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\10.0")]
        [ProvideObject(typeof(GeneralPropertyPage))]
        [ProvideProjectFactory(typeof(WebProjectFactory), "MVP Project for Web Forms", "MVP Project for Web Forms Files (*.mvpproj);*.mvpproj", "mvpproj", "mvpproj", @"..\..\Templates\Projects\MVPTemplate", LanguageVsTemplate = "MVPWebForms", NewProjectRequireNewFolderVsTemplate = false)]
        [ProvideProjectItem(typeof(WebProjectFactory), "My Items", @"..\..\Templates\ProjectItems\MyCustomProject", 500)]
        [ProvideMenuResource("Menus.ctmenu", 1)]
        [ProvideAutoLoad(UIContextGuids.SolutionExists)]
        [ProvideOptionPage(typeof(OptionsPageGeneral), "My Option Pages", "General", 101, 106, true)]
        [Guid(GuidStrings.guidCustomProjectPkgString)]
        public sealed class WebProjectPackage : ProjectPackage
        {
            #region Overridden Implementation
            protected override void Initialize()
            {
                base.Initialize();
                this.RegisterProjectFactory(new WebProjectFactory(this));
    
                OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
                if (null == mcs) return;
    
                try
                {
                     CommandID menuCommandID = new CommandID(GuidStrings.guidMVP_WebForms_ViewCmdSet, (int)PkgCmdIDList.cmdidMyCommand);
                    MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
                    mcs.AddCommand(menuItem);
                }
                catch (Exception exc)
                {
                    MessageBox.Show("Exception: " + exc.Message);
                }
            }
    
            private void MenuItemCallback(object sender, EventArgs e)
            {
                MessageBox.Show("Button clicked!");
            }
    
            public override string ProductUserContext
            {
                get { return "MVPWebForms"; }
            }
    
            #endregion
        }
    


    After clicking button, I am excpecting a message box which says "Button clicked!" but nothing happens.

    This is .vsct file

    <?xml version="1.0" encoding="utf-8"?>
    <CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <Extern href="stdidcmd.h"/>
      <Extern href="vsshlids.h"/>
    
      <Commands package="guidMVP_WebForms_ViewPkg">
        <Groups>
          <Group guid="guidMVP_WebForms_ViewCmdSet" id="MyMenuGroup" priority="0x0100">
            <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>
          </Group>
        </Groups>
    
        <Buttons>
          <Button guid="guidMVP_WebForms_ViewCmdSet" id="cmdidCreateViewCommand" priority="0x0100" type="Button">
            <Parent guid="guidMVP_WebForms_ViewCmdSet" id="MyMenuGroup" />
            <Icon guid="guidImages" id="bmpPicSearch" />
            <Strings>
              <CommandName>cmdidCreateViewCommand</CommandName>
              <ButtonText>Create View</ButtonText>
            </Strings>
          </Button>
        </Buttons>
    
        <Bitmaps>
          <Bitmap guid="guidImages" href="Resources\Images.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows"/>
        </Bitmaps>
      </Commands>
    
      <Symbols>
        <GuidSymbol name="guidMVP_WebForms_ViewPkg" value="{e3543c00-b8c3-4d07-83e5-6e2e640be201}" />
        <GuidSymbol name="guidMVP_WebForms_ViewCmdSet" value="{a8a2e358-61a6-4f67-a986-eeab677e9cfd}">
          <IDSymbol name="MyMenuGroup" value="0x1020" />
          <IDSymbol name="cmdidCreateViewCommand" value="0x0100" />
        </GuidSymbol>
    
        <GuidSymbol name="guidImages" value="{ffc2cc76-f613-4c05-8552-3a0ae7f80606}" >
          <IDSymbol name="bmpPic1" value="1" />
          <IDSymbol name="bmpPic2" value="2" />
          <IDSymbol name="bmpPicSearch" value="3" />
          <IDSymbol name="bmpPicX" value="4" />
          <IDSymbol name="bmpPicArrows" value="5" />
          <IDSymbol name="bmpPicStrikethrough" value="6" />
        </GuidSymbol>
      </Symbols>
    
    </CommandTable>
    

    I can provide further informatin if needed.

    Thanks.

    Saturday, June 27, 2015 8:53 PM
  • Looks okay to me.

    Only thing I can think of offhand is to ensure the cmdset guid and command id is defined the same in our C# code.

    Meaning guidMVP_WebForms_ViewCmdSet  =     a8a2e358-61a6-4f67-a986-eeab677e9cfd

    and cmdidMyCommand = 0x0100

    The only other thing that comes to mind is that perhaps this is being handled/ignored by the project's IOleCommandTarget or IVsUIHierarchy implementation of Exec or ExecCommand.

    Normally, I'd recommend adding your menu handlers there anyway, as this appears to be a command specific to your project type.

    Sincerely,


    Ed Dore

    Monday, June 29, 2015 9:38 PM