2013-04-16 50 views
1

我有一個簡單的代碼來繪製形狀。如何通過C#在Visio 2010中添加容器?

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

我whant添加容器形狀這

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); 

我在網上查到了這個樣本,但沒有奏效。有什麼想法?

回答

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);