如何使用C#进行Visio二次开发

      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的几个重要对象

None.gif  Application
None.gif Window (Application.ActiveWindow)
None.gif Document (Application.ActiveDocument)
None.gif Master、Shape

VisioObjects.jpg

Visio XML格式文件分析
 Master格式
Visio_Master.jpg
 
Pages/Shapes格式 
Visio_Page.jpg

 图纸的XML文档中,Master后面的Shapes集合中只有一个Shape对象
 图纸的Shapes集合有多个对象,每个对象的NameU和Name值可能不一样,一般使用NameU


Visio基本操作的实现

None.gif VisApplication  =   this .ctrlDrawing.Document.Application;
None.gifVisWindow 
=  VisApplication.ActiveWindow;
None.gifVisDocument 
=  VisApplication.ActiveDocument;
None.gif
None.gif
// Settings
None.gif
VisApplication.Settings.ShowShapeSearchPane  =   false // 显示搜索形状窗体
None.gif
VisApplication.Settings.EnableAutoConnect  =   false // 自动链接(2007默认)
None.gif
VisApplication.Settings.StencilBackgroundColor  =   10070188 // vbGrayText
None.gif
None.gif
// 文档各种显示
None.gif
VisApplication.DoCmd(( short )VisUICmds.visCmdViewRulers);
None.gifVisApplication.DoCmd((
short )VisUICmds.visCmdViewGrid);
None.gifVisApplication.DoCmd((
short )VisUICmds.ShowGuides);
None.gifVisApplication.DoCmd((
short )VisUICmds.ShowConnectPoints);
None.gifVisApplication.DoCmd((
short )VisUICmds.ShowPageBreaks);
None.gif
None.gif
// 各种窗口
None.gif
VisApplication.DoCmd(( short )VisUICmds.visCmdShapesWindow);
None.gifVisApplication.DoCmd((
short )VisUICmds.visCmdPanZoom);
None.gifVisApplication.DoCmd((
short )VisUICmds.visCmdCustProp);
None.gifVisApplication.DoCmd((
short )VisUICmds.visCmdSizePos);
None.gif
None.gif
None.gifSendKeys.Send(
" ^(x) " );
None.gif
// VisApplication.DoCmd((short)VisUICmds.visCmdUFEditCut);
None.gif
SendKeys.Send( " {DELETE} " );
None.gif
// VisApplication.DoCmd((short)VisUICmds.visCmdUFEditClear);
None.gif
SendKeys.Send( " ^+(p) " );
None.gif
// VisApplication.DoCmd(VisUICmds.visCmdFormatPainter);
None.gif
SendKeys.Send( " ^(z) " );
None.gif
// VisApplication.DoCmd(VisUICmds.visCmdEditUndo);

调用工具条对象、菜单对象的方法
Application.CommandBars
Microsoft.Office.Core.CommandBars共享Office对象模型
使用CommandBar代替UIObject
CommandBar对象中,菜单及工具条是同一个东西
CommandBar、CommandBarButton、 CommandBarComboBox、CommandBarControl、 和 CommandBarPopup

示例:执行视图中的工具条的所有按钮事件。
None.gif Microsoft.Office.Core.CommandBars commandBars;
None.gifcommandBars 
=  (Microsoft.Office.Core.CommandBars)VisApplication.CommandBars;
None.gif
None.gif
foreach  (Microsoft.Office.Core.CommandBarControl control  in  commandBars[ " View " ].Controls)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Microsoft.Office.Core.CommandBarButton button 
=  control as  Microsoft.Office.Core.CommandBarButton;
InBlock.gif    
if (button != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        button.Execute();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gifStringBuilder sb 
=   new  StringBuilder();
None.gif
foreach  (Microsoft.Office.Core.CommandBar bar  in  commandBars)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    sb.Append(
string.Format("CommandBar Name:{0}\r\n", bar.Name));
InBlock.gif    
foreach(Microsoft.Office.Core.CommandBarControl control in bar.Controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Microsoft.Office.Core.CommandBarButton button 
=  control as  Microsoft.Office.Core.CommandBarButton;
InBlock.gif        
if(button != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sb.Append(
string.Format("Button Name:{0} \r\n", button.Caption));
ExpandedSubBlockEnd.gif        }
                    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gifForm2 frm 
=   new  Form2();
None.giffrm.txtContent.Text 
=  sb.ToString();
None.giffrm.Show();
None.gif
None.gif
None.gif
short  flags  =  ( short )VisOpenSaveArgs.visOpenDocked  |  ( short )VisOpenSaveArgs.visOpenRO;
None.gifStencilOpenEx(wndVisio.Application, flags);
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 打开模具的公共方法
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="visApp">按引用调用的VisioApplication对象</param>
ExpandedBlockEnd.gif
/// <param name="flags">打开的模式</param>

None.gif private   void  StencilOpenEx(Application visApp,  short  flags)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    List
<string> stencilList = GetStencils();
InBlock.gif    
string stencilFileName;
InBlock.gif    
InBlock.gif    
foreach(string stencil in stencilList)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        stencilFileName 
= GetStencilsFileName(stencil);
InBlock.gif        
if(!string.IsNullOrEmpty(stencilFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            visApp.Documents.OpenEx(Portal.gc.gStencileFileBasePath 
+ stencilFileName, flags);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 关闭模具文件
None.gif
visApp.Documents[ " Switch.vss " ].Close();
None.gifvisApp.Documents[
" Span.vss " ].Close();
None.gifvisApp.Documents[
" Line.vss " ].Close();
None.gifvisApp.Documents[
" Label.vss " ].Close();
None.gifvisApp.Documents[
" Construct.vss " ].Close();
None.gifvisApp.Documents[
" Monitor.vss " ].Close();


Visio Shape的属性操作
  StringToFormulaForString、FormulaStringToString函数
  访问属性
  设置属性
  添加属性

None.gif // 列出模具组
None.gif
this .cmbStencilGroup.Items.Clear();
None.gifList
< string >  stencilGroups  =  stencil.GetStencils();
None.gif
foreach  ( string  group  in  stencilGroups)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this.cmbStencilGroup.Items.Add(group);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// 根据模具组列出模具
None.gif
string  stencilName  =  stencil.GetStencilsFileName( this .cmbStencilGroup.Text);
None.gif
this .cmbStencil.Items.Clear();
None.gif
string  tempName;
None.gif
foreach  (Master master  in  visApp.Documents[stencilName].Masters)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    tempName 
= master.Name;
InBlock.gif    
if (!stencil.IsExcludeItem(tempName))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.cmbStencil.Items.Add(tempName);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// 根据模具,获取对应的属性集合,遍历属性集合,列出属性名称
None.gif
string  stencilName  =  stencil.GetStencilsFileName( this .cmbStencilGroup.Text);
None.gif
string  masterName  =   this .cmbStencil.Text;
None.gifVisio.Shape shape 
=  visApp.Documents[stencilName].Masters[masterName].Shapes[ 1 ];
None.gif
if  (shape  !=   null )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    List
<StencilPropertyInfo> propInfos = property.GetPropertyCollection(shape);
InBlock.gif    
foreach (StencilPropertyInfo info in propInfos)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.cmbProperty.Items.Add(info.Name);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// 根据模具、模具属性,列出对应的属性信息
None.gif
string  stencilName  =  stencil.GetStencilsFileName( this .cmbStencilGroup.Text);
None.gif
string  masterName  =   this .cmbStencil.Text;
None.gifVisio.Shape shape 
=  visApp.Documents[stencilName].Masters[masterName].Shapes[ 1 ];
None.gifStencilPropertyInfo info 
=  property.GetProperty(shape,  this .cmbProperty.Text);
None.gif
if  (info  !=   null )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this.txtName.Text = info.Name;//属性名称
InBlock.gif
    this.txtValue.Text = info.Value;//属性值
InBlock.gif
    this.txtFormat.Text = info.Format;//属性格式
InBlock.gif
    this.txtSortKey.Text = info.Sort;//属性的排序
InBlock.gif
    this.txtPrompt.Text = info.Prompt;//属性的提示信息
ExpandedBlockEnd.gif
}

None.gif
None.gif
None.gif
// 根据模具,获取属性对象集合
None.gif
public  List < StencilPropertyInfo >  GetPropertyCollection(Visio.Shape shape)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    List
<StencilPropertyInfo> list = new List<StencilPropertyInfo>();
InBlock.gif    StencilPropertyInfo propertyInfo;
InBlock.gif    Visio.Cell shapeCell;
InBlock.gif    
short shortSectionProp = (short)VisSectionIndices.visSectionProp;
InBlock.gif
InBlock.gif    
if (shape != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for (short i = 0; i < shape.get_RowCount(shortSectionProp) - 1; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (shape.get_CellsSRCExists(shortSectionProp, i, (short)VisCellIndices.visCustPropsLabel, 0!= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                propertyInfo 
= new StencilPropertyInfo();
InBlock.gif
InBlock.gif                shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsLabel);
InBlock.gif                propertyInfo.Name 
= VisioUtility.FormulaStringToString(shapeCell.RowNameU);
InBlock.gif
InBlock.gif                shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsPrompt);
InBlock.gif                propertyInfo.Prompt 
= VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsFormat);
InBlock.gif                propertyInfo.Format 
= VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsValue);
InBlock.gif                propertyInfo.Value 
= VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsSortKey);
InBlock.gif                propertyInfo.Sort 
= VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                
//shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsType);
InBlock.gif                
//propertyInfo.PropType = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                
//shapeCell = shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsInvis);
InBlock.gif                
//propertyInfo.InVisible = VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                
//dot.gifdot.gifdot.gif..
InBlock.gif
                list.Add(propertyInfo);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return list;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// 根据模具和属性名称,获取属性对象信息
None.gif
public  StencilPropertyInfo GetProperty(Visio.Shape shape,  string  propertyName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    List
<StencilPropertyInfo> list = GetPropertyCollection(shape);
InBlock.gif    StencilPropertyInfo propertyInfo 
= null;
InBlock.gif    
foreach(StencilPropertyInfo tempInfo in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (tempInfo.Name == propertyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            propertyInfo 
= tempInfo;
InBlock.gif            
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return propertyInfo;
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值