【发布时间】:2011-06-09 19:24:38
【问题描述】:
假设我在 Visio 中打开了一个文档,并且在我的 Visio 加载项代码中有一个指向该文档的指针。是否可以从 stencil 中得出以下信息:
- 形状名称(我的意思是类型,不是唯一名称)
- 图片
据我所知,图表和模具是当前文档的一部分。那么如何从文档指针移动到可用的模板表单呢?
(模板是指左侧的面板,用户可以看到所有可用的形状)
提前致谢。 丹
【问题讨论】:
假设我在 Visio 中打开了一个文档,并且在我的 Visio 加载项代码中有一个指向该文档的指针。是否可以从 stencil 中得出以下信息:
据我所知,图表和模具是当前文档的一部分。那么如何从文档指针移动到可用的模板表单呢?
(模板是指左侧的面板,用户可以看到所有可用的形状)
提前致谢。 丹
【问题讨论】:
您可以在页面上放置的形状的定义称为 Master。将 Shapes 和 Masters 视为类似于 OOP 中的实例化对象和类。 Visio 文档有一个 Masters 集合。您在左窗格中查看的母版可能不在活动文档的母版集合中。左侧的每个窗格都是一个不同的文档,称为模板。当您使用模板创建新图表时,可以打开多个模具。要详细了解 Documents、Stencils、Masters 和 Shapes 之间的关系,请参阅Chapter 3 of Developing Microsoft Visio Solutions。
要访问其中一个打开的模板,请使用应用程序的 Documents 集合。然后,您可以使用 Document 的 Masters 集合访问各个 Masters。 Master 对象有一个Name 和一个Icon 属性。
在 .Net 中使用 Icon 属性存在许多挑战。 Icon 属性是一个 IPictureDisp,您需要找到一种将其转换为可在 .Net 中使用的图像类型的方法。 VB6 库中的IPictureDispToImage 方法是一种方法,但它仅适用于 32 位可执行文件。
如果您在进程外调用,即从外部可执行文件而不是加载项调用,Icon 属性将引发 COM 异常。我从未在 C# 中实际使用过它,所以我不确定 IPictureDisp 属性是否可以在 COM 和 .Net 之间编组。
如果您不能使用 Icon 属性,您仍然可以通过调用 ExportIcon 方法将图标写入文件或剪贴板来获取图标。
以下代码展示了如何获取母版名称以及如何将母版图标导出到文件中。
using Visio = Microsoft.Office.Interop.Visio;
...
// Create a new Basic Flowchart diragram ("_U" specifies the US units version).
Visio.Document docDiagram = app.Documents.Add("BASFLO_U.VST");
// Get a reference to the Basic Flowchart Shapes Stencle which was opened by
// the template above.
Visio.Document docStencle = app.Documents["BasFlo_U.vss"];
// Get the Decision master from the Stencil.
Visio.Master master = docStencle.Masters["Decision"];
// Get the name of the Decision master
string masterName = master.Name;
// Export the Icon from the Decision Master.
// You could use GetTempFileName here.
master.ExportIcon(
@"c:\temp\icom.bmp",
(short) Visio.VisMasterProperties.visIconFormatBMP);
【讨论】:
我明白了:
Visio.Application app = Globals.ThisAddIn.Application;
Visio.Documents docs = app.Documents;
ArrayList masterArray_0 = new ArrayList();
ArrayList masterArray_1 = new ArrayList();
Visio.Document doc_0 = docs[1]; // HERE IS THE MAIN POINT
Visio.Document doc_1 = docs[2]; // HERE IS THE MAIN POINT
Visio.Masters masters_0 = doc_0.Masters;
Visio.Masters masters_1 = doc_1.Masters;
foreach (Visio.Master master in masters_0)
{
masterArray_0.Add(master.NameU); // THIS WILL CONTAIN DIAGRAM FIGURES
}
foreach (Visio.Master master in masters_1)
{
masterArray_1.Add(master.NameU); // THIS WILL CONTAIN STENCIL FIGURES
}
“docs”数组成员编号有一个关键点,它们从 1 开始,而不是像以前用于数组的那样从 0 开始。 谢谢你的帮助。这个问题应该已经结束了。
丹
【讨论】: