tool windows are first class citizens in
visual studio and of course you know
solution explorer error list the output
window and a whole lot of other tool
windows already the good news is that a
tool window is just wpf and saml so you
can use your existing skills in those
technologies and everything you can
express in saml you can put in a tool
window
now there's two things we need when
we're
adding a tool window the first is that
we need to add a command usually to the
view top level menu under other windows
so that users have a place that they can
go and
open our tool window
and second
we need the tool window itself
so let's look at those two things and
add them to our extension
we're going to start by adding the
command to the command table file
and the first thing we do is that we
create a new id simple and we give it a
descriptive name now this is the command
for opening the tool window so we call
it open guide window and give it a
unique id
we're just gonna copy the previous
button
and update some of the metadata such as
the id to match the newly created id
symbol
we're going to keep the same icon here
that we used for our previous command
and we're going to call the command for
insert guide window
now i want this in the view menu so i'm
going to update the canonical name to
reflect that
currently the parent of our
button is the previous
menu group which is defined right here
and it's located in the edit top level
menu
but i want to put this in with the other
tool windows under view and other
windows
right in here
to do that we come down to our button
and we change the good of the parent
element
to be via's main menu
and then
we can specify and see in the
intellisense list
the names of the different locations i
can put my
command
i'm just going to search for other
windows
and insert that right here
now that we have declared our command
it's time to create the command handler
i'm just going to copy and paste an
existing command handler i already have
and give it a name that matches
the id that i declared in the xml file
here
i also have to remember to rename the
class name itself of course and i can
just delete the
code that was applied to the command i
copied
and then i have to update the id to
match the one in the xml file
and that's it
i now have my command declared and i can
move on to the next step which is to
create
the tool window itself
we're going to right click the project
and say add new item
then navigate to the extensibility
category and choose async tool window
i'm going to call it good window
that's going to create a
folder called tool windows and put my
good window class in here as well as a
saml
file
i'm going to update some of the
properties in my good window here give
it a good title
and it points to a pane which is a tool
window pane defined down here where i
can put additional metadata such as the
image i'm going to use the same image as
i have in my commands
it returns the good window control which
is the saml file that i have right here
the template gives me a very simple saml
file with just a big button on it
go into the code behind i can see what's
happening in the button click event
handler
i want to execute the existing my
command command
to insert a guide into the text editor
so i'm going to use the api
to execute
my command
i'm going to call fire and forget on the
task to let it safely execute in the
background
all that is left now is to register the
tool window on the package class
i do that by adding an attribute
called provide tool window and in it
specify the type of my newly created
good windows pane type after doing that
i need to register tool windows from the
initialize async method in my class
this is very simple i simply call
register tool windows
and that's it
we have to do this to let visual studio
know about our tool window
all that is left to do now is to hook up
our tool window to our command so when
our command is being executed we want
the tool window to open so i can simply
call
goodwindow.show async and that's all we
need to add a tool window to our
extension now let's build and run our
extension in the experimental instance
of visual studio
let's start by opening a code file to
insert a guide into
and then check in the view menu to see
if our command shows up under other
windows
and it does it's right here
clicking that opens the tool window and
i can now make my selection in the
editor and clicking the button to insert
the good
so as you can see tool windows are
pretty easy to do they're very powerful
they're flexible and they are full of
nuances just waiting for you to explore
Wednesday, February 4, 2026
Creating tool windows
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment