Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a tool button dynamically with Visual Studio extensions

I'm building a Visual Studio extension, which should add my tool button on a ToolBar or ToolStrip.

There are 2 cases:

  • first case, add my red button to the toolbar/toolstrip which was added by another extension (Visual Micro), see image 1.

  • second case, add my red button to the Properties toolbar/toolstrip of the Visual Studio UI, see image 2.

Image 1:

enter image description here

Image 2:

enter image description here

I tried to implement the second case, but without any positive results.

Here is the code:

EventHandler btnClick = new EventHandler(delegate (Object o, EventArgs a)
            {
                //snip
            });
System.Drawing.Image img = System.Drawing.Image.FromFile("W:\\...\\red_btn.png");

ToolStripButton btn = new ToolStripButton("My Button", img, btnClick, "RedButton");
            btn.Width = 32;
            btn.Height = 32;
            btn.Visible = true;
IntPtr hProperties = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "WindowsForms10.Window.8.app.0.c940ee_r43_ad1", null) ;
ToolStrip toolStrip = (ToolStrip)ToolStrip.FromHandle(hProperties);

if (toolStrip != null)
{
  toolStrip.Items.Add(btn);
  toolStrip.Refresh();
  toolStrip.Visible = true;
}

When I execute the above code from my ToolWindow1Control init() method, nothing happens. What I tried was to find the handle of toolbar from Properties window and add my button to it. But that is not working.

I'm expecting to add the red button to the Properties window's toolbar. This button should execute some code related to the source file which is currently viewed. And this is the second case.

For the first case I don't have any idea how to find the handle of that toolbar to add my button.

Please help.

like image 525
Fed Avatar asked Dec 04 '21 11:12

Fed


People also ask

How do I add a code to the toolbar in Visual Studio?

Go to VSCode settings ( CTRL+, or CMD+, ) and search for shortcut menu bar . Toggle buttons from there.

How do I add a Start button in Visual Studio?

Simply activate the "Build" toolbar in View > Toolbars. This adds the Start Without Debugging, Build Project, and Build Solution buttons to the Toolbar.

How do I make the toolbar appear in Visual Studio?

Right-click the Visual Studio menu bar to get the list of toolbars. Select Test Toolbar. You should now see your toolbar as an icon to the right of the Find in Files icon.

How do I create a tool window in Visual Studio?

When the project opens, add a tool window item template named MyWindow. In the Solution Explorer, right-click the project node and select Add > New Item. In the Add New Item dialog, go to Visual C# > Extensibility and select Custom Tool Window. In the Name field at the bottom of the window, change the tool window file name to MyWindow.cs.

How do I extend the myWindow tool window in Visual Studio?

In the experimental instance, go to View > Other Windows. You should see a menu item for MyWindow. Click it. You should see a tool window with the title MyWindow and a button saying Click Me!. Learn about extending and customizing tool windows that Visual Studio provides, including the Properties window, the Output window, and the Task List window.

How do I add a dynamic menu item in Visual Studio?

Dynamically add menu items. You can add menu items at run time by specifying the DynamicItemStart command flag on a placeholder button definition in the Visual Studio command-table (.vsct) file, then defining (in code) the number of menu items to display and handling the command(s).

How do I create a dynamic menu on a toolbar?

For more information, see Create an extension with a menu command. To create a menu controller with dynamic menu items on a toolbar, you specify the following elements: Two command groups, one that contains the menu controller and another that contains the menu items in the dropdown


Video Answer


1 Answers

There aren't any toolbar or toolstrip HWNDs in WPF windows. What you are trying to do is not possible. If you need to add any visuals to Visual Studio's GUI, use the public API. This isn't just better, it's the only way to do this.

like image 69
Aziz Avatar answered Nov 14 '22 05:11

Aziz