Introducing the Microsoft Visio 2010 Beta SDK
Visio Insights blog about the first beta release of the 2010 SDK.
http://blogs.msdn.com/visio/archive/2009/11/19/introducing-the-microsoft-visio-2010-beta-sdk.aspx
Visio 2007: Software Development Kit
The Microsoft Office Visio 2007 Software Development Kit (SDK) contains the latest documentation, samples, header files, libraries and tools that you need to develop custom solutions for Microsoft Office Visio 2007.
http://www.microsoft.com/downloads/details.aspx?familyid=373d34b8-5ef7-4e6e-a582-c8d6b5ee4e33&displaylang=en
Microsoft Office Visio 2007 SDK Release Notes
http://msdn2.microsoft.com/en-us/library/ms409266.aspx
Visio 2003 Software Development Kit (SDK)
The Visio 2003 SDK contains sample applications, code librarian code snippets, documentation, and tools, including the ShapeStudio, Event Monitor, Persistent Events, Print ShapeSheet, and Solution Publishing tools.
http://www.microsoft.com/downloads/details.aspx?familyid=557120bd-b0bb-46e7-936a-b8539898d44d&displaylang=en
Visio 2002 Technical Articles
Technical articles from the Visio 2002 SDK. Many of the technical articles are still relevant, but they aren’t repeated in later versions of the SDK.
http://msdn2.microsoft.com/en-us/library/aa167946(office.11).aspx
Visio Developer Portal
http://msdn2.microsoft.com/en-us/office/aa905478.aspx
How to automate Visio with Visual Basic .NET
This article demonstrates how to automate Visio using Visual Basic .NET. It does not use the add-in wizard for Visual Studio.
http://support.microsoft.com/kb/305199
Review: SDK Questions -
What kind of add-ins and add-ons can be created for Visio with the add-in wizard in the SDK.
-
What versions of Visio do projects created with this wizard work with?
-
What are some reasons for using the event monitor?
-
What are persistent events?
-
What is the advantage of using Publish Components over using Visio’s File Paths to detect file locations?
Answers -
Depending on the programming language you choose:
-
VSL add-ons (can only be created with C++)
-
Executable add-ons (EXEs)
-
COM add-ins (DLLs)
-
Only Visio 2003 and later.
-
Discover what events fire when certain actions occur in Visio.
-
These are actions defined to be kicked off when an event occurs. They are saved with the drawing and do not need to be reestablished each time the drawing is opened – thus they “persist”.
-
It offers tighter integration with the Visio application, and better performance on add-on discovery.
Visio Drawing Control
The Microsoft® Visio® Drawing Control offers the full functionality of the Visio application through the rich Visio object model, as an embeddable component. You can drive the Visio drawing control programmatically by events or by code in your hosting application. Alternatively, the Visio drawing control can provide a diagramming environment for application users within the context of your own application's user interface (UI).
Module Objectives
After completing this module you will be able to:
-
Describe how that drawing control can be used to provide a graphics rich application without using the Visio user interface
-
Identify the features of the Visio drawing control and example use scenarios.
-
Describe best practices for creating new applications that use the drawing control.
-
Embed the Visio drawing control in a variety of host applications.
-
Use the properties of the Visio drawing control API to manage Visio from your host application.
-
Deploy the Visio drawing control with your application.
Lesson 1: Concepts
The Visio Drawing Control is a Microsoft ActiveX control that provides full access to the Visio object model (API) and user interface so that you can integrate the Visio user interface, customize its appearance, and automate Visio in your applications. The Visio Drawing Control is provided with all version of Visio and is installed when you install Visio.
The Visio Drawing Control allows the developer to provide Visio functionality within the context of another application. This level of integration allows the developer full control over the Visio user interface integration with the host application. The new functionality provides more power than just simply embedding a Visio drawing into an OLE container document, like Word. Using a Visio drawing as an OLE object allows you to view the diagram in the container application, link the OLE object to the actual Visio document to reflect changes, and edit the Visio drawing by activating the Visio application from within the container document.
In the case of in-place OLE activation, you are still working within the Visio user interface. You cannot create your own UI. You are also limited to application hosts that implement an OLE container, which rules out technologies such as .NET Windows Forms.
The Visio drawing control, however, provides functionality to address these scenarios, and allows you to develop new Visio solutions that go beyond creating add-ons and add-ins for Visio. With the drawing control you can create your own application with its own interface and be in control of how much of the Visio user interface is presented to the user.
Lesson Objectives
After completing this lesson you will be able to:
-
Describe how that drawing control can be used to provide a graphics rich application without using the Visio user interface
-
Describe the requirements of deploying an application containing the drawing control
-
Describe the types of container applications that can host the drawing control
44.Requirements / Containers
The Visio drawing component is essentially an ActiveX® wrapper for the main Visio library. Like any ActiveX component, it can be embedded in custom host applications developed using Visual Studio .NET 2003 and later, Office 2003 and later containers (such as Microsoft Word), Microsoft Internet Explorer 5.5 and greater, and other ActiveX component containers. Although the component uses the ActiveX architecture, it is more of an embeddable application component than the typical ActiveX control found on a Web site. The Visio component provides the full functionality of the Visio application rather than a small subset of features.
Because it essentially repackages the Visio application engine in a component, the Visio control is installed with the Visio application setup program and requires the Visio client application to initialize. If the user does not have the Visio client application installed on the machine, the control will fail to initialize in the host container.
Host container applications that support the Visio Drawing Control
You can embed the Visio Drawing Control in Microsoft Visual Basic 6.0, Microsoft Visual C++ 6.0, Microsoft Visual Studio 7.1 and higher, and other ActiveX control containers. However, you cannot embed the Visio Drawing Control in another Visio drawing, another ActiveX control, a Microsoft Visual Basic for Applications (VBA) form in Visio, or a Visio solution window.
Security considerations
The Visio Drawing Control is targeted for use in an intranet environment. The Visio Drawing Control does not support the IObjectSafety interface. Before it is run in any version of Internet Explorer, the control warns the user that it is an unsafe file (unless the end user's browser security level is set to Medium, Medium-Low, or Low).
Run-time requirements and distribution of your application
Microsoft Visio must be installed on any computer where the Visio Drawing Control will be used. When you distribute your application, all users must have licensed copies of a version of Visio that supports the Visio Drawing Control on their computers to use it.
Note:
|
To install the Visio Drawing Control, install Visio. The Minimal Install option of Visio will install the Visio Drawing Control.
|
45.Demo: Drawing Control
The Office Plan Sample Application included with the Visio SDK shows how you can use the Drawing Control to create an application that takes advantage of the drawing features of Visio in a Microsoft Windows Form. The sample application can be used to select furniture from an inventory list and plan the space for a conference room or office.
The sample creates a simple office floor plan that permits the end-user of the application, presumably a corporate administrative assistant, to do the following:
-
Click any of three furniture shapes to add them to the drawing.
-
View dynamically updated information about the furniture that has been added.
-
View dynamically updated information about the total retail and wholesale costs of the current contents of the office plan.
-
Save the results to a Visio drawing file and then re-open the file.
The code for this sample application is written in the C# programming language, and is designed to work with Microsoft Visual Studio 2005 or Microsoft Visual Studio 2008.
Demo of Office Plan sample from the SDK.
The code for this sample is included in the Visio 2010 SDK.
This sample application shows how to do the following:
-
Embed the Drawing Control in a Windows Form.
-
Use the Drawing Control interactively
-
Respond to various Visio events fired by user actions
-
Use mouse events to capture the right-click actions on a drawing shape
-
Use the MarkerEvent event and the EventDblClick ShapeSheet cell to respond to when the user double-clicks shapes in the drawing.
-
Create a custom Windows form to display shape product information in an adjacent DataGrid control
-
Use events to keep track of the shapes within the drawing and to calculate total price information for these shapes
-
Save Visio drawings created by using the Drawing Control
-
Open Visio drawings in the Drawing Control
To build the sample code in the Office Plan Sample in Visual Studio 2005 -
Double-click OfficePlanSample.sln in the following folder:
C:\Program Files\Microsoft Office\Office14\VisSDK\Samples\Office Plan\CSharp\Visual Studio 80.
Note:
|
The folder path may differ depending on where the SDK was installed.
| -
In Visual Studio 2005, click Debug > Start Without debugging.
The application is started. The application image is shown below.
Add shapes to drawing -
Click on the furniture buttons on the form. A shape is added for each click. The drop location on the page is adjusted so that the shapes do not lie in exactly the same place.
-
As each shape is added the Total Price figures on the form are updated and the Product data grid is updated. It maintains an accurate total of all of the shapes in the drawing.
Keyboard shortcuts are available
Many Visio keyboard shortcuts are supported in the drawing control.
-
Multi-select shapes and enter the keyboard shortcut Ctrl-G. The shapes are grouped together.
-
Zoom in/Zoom out using Ctrl-Shift left mouse/right mouse.
-
Zoom to whole page with Ctrl-W.
Delete shapes -
Delete an individual shape from the drawing.
-
Delete a grouped shape from the drawing. In both cases the Total Price and Products grid are updated.
Save the drawing -
Choose File > Save… and enter a file name in the Save Visio Document dialog.
-
Choose File > New. A new blank drawing is created.
-
Choose File > Open and open the file saved earlier.
Try this! Application Architecture Review
Now let’s take a look at how this application was architected. The following sections describe the structure of the Office Plan code sample. Go through each section and view the code and shapes to see how it works.
Embed the Drawing Control in a Windows Form.
When the application is started the frmOfficePlanSample form is displayed.
-
In Visual Studio in the Solution Explorer window, right-click frmOfficePlanSample.cs and select View Designer. Right-click frmOfficePlanSample.cs again and choose View Code. Both the code and design view for the form are displayed.
-
Look for the Main procedure in the code view of the form. This is the main entry point for the application. It creates a new instance of the form.
-
Just above the Main procedure is the following code:
public OfficePlanSampleForm() {
// Initializing the components of the form is required for
// Windows Form Designer support
InitializeComponent();
}
This code is executed when the form is created. The InitializeComponent procedure is code that is generated by the forms designer. It contains all form variable initialization, control initializations, and menu definitions.
-
View the design view of the form. Click on the drawing control and scan its properties in the Properties window. Note its name.
-
Click on the Events button in the Properties window for the drawing control. Scroll through the list and note there are two event procedures defined for the drawing control: MouseUpEvent and VisibleChanged.
The code for the VisibleChanged event is in onDrawingControlVisibleChanged. This event is triggered initially when the form is created. Within the event procedure is a call to setUpVisioDrawing.
Within setUpVisioDrawing is the code to establish the connection to the Visio application object. It hides all Visio docked windows and then turns off display of rulers, grid, page tabs, scroll bars. It then gets the viewing size of the window rectangle and does some calculations for positioning shapes when they are created. It then initializes an event sink to watch for Visio events with a call to initializeEventSink.
When using the Office Plan application the user is directly interacting with the Office Plan application and indirectly interacting with Visio through the Visio drawing control. Events can be fired and listened to for both the Office Plan application and for Visio.
For example, when the user clicks a button control in the Office Plan application, there is a click event that is triggered. This click event instructs Visio to add the appropriate shape to the page. The action of adding the shape to the page causes Visio’s shape added event to fire. The shape added event is used to keep track of the number of shapes in the drawing and keep the product information up to date.
Why bother listening for Visio’s shape added event when we know that a shape is going to be added when we click the furniture button? If everything works perfectly you might can assume that a shape will be added and go ahead and process it, but in reality something might go wrong. For example, the stencil that contains the shape’s master may not be accessible by Visio and an error condition may be raised. It is best not to add the product information for a new shape until you are sure that the new shape has finished being added to the drawing. That doesn’t happen until the Visio shape added event is fired.
Respond to Visio events fired by user actions
The procedure initializeEventSink sets up for listening for events from Visio. This procedure is in frmOfficePlanSample.cs. The following events are established in the event sink:
-
Shape add: OnAddProductInformation will be called when a shape is added
-
Shape delete: OnRemoveProductInformation will be called when a shape is deleted
-
Marker events: Used to trap double clicks on a shape for those shapes whose double-click event runs the QueueMarkerEvent addon. OnShapeDoubleClick will be called.
The VisEventProc method also listens for Visio’s NoEventsPending which occurs after Visio has finished flushing its events queue. When a shape is added or deleted they are simply added to a queue of shapes that need to be processed to update the product information. When the event NoEventsPending is fired, the added and deleted queues are processed.
During processing of the shapes added and shapes deleted queues the totals for Retail price and Wholesale price are updated on the form and the data grid is updated to reflect additions and deletions from the drawing.
Use events to keep track of the shapes within the drawing and to calculate total price information for these shapes -
In the design view of the form select one of the furniture button controls.
-
In the property window note its name.
-
Scroll through the properties list and find the value in the Text property. This is the master name that is retrieved from the stencil when this button is clicked.
-
Select the event procedures button in the properties window. Scroll through the list and find the click event procedure. The event procedure is onFurnitureButtonClicked.
The onFurnitureButtonClicked method handles the event that is raised when one of the furniture buttons is clicked. The button text field contains the corresponding name of the master shape on the product stencil. The master is retrieved and dropped on the page.
-
When the furniture shape is dropped on the page Visio’s ShapeAdded event is fired. We saw earlier how the event sink was established to listen for this event.
-
In the design view of the form select the drawing control.
-
In the properties window scroll through the event list and find the MouseUpEvent. Note that it is assigned to the procedure onDrawingControlMouseUp.
The onDrawingControlMouseUp checks for different kinds of mouse up actions which can occur when a shape is right-clicked, such as when the user is using keyboard shortcuts to pan and zoom, or when a user is selecting a shape. The main body of the code for onDrawingControlMouseUp is below.
// Check if the event was raised because of a right mouse button click,
// and the user is not zooming (control+shift+right mouse click)
// or selecting shapes (control+right mouse click).
if ((eventData.button == (int)VisKeyButtonFlags.visMouseRight) &&
((eventData.keyButtonState & (int)VisKeyButtonFlags.visKeyControl) ==0 )) {
// Look for a shape on the drawing page at the click location.
clickedShape = Utility.GetClickedShape(drawingControl,
eventData.x, eventData.y);
if (clickedShape != null) {
// Cancel the default processing of this event since
// it will be handled in this method.
eventData.cancelDefault = true;
// Show the shortcut menu at the click location. The
// coordinates need to be converted from the Visio page
// units that are in the event data to Windows pixel
// coordinates.
shapeShortcutMenu.Show(this,
Utility.MapVisioToWindows(drawingControl,
eventData.x, eventData.y));
}
}
Finding the shape is done inside Utility.GetClickedShape. This uses Visio’s SpatialSearch method of the page to determine the closest shape to our mouse click location.
Use the MarkerEvent event and the EventDblClick ShapeSheet cell to respond to when the user double-clicks shapes in the drawing
The detecting of when the user double clicks on a shape is handled with marker events. We saw earlier how the event sink was set up to listen to marker events. Now let’s look at how a marker event gets fired in the first place.
-
In the design view of the form select the drawing control.
-
In the properties window scroll through the event list and note that there is no double click event exposed for the drawing control. When you double-click on a shape within the control, the shape’s double click event fires which evaluates the EventDblClick cell in the shape’s ShapeSheet.
-
The Events section for the Office Chair shape is shown below. Note that the formula calls RUNADDONWARGS and passes the string “QueueMarkerEvent”. This causes the MarkerEvent to fire. In addition the MarkerEvent is passed the string “/officeplan /cmd=1” which is used by the marker event listener to verify that the marker event was caused by double clicking a shape that it can recognize. Also, the RUNADDONWARGS functions appends the string “/doc=id /page=id /shape=sheet.id” to the string passed to the marker event. In our sample application this is used to get a reference to the specific shape that was double-clicked.
-
The code trail starts with VisEventProc. This calls handleMarker, which gets a reference to the shape that was double clicked and then calls onShapeDoubleClick. onShapeDoubleClick displays the product detail form.
Create a custom Windows form to display shape product information in an adjacent DataGrid control
The information displayed in the datagrid is kept in the ProductData.xml file. The master shape has shape data fields defined, but contains no shape data except for the ProductID which is used to identify the corresponding ProductListItem in the xml file.
The following code comes from the onAddProductInformation procedure which is called when a Visio shape added event is fired.
// Update the total prices and the data grid
// add the information from the new shape.
TotalRetailPrice += productData.GetRetailPrice(productId);
TotalWholesalePrice +=
productData.GetWholesalePrice(productId);
productData.AddProduct(productId);
Setting TotalRetailPrice updates the retail price on the form.
Setting TotalWholesalePrice updates the wholesale price on the form.
The variable productId contains the value in the shape data cell ProductID from the shape. This is used to associate the product id with the shape. The method productData.AddProduct updates the datagrid with the data for this productID when a shape is added. The method productData.RemoveProduct decrements the product information in the data grid with a shape is deleted.
The actual product data is maintained in the ProductData object after being loaded from the xml file.
The onShapeDoubleClick procedure and the onMenuProductInformationClicked procedure each display the Product Detail form.
Save Visio drawings created by using the Drawing Control
After constructing a Visio drawing using the drawing control the user can save the drawing using the File > Save command. This causes the click event for the menu item onMenuFileSaveClicked to execute. This event procedure calls Utility.SaveDrawing to save the Visio drawing.
Open Visio drawings in the Drawing Control
Visio drawings can also be opened in the drawing control. This is done through the File > Open menu and the onMenuFileOpenClicked event procedure. This sets the drawingControl.Src property to the filename of the Visio drawing.
Share with your friends: |