Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / ATL

Create Visio Add-on VSL project with Toolbar, Menu and Command Handler

1.54/5 (6 votes)
29 Feb 2008CPOL2 min read 1   422  
An article on a Visio Add-on VSL project that is focussed on speed

Introduction

VSL add-ons: VSL’s (ViSio Libraries) were first made available with Visio 2.0. In fact, many of the out-of-the-box Visio solutions, such as the Organization Chart, are implemented as a VSL add-on. You create VSL add-ons using C or C++. The emphasis of this Visio solution option is sheer speed. Bear in mind, Visio SDK needs to installed to use this.

Using the Code

Procedure to Create an Application

Create a new project using Microsoft Visual 2005 Editor. Navigate to Other Languages and choose Visual C++. This will display many options with Visio add-in or add-on and select it. Provide the project a name, like "VisioProject1." This pops up the Microsoft Visio Add-in or Add-on Wizard. Click the Next button, choose "Create a Visio VSL add-on, Use C++ without ATL of MFC," Click Next, Choose "Import Visio type library," and Click Next and Finish the Wizard. The Wizard generates sample code. When running the Add-on, Message Box will be displayed with the information that "VisioProject1 add-on generated by Visio 2007 SDK Add-in/Add-on Wizard VisioProject1." Comment out the MessageBox code and add the following source code to implement the customized menu, Toolbar and handling the MenuItem, ToolbarItem functionalities. Build the project. It generates the VisioProject1.vsl binary file in the Debug folder.

C++
//Common
BSTR bstrAddOnName = SysAllocString(L"VisioProject1U"); 
BSTR bstrTitle = SysAllocString(L"&Sample"); 
BSTR bstrItem = SysAllocString(L"Item");

//Menu: Visio::IVUIObjectPtr uiObjPtr; 
Visio::IVMenuSetsPtr menuSetsPtr; 
Visio::IVMenuSetPtr menuSetPtr; 
Visio::IVMenusPtr menusPtr; 
Visio::IVMenuPtr menuPtr; 
Visio::IVMenuItemsPtr menuItemsPtr; 
Visio::IVMenuItemPtr menuItemPtr;

hr = m_app->get_BuiltInMenus(&uiObjPtr); 
hr = uiObjPtr->get_MenuSets(&menuSetsPtr); 
//long itemId = Visio::VisUIObjSets::visUIObjSetDrawing; 
hr = menuSetsPtr->get_ItemAtID(2, &menuSetPtr); 
hr = menuSetPtr->get_Menus(&menusPtr); //add a new menu 
menuPtr = menusPtr->AddAt(7); 
hr = menuPtr->put_Caption(bstrTitle); 
hr = menuPtr->get_MenuItems(&menuItemsPtr); 
//MenuItem1 menuItemPtr = menuItemsPtr->Add(); 
hr = menuItemPtr->put_Caption(bstrItem); 
menuItemPtr->put_AddOnName(bstrAddOnName); 
//Add-on name 
menuItemPtr->put_ActionText(bstrItem); 
menuItemPtr->put_MiniHelp(bstrItem); 
menuItemPtr->put_AddOnArgs(bstrItem); 
m_app->SetCustomMenus(uiObjPtr); //Add the Menu to the Application

//Toolbar

Visio::IVUIObjectPtr uiToolbarPtr; 
Visio::IVToolbarSetPtr toolbarSetPtr; 
Visio::IVToolbarPtr toolbarPtr; 
Visio::IVToolbarItemsPtr toolbarItemsPtr; 
Visio::IVToolbarItemPtr toolbarItemPtr;

// get build in toolbars 
hr = m_app->get_BuiltInToolbars(0, &uiToolbarPtr); 
hr = uiToolbarPtr->ToolbarSets->get_ItemAtID(2, &toolbarSetPtr); //add a new toolbar 
toolbarPtr = toolbarSetPtr->Toolbars->Add(); 
toolbarPtr->put_Caption(bstrTitle); 
//toolbarPtr->put_Position((short)Visio::VisUIBarPosition::visBarMenu); 
toolbarPtr->put_Position(6); 
hr = toolbarPtr->get_ToolbarItems(&toolbarItemsPtr); 
//ToolbarItem1 
toolbarItemPtr = toolbarItemsPtr->Add(); 
toolbarItemPtr->put_Caption(bstrItem); 
toolbarItemPtr->put_AddOnName(bstrAddOnName); 
//Add-on Name toolbarItemPtr->put_ActionText(bstrItem); 
toolbarItemPtr->put_MiniHelp(bstrItem); 
toolbarItemPtr->put_AddOnArgs(bstrItem); 
toolbarItemPtr->put_CntrlType(2); 
//(int)Visio.VisUICtrlTypes.visCtrlTypeBUTTON;
toolbarItemPtr->put_FaceID(270);
m_app->SetCustomToolbars(uiToolbarPtr); //Add the Toolbar to the Application


Command Handling
    //Handling the User click commands from Visio - Command Handler     
        if (pV2L->szCmdLineArgs != NULL){
            if (strcmp(pV2L->szCmdLineArgs, "Item") == 0){
            MessageBoxA(0, pV2L->szCmdLineArgs, "Info", 0);

            }
        }

To Run

Open the Visio Application. Navigate to Tools->Option->Advanced then File Paths. Add the file path of VisioProject1.vsl in the Add-ons path and click OK. Close and reopen the the Visio application. Navigate to Tools->Add-ons. Select the VisioProject1 in the Run Add-On popup window. Click OK to run the Add-on. You can see the customized Toolbar and Menu in the Visio application as I mention in the picture. If you click on the menu or toolbar item then it will display the Message Box with Item name.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)