如何通过C#在Visio 2010中添加容器?

时间:2013-04-16 10:26:46

标签: c# .net visio

我有一个简单的代码来绘制形状。

Visio.Shape dropShape = ActivePage.Drop(GetMaster(@"Circle"), x, y);
Visio.Shape dropShape2 = ActivePage.Drop(GetMaster(@"Circle"), x + 3, y);

我想为这种形状添加容器

VisApp.ActiveWindow.Select(dropShape, (short)Visio.VisSelectArgs.visSelect);
VisApp.ActiveWindow.Select(dropShape2, (short)Visio.VisSelectArgs.visSelect);

object vsoSelection = VisApp.ActiveWindow.Selection;
Visio.Shape containerMaster = ActivePage.Drop(GetMaster(@"Square"), x -3, y-3);
Visio.Shape container = ActivePage.DropContainer(containerMaster, vsoSelection);

我在互联网上找到了这个样本,但它没有用。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的代码中的问题是您无法在DropContainer()调用中使用任何随机Master作为容器。

相反,您必须加载包含容器的模板,然后在该模板中选择一个容器主数据。

完整的代码如下:

var app = new IVisio.ApplicationClass();

var docs = app.Documents;
var doc = docs.Add("");
var page = app.ActivePage;
short flags = (short)
    ( IVisio.VisOpenSaveArgs.visOpenDocked | 
    IVisio.VisOpenSaveArgs.visOpenRO);

var basic_stencil = docs.OpenEx("basic_u.vss", flags);
var basic_masters = basic_stencil.Masters;
var rounded_rect_master = basic_masters["Rounded Rectangle"];

double x = 2.0;
double y = 3.0;

var shape1 = page.Drop(rounded_rect_master, x, y);
var shape2 = page.Drop(rounded_rect_master, x + 3.0, y+1.0);

var stenciltype = IVisio.VisBuiltInStencilTypes.visBuiltInStencilContainers;

var measurementsys = IVisio.VisMeasurementSystem.visMSDefault;
short containeropenflags = (short) IVisio.VisOpenSaveArgs.visOpenHidden;
string containerstencil_filename = app.GetBuiltInStencilFile(stenciltype, measurementsys);
var container_stencil = docs.OpenEx(containerstencil_filename, containeropenflags);
var containermasters = container_stencil.Masters;
var container = containermasters["Container 1"];

var activewindow = app.ActiveWindow;
short selectargs = (short)IVisio.VisSelectArgs.visSelect;
activewindow.Select(shape1, selectargs);
activewindow.Select(shape2, selectargs);

page.DropContainer(container, activewindow.Selection);