Share via


Dragging-and-dropping from Custom Anchor Bar Windows

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:

  1. First, launch Microsoft Visual Studio .NET 2003.
  2. Create a New Project.
  3. In the New Project dialog, select Visual Basic Projects. Then, select the Windows Application template.
  4. 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:

  1. From your VB.NET project, click the Project menu, and then click Add Reference.
  2. In the Add Reference dialog, click the COM tab.
  3. 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.

  1. In Visual Studio .NET, click the Project menu, and then click Anchor Bar Usage Properties.
  2. 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:

  1. In Visual Studio .NET, click the Project menu, and then click Add New Item.
  2. Select the Module template.
  3. 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.
  4. In Code Librarian, make sure the Anchor Bars Usage module is selected in the tree.
  5. Copy the code from the code pane to the clipboard.
  6. 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.

  1. In Visual Studio .NET, click the Project menu, and then click Add New Item.
  2. Select the Windows Form template.
  3. Name it AnchorBarForm.vb. Click OK.
  4. In Code Librarian, select the Anchor Bars Form form sample is in the tree.
  5. Copy the code from the code pane to the clipboard.
  6. Replace the contents of AnchorBarForm.vb with the code in the clipboard. Save.
  7. Open Windows Explorer.
  8. 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\ .
  9. Copy the file AnchorBarForm.resx.
  10. 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:

  1. In Visual Studio .NET, open Form1.vb in design mode.
  2. Add a Button control to the form.
  3. Double click on the Button1 to add a click event handler.
  4. 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

  1. 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

This posting is provide “AS IS” with no warranties, and confers no rights.

Comments