Visio 加载项入门






4.75/5 (4投票s)
Visio 加载项创建(
目录
引言
Visio 在我们的生活中被广泛使用,并且时常我们需要为其添加一些额外的功能。本文演示了如何创建一个简单的 Visio 插件,包含菜单、工具栏和事件。附带的示例远非完美。它可能会引发一些异常。但其主要目的是展示基本原理。所以,别再说废话了,让我们开始工作吧。
定义
首先,让我们统一术语,以便大家都能在同一页面上。在图片中,您可以找到以下术语:“文档
”、“页面
”、“模具
”、“主控形状
”和“形状
”。

我们还有一个“应用程序
”,它没有在图片中显示。“应用程序
”是代表当前正在运行的 Visio 实例的对象。它是我们将开始工作的核心对象。“应用程序
”对象使我们能够访问 Visio 的主要配置,例如“语言
”、“路径
”等。但它也使我们能够访问已打开文档的集合。
“文档
”对象代表已打开的文件,例如 *.vsd、*.vsx 等。它提供了诸如“FullName
”(文件的完整路径)和“Name
”(仅文件名)以及文档的其他属性。
“模具
”就是扩展名为“.vss”或“.vsx”的已打开文件。每个模具都包含一个“主控形状”对象的集合。
同样,“文档
”对象提供了“页面
”的集合,但“应用程序
”对象提供当前选定的“页面
”。“页面
”对象提供了属于它的“图层
”和“形状
”的集合。
“主控形状
”是一个可以放置在页面上的模板。页面上“主控形状
”的一个实例称为“形状
”。
每个“页面
”都有放置在其上的“形状
”的集合。每个“形状
”都有许多属性,例如位置、尺寸等。
这些对象的连接和一些属性显示在下面的图表中

ShapeSheet 概念
Visio 使用 ShapeSheet 概念来表示“形状
”的属性。这意味着每个“形状
”都有一个属性表。每个表都包含一个单元格集合,其中包含数据。

在这张图片中,我们可以看到形状(这里是“消息调用”箭头)具有“BeginX
”、“BeginY
”、“EndX
”等属性。
所有插件的通用结构
本节描述了 Visio 插件的基本结构。
Visio 插件具有以下入口点:“Load
”(加载) 、“Unload
”(卸载) 和 “Run
”(运行)。
Visio 调用插件的“Load
”以确定插件必须被加载。“Unload
” - 插件必须被卸载。“Run
”当 Visio 中发生插件订阅的某个事件时被调用。
附带的示例将在工具栏上添加两个按钮。

第一个按钮将显示页面上所有形状的全部信息。第二个按钮将添加“SelectionAdded
”(选择添加)事件。事件“SelectionAdded
”意味着每次在页面上添加某个“形状
”时,Visio 都会调用我们 Visio 插件的“Run
”函数(您可以在 Visio SDK 文档中找到有关此事件的更详细信息)。
代码概述
当插件首次启动时,它通过 COM 接口与 Visio 建立连接。
//Attach to the COM interface of Visio
if (!SUCCEEDED(m_app.GetActiveObject(Visio::CLSID_Application)))
{
m_app.CreateInstance(Visio::CLSID_Application);
}
// check if application is reached
if (!m_app.GetInterfacePtr())
{
return VAORC_FAILURE;
}
之后,插件添加了一个带有“Smiley
”(笑脸)和“Pig
”(猪)两个按钮的工具栏?
//////////////////////////////////////////////////////////////////////////
// Get build in ToolBar
//////////////////////////////////////////////////////////////////////////
Visio::IVUIObjectPtr uiToolbarPtr;
HRESULT hr = m_app->get_BuiltInToolbars(0, &uiToolbarPtr);
Visio::IVToolbarSetPtr toolbarSetPtr;
hr = uiToolbarPtr->ToolbarSets->get_ItemAtID(2, &toolbarSetPtr);
//add a new toolbar
Visio::IVToolbarPtr toolbarPtr;
toolbarPtr = toolbarSetPtr->Toolbars->Add();
toolbarPtr->put_Caption(TOOL_BAR_CAPTION);
toolbarPtr->put_Position(6);
Visio::IVToolbarItemsPtr toolbarItemsPtr;
hr = toolbarPtr->get_ToolbarItems(&toolbarItemsPtr);
//Add toolbar item with "Smiley"
Visio::IVToolbarItemPtr toolbarItemPtr;
toolbarItemPtr = toolbarItemsPtr->Add();
toolbarItemPtr->put_Caption(ITEM_SMILEY);
toolbarItemPtr->put_AddOnName(ADDON_NAME); //Add-on Name
// we need to specify ComandLine arguments
// so Add-on can distinguish what button was pressed in ToolBar("Smiley" or "Pig")
toolbarItemPtr->put_AddOnArgs(ITEM_SMILEY);
toolbarItemPtr->put_CntrlType(Visio::visCtrlTypeBUTTON);
toolbarItemPtr->put_FaceID(151); // 151 is a type of button(Smiley)
//Add toolbar item with "Pig"
toolbarItemPtr = toolbarItemsPtr->Add();
toolbarItemPtr->put_Caption(ITEM_PIG);
toolbarItemPtr->put_AddOnName(ADDON_NAME); //Add-on Name
// we need to specify ComandLine arguments
// so Add-on can distinguish what button was pressed in ToolBar("Smiley" or "Pig")
toolbarItemPtr->put_AddOnArgs(ITEM_PIG);
toolbarItemPtr->put_CntrlType(Visio::visCtrlTypeBUTTON);
toolbarItemPtr->put_FaceID(153); // 153 is a type of button(Pig)
m_app->SetCustomToolbars(uiToolbarPtr); //Add the Toolbar to the Application
现在让我们看一下主函数“Run
”。此函数仅检查发生了什么事件并调用相应的事件处理程序。
如果命令行中是“Smiley
”,则调用函数“OnSmileyAction
”来处理按下笑脸按钮的操作。
if (ITEM_SMILEY == (_bstr_t)pV2L->lpCmdLineArgs)
{
// check if in the command line is "Smiley"
OnSmileyAction();
}
如果命令行中是“Pig
”,则调用函数“OnPigAction
”来处理按下猪按钮的操作。
else if (ITEM_PIG == (_bstr_t)pV2L->lpCmdLineArgs)
{
// check if in the command line is "Pig"
OnPigAction();
}
对于 Visio 事件,我们有特殊的 CmdLineArguments
格式(例如,“/doc=1 /page=1 /event=902 /eventid=70”)。插件解析此行并检查“/event”是否为“visEvtCodeSelAdded
”。
else
{
// process other events
CString cmdLine = (LPTSTR)pV2L->lpCmdLineArgs;
// finding in the cmdLine keyword "/event"
// "/event" means that some event fired
int posComand = cmdLine.Find("/event");
if (posComand >= 0)
{
// excluding the ID of event
posComand+=strlen("/event=");
int posComandEnd = cmdLine.Find(" ",posComand);
CString strId = cmdLine.Mid(posComand,posComandEnd-posComand);
int eventId = atoi(strId.GetBuffer(0));
if (visEvtCodeSelAdded == eventId)
{
// check that event is that we wait
OnEventOccurred();
}
else
{
TRACE((LPTSTR)pV2L->lpCmdLineArgs);
TRACE("Not supported event");
}
}
}
在“OnSmileyAction
”中,插件仅输出有关页面上放置的形状的信息。在“OnPigAction
”中,订阅“visEvtCodeSelAdded
”事件(您可以在 Visio SDK 文档中阅读有关此事件的更详细信息),并输出有关所有主控形状的信息。
// Add notification for event when shape is placed on the page
doc->EventList->Add(visEvtCodeSelAdded,visActCodeRunAddon,ADDON_NAME,"");
当事件发生时,插件仅输出有关当前添加到页面的形状的信息。
环境
要构建此解决方案,您需要:VC6+、Visio SDK 2007 和 MS SDK (Platform SDK)