Visio在VB6中的开发有很多现成的项目及代码,但在.NET领域相对比较滞后,但是从Visio SDK2007发布以来,给予Visio的C#开发逐渐多了起来,虽然和VB6的开发思路有一定的相似,不过平台不一样,做出来的东西差别还是有一定的距离,虽然总体慢一点,但是,DotNET框架的逐渐强大以及更多的DotNet程序和开发人员的加入,带给大家更高的整合价值以及更美好的发展未来。
本人将会在C#领域将Visio的二次开发进行下去,希望大家一起学习研究,碰撞出更美的火花
Visio Drawing Control控件使用
Visio开发必备
Visio SDK 2007
VisSDK.chm
Visio Code Samples Library.chm
Visio文档操作
查看ShapeSheet属性及帮助内容
宏的录制
Visio的几个重要对象
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
Application
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
Window (Application.ActiveWindow)
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
Document (Application.ActiveDocument)
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
Master、Shape
![VisioObjects.jpg](https://ucc.alicdn.com/z6emli64hecj4/developer-article327355/20241017/c6c5a957b753492ea2cab11ea88d98de.jpeg?x-oss-process=image/resize,w_1400/format,webp)
Visio XML格式文件分析
Master格式
![Visio_Master.jpg](https://ucc.alicdn.com/z6emli64hecj4/developer-article327355/20241017/95145ff6907c4dd7a481e6c00849970f.jpeg?x-oss-process=image/resize,w_1400/format,webp)
Pages/Shapes格式
![Visio_Page.jpg](https://ucc.alicdn.com/z6emli64hecj4/developer-article327355/20241017/c829942f2ae246d0bbbdb0829c267321.jpeg?x-oss-process=image/resize,w_1400/format,webp)
图纸的XML文档中,Master后面的Shapes集合中只有一个Shape对象
图纸的Shapes集合有多个对象,每个对象的NameU和Name值可能不一样,一般使用NameU
Visio基本操作的实现
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication =
this.ctrlDrawing.Document.Application;
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisWindow = VisApplication.ActiveWindow;
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisDocument = VisApplication.ActiveDocument;
//
Settings
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.Settings.ShowShapeSearchPane =
false;
//
显示搜索形状窗体
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.Settings.EnableAutoConnect =
false;
//
自动链接(2007默认)
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.Settings.StencilBackgroundColor = 10070188;
//
vbGrayText
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
//
文档各种显示
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.visCmdViewRulers);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.visCmdViewGrid);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.ShowGuides);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.ShowConnectPoints);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.ShowPageBreaks);
//
各种窗口
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.visCmdShapesWindow);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.visCmdPanZoom);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.visCmdCustProp);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
VisApplication.DoCmd((
short)VisUICmds.visCmdSizePos);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
SendKeys.Send("^(x)");
//
VisApplication.DoCmd((short)VisUICmds.visCmdUFEditCut);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
SendKeys.Send("{DELETE}");
//
VisApplication.DoCmd((short)VisUICmds.visCmdUFEditClear);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
SendKeys.Send("^+(p)");
//
VisApplication.DoCmd(VisUICmds.visCmdFormatPainter);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
SendKeys.Send("^(z)");
//
VisApplication.DoCmd(VisUICmds.visCmdEditUndo);
调用工具条对象、菜单对象的方法
Application.CommandBars
Microsoft.Office.Core.CommandBars共享Office对象模型
使用CommandBar代替UIObject
CommandBar对象中,菜单及工具条是同一个东西
CommandBar、CommandBarButton、 CommandBarComboBox、CommandBarControl、 和 CommandBarPopup
示例:执行视图中的工具条的所有按钮事件。
Visio Shape的属性操作
StringToFormulaForString、FormulaStringToString函数
访问属性
设置属性
添加属性
//
列出模具组
this.cmbStencilGroup.Items.Clear();
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
List<
string> stencilGroups = stencil.GetStencils();
foreach (
string group
in stencilGroups)
{
this.cmbStencilGroup.Items.Add(group);
}
//
根据模具组列出模具
string stencilName = stencil.GetStencilsFileName(
this.cmbStencilGroup.Text);
this.cmbStencil.Items.Clear();
string tempName;
foreach (Master master
in visApp.Documents[stencilName].Masters)
{
tempName = master.Name;
if (!stencil.IsExcludeItem(tempName))
{
this.cmbStencil.Items.Add(tempName);
}
}
//
根据模具,获取对应的属性集合,遍历属性集合,列出属性名称
string stencilName = stencil.GetStencilsFileName(
this.cmbStencilGroup.Text);
string masterName =
this.cmbStencil.Text;
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
Visio.Shape shape = visApp.Documents[stencilName].Masters[masterName].Shapes[1];
if (shape !=
null)
{
List<StencilPropertyInfo> propInfos = property.GetPropertyCollection(shape);
foreach (StencilPropertyInfo info in propInfos)
{
this.cmbProperty.Items.Add(info.Name);
}
}
//
根据模具、模具属性,列出对应的属性信息
string stencilName = stencil.GetStencilsFileName(
this.cmbStencilGroup.Text);
string masterName =
this.cmbStencil.Text;
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
Visio.Shape shape = visApp.Documents[stencilName].Masters[masterName].Shapes[1];
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/d7b8e25c83db4ae38cbb4ccf1facf34f.gif)
StencilPropertyInfo info = property.GetProperty(shape,
this.cmbProperty.Text);
if (info !=
null)
{
this.txtName.Text = info.Name;//属性名称
this.txtValue.Text = info.Value;//属性值
this.txtFormat.Text = info.Format;//属性格式
this.txtSortKey.Text = info.Sort;//属性的排序
this.txtPrompt.Text = info.Prompt;//属性的提示信息
}
//
根据模具,获取属性对象集合
public List<StencilPropertyInfo> GetPropertyCollection(Visio.Shape shape)
{
List<StencilPropertyInfo> list = new List<StencilPropertyInfo>();
StencilPropertyInfo propertyInfo;
Visio.Cell shapeCell;
short shortSectionProp = (short)VisSectionIndices.visSectionProp;
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
if (shape != null)
{
for (short i = 0; i < shape.get_RowCount(shortSectionProp) - 1; i++ )
{
if (shape.get_CellsSRCExists(shortSectionProp, i, (short)VisCellIndices.visCustPropsLabel, 0) != 0)
{
propertyInfo = new StencilPropertyInfo();
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsLabel);
propertyInfo.Name = VisioUtility.FormulaStringToString(shapeCell.RowNameU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsPrompt);
propertyInfo.Prompt = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsFormat);
propertyInfo.Format = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsValue);
propertyInfo.Value = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsSortKey);
propertyInfo.Sort = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
//shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsType);
//propertyInfo.PropType = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
//shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsInvis);
//propertyInfo.InVisible = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
//![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327289/20241017/fa99a9fc687a46dfbcb1aabc26641913.gif)
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327289/20241017/fa99a9fc687a46dfbcb1aabc26641913.gif)
..
list.Add(propertyInfo);
}
}
}
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
return list;
}
//
根据模具和属性名称,获取属性对象信息
public StencilPropertyInfo GetProperty(Visio.Shape shape,
string propertyName)
{
List<StencilPropertyInfo> list = GetPropertyCollection(shape);
StencilPropertyInfo propertyInfo = null;
foreach(StencilPropertyInfo tempInfo in list)
{
if (tempInfo.Name == propertyName)
{
propertyInfo = tempInfo;
break;
}
}
![](https://ucc.alicdn.com/z6emli64hecj4/developer-article327317/20241017/03cf4d2d5b4342f0acc6561adf2ff78b.gif)
return propertyInfo;
}