I think the Visio application environment is pretty impressive because of all the things you can do to customize the user interface. There are not very many apps out there that will let you add your own custom windows. Visio allows developers to do just that by enabling you to add Windows Forms as anchor bar windows. The Custom Properties window and the Pan & Zoom window are examples anchor bar windows. They can be docked, pinned, and resized on top of the Visio drawing surface.
I hope by now you have downloaded the Visio 2003 SDK. It has so many code samples for VB6, VB.NET and C#. There is even a sample that shows you how to create an anchor bar window. It also shows you how to drag and drop items from a list view control on to the Visio drawing surface as a shape. For those of you who are new to the Visio 2003 SDK, or Visio development in general, I would like to show you how to use the Code Librarian samples included with the Visio 2003 SDK.
The code samples in the Code Librarian are intended to be used in a variety of development scenarios. You can use them when creating a Visio add-in or add-on, when using the Visio drawing control, or even in standard Windows Applications that just launch and automate the Visio application environment. The Anchor Bar Usage sample would most commonly be used in a Visio add-in scenario, but for illustrative purposes, we will create a standard Windows Application:
- First, launch Microsoft Visual Studio .NET 2003.
- Create a New Project.
- In the New Project dialog, select Visual Basic Projects. Then, select the Windows Application template.
- Name the project Anchor Bar Usage, or whatever you want. Click OK.
Next, you will need to add a reference to the Visio Primary Interoperability Assembly (“PIA” for short). The Visio PIA gives you access to the COM-based Visio Object Model. To add a reference to the Visio 11 PIA:
- From your VB.NET project, click the Project menu, and then click Add Reference.
- In the Add Reference dialog, click the COM tab.
- Scroll down the list and select Microsoft Visio 11.0 Type Library. Click the Select button, then click OK
Then you need to remove the default root namespace that is assigned when creating Visual Basic .NET projects. Note: You don’t have to do this in C# projects.
- In Visual Studio .NET, click the Project menu, and then click Anchor Bar Usage Properties.
- Under the General properties, remove the contents of the Root namespace text box. Click OK.
Now let’s get some code from the Code Librarian. After installing the Visio 2003 SDK, you should have a shortcut titled Microsoft Office Visio Code Librarian Viewer under the Microsoft Office Visio 2003 SDK Start Menu folder. Go ahead and start up the Code Librarian.
The Code Librarian has a tree in the top left-hand corner that allows you to navigate the different samples. Alternatively, you can use the search button on the toolbar. Find the Anchor Bar Usage sample for Microsoft Visual Basic .NET under User Interface.
The Anchor Bar Usage sample consists of two parts: a form and a module. Let’s add the module first because it’s more straightforward:
- In Visual Studio .NET, click the Project menu, and then click Add New Item.
- Select the Module template.
- Name it AnchorBarsUsage.vb. (Note: You can find a suggested name in the comments towards the top of the sample in Code Librarian.) Click OK.
- In Code Librarian, make sure the Anchor Bars Usage module is selected in the tree.
- Copy the code from the code pane to the clipboard.
- Replace the contents of AnchorBarsUsage.vb with the code in the clipboard. Save.
Now adding the form is a bit tricky because a form as a resource file associated with it. The resource file is not in the Code Librarian, but rather in a folder where the Code Librarian database is installed. Here’s how you add the sample Anchor Bar form.
- In Visual Studio .NET, click the Project menu, and then click Add New Item.
- Select the Windows Form template.
- Name it AnchorBarForm.vb. Click OK.
- In Code Librarian, select the Anchor Bars Form form sample is in the tree.
- Copy the code from the code pane to the clipboard.
- Replace the contents of AnchorBarForm.vb with the code in the clipboard. Save.
- Open Windows Explorer.
- Find the Code Librarian folder. If you selected the default options during setup, it should be under \Program Files\Microsoft Office\Visio11\SDK\Samples\Code Librarian\.
- Copy the file AnchorBarForm.resx.
- Paste it to the Anchor Bars Usage VB.NET solution folder. There should be one there already from step #6, but replace it with the copy from steps #8 and #9.
Now, we are ready to use the sample. When we first created our Windows Application project, a default Windows Form was added. It should still be named Form1.vb. Let’s add a button to it to call our code sample:
- In Visual Studio .NET, open Form1.vb in design mode.
- Add a Button control to the form.
- Double click on the Button1 to add a click event handler.
- We need to use some namespaces. Add the following statements to the top of the code file:
Imports Visio = Microsoft.Office.Interop.Visio
Imports Microsoft.Samples.Visio.VBNet
- Then, add the following code to the Button1_Click procedure to demo the sample code:
Dim visioApp = New Visio.Application
DemoAnchorBar(visioApp)
Finally, we are ready to run our sample project. Go ahead and compile and run the project (F5). The Windows Form with the button control should display. When you click on this button, Visio should start. When it does, an anchor bar window should be collapsed towards the bottom of Visio. Hover over it to make it expand. It is populated with a list of shape names. Try dragging-and-dropping one of these items to the page. Voila!
This is a fairly complex sample and you should definitely step through the DemoAnchorBar function to learn how you add an anchor bar window to Visio. You should also step through the code when a drag occurs from the list view control.
We had to jump over some hurdles to set up the project. This sample in particular had some extra steps because it required a resource file. However, I hope you understand how you can take code straight from the Code Librarian and use it in your Visio development projects.
-Chris