locked
Package Menus without CTC RRS feed

  • Question

  • Can someone point me at a code sample on how to add in menus and command buttons without use of a CTC file? (and not the XML CTC either Smile )
    Saturday, April 14, 2007 4:07 PM

Answers

  • Hi Will,

     

    As BLoggins pointed out, you can also add menu items by way of addins. Dr. eX just posted a couple of new blog entries that you might also find helpful, when attempting to identify and retrieve the CommandBar you want to add your menus to at:

     

    See my earlier post entitled EnableVSIPLogging and IVsProfferCommands for the details.

     

    Sincerely,

    Tuesday, April 17, 2007 9:59 PM
  • Hi Will,

     

    Correct. The only other thing I'd add is that you can use tie commands (menu items) in the .CTC to a specific CommandUI Context GUID. Which allows for controlling the visibility of the menu item based on the current UIContext. Meaning, you use the contextUI guid to control to control the visibility of your command, without implementing any code. For all the gory details, you'll want to read up on the documentation covering the VISIBILITY_SECTION  of the .CTC resource.

     

    Sincerely,

    Tuesday, April 24, 2007 3:24 AM

All replies

  • Hi.  I'm assuming the functionality provided by DYNAMICITEMSTART isn't flexible enough for you (join the club).  The best way I know of is to use the Automation API.

     

    Grab a reference to the DTE object (and the MainMenu command bar) using:

     

    Code Snippet

    EnvDTE80.DTE2 application = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));

    CommandBar cb = (CommandBar)((CommandBars)application.CommandBars)["MenuBar"];

     

    The commandbar stuff is located in Microsoft.VisualStudio.CommandBars.dll.  From there, reference the MSDN CommandBar for MS Office documentation to work with this API.  A few things to note:

     

    - You want to use "temporary" command bar controls if you want to add and remove menus and menu items dynamically while visual studio is running, rather than simply registering them to appear on the next load

     

    - If you create a CommandBarButton, you can hookup to its Click event to receive click notifications.  However, these aren't truly "actions" so you don't get keyboard shortcut support for example.  Also, you need to keep a reference to the CommandBarControls you create inside your VSPackage, otherwise you'll find the Click events mysteriously stop firing.  This is because, without a reference to them, .NET's garbage collector sweeps 'em up.

     

    A few links:

     

    Command Bar best practices:

    http://msdn2.microsoft.com/en-us/library/aa159884(office.11).aspx

     

    Tempory Command Bars:

    http://www.mztools.com/articles/2005/MZ003.htm

     

    Hope this helps, and sorry for the lousy formatting!

     

     

    Monday, April 16, 2007 1:46 AM
  • Hi Will,

     

    Maybe somebody will be interested what did you mean saying "XML CTC" - so that is the link to Aaron's blog post "CTC is dead...Long Live VSCT! (Part 1)"

    Tuesday, April 17, 2007 9:45 PM
  • Hi Will,

     

    As BLoggins pointed out, you can also add menu items by way of addins. Dr. eX just posted a couple of new blog entries that you might also find helpful, when attempting to identify and retrieve the CommandBar you want to add your menus to at:

     

    See my earlier post entitled EnableVSIPLogging and IVsProfferCommands for the details.

     

    Sincerely,

    Tuesday, April 17, 2007 9:59 PM
  • Very useful. In fact, there are only a very few times that I really need the hotkey bindings, typically when I'm injecting in the context menu. As I currently understand it, the only material feature of CTC or XML/CTC over hooking in through the DTE for adding menus is hotkeys.
    Saturday, April 21, 2007 7:54 PM
  • Hi Will,

     

    Correct. The only other thing I'd add is that you can use tie commands (menu items) in the .CTC to a specific CommandUI Context GUID. Which allows for controlling the visibility of the menu item based on the current UIContext. Meaning, you use the contextUI guid to control to control the visibility of your command, without implementing any code. For all the gory details, you'll want to read up on the documentation covering the VISIBILITY_SECTION  of the .CTC resource.

     

    Sincerely,

    Tuesday, April 24, 2007 3:24 AM