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:
Image 2:
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.
Go to VSCode settings ( CTRL+, or CMD+, ) and search for shortcut menu bar . Toggle buttons from there.
Simply activate the "Build" toolbar in View > Toolbars. This adds the Start Without Debugging, Build Project, and Build Solution buttons to the Toolbar.
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.
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.
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.
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).
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With