C#进行Visio二次开发之鸡毛蒜皮(一)

本文主要分享一些在Visio二次开发中碰到的各种小问题及其解决方法:
1. 为图元设置颜色
在一些分析中,如电气线路分析中,需要根据不同的状态改变图元的颜色,那么如何改变指定图元的设备颜色呢?下面提供代码解决该问题。
None.gif   shape.get_CellsU( " LineColor " ).ResultIU  =  ( double )VisDefaultColors.visDarkGreen; // 有电(绿色)
None.gif
其中VisDefaultColors是一个枚举,有很多颜色,可以查下SDK,也可以使用其对应的数值来代替
ConstantValueDescription

visBlack

0

Black

visBlue

4

Blue

visCyan

7

Cyan

visDarkBlue

10

Dark blue

visDarkCyan

13

Dark cyan

visDarkGray

19

Dark gray

visDarkGreen

9

Dark green

visDarkRed

8

Dark red

visDarkYellow

11

Dark yellow

............

上面的代码是比较简洁的写法,当然也可以使用下面这种方式:
None.gif shape.get_CellsSRC(( short )VisSectionIndices.visSectionObject, ( short )VisRowIndices.visRowLine,( short )VisCellIndices.visLineColor).FormulaU  =   4

2. 获取图元设备的连接关系
每个设备Shape都有一个Connects和FromConnects集合,该集合是Connect对象集合,每个Connect有ToSheet和FromSheet属性,分别是指向一个Shape对象,我们如果要获取设备的关联关系,就是需要判断这些Connect的ToSheet和FromSheet属性。
如下代码:
None.gif              string  strShapes  =   " ; " ;
None.gif            
if  (IsSpecialEquipTypeInShape(shape))
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
foreach (Visio.Connect connect in shape.Connects)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strShapes 
+= GetConnectsShapes(shape, connect.ToSheet);     //检查接入的设备
InBlock.gif
                    strShapes += GetConnectsShapes(shape, connect.FromSheet);   //检查接出的设备
ExpandedSubBlockEnd.gif
                }

InBlock.gif
InBlock.gif                
foreach (Visio.Connect connect in shape.FromConnects)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strShapes 
+= GetConnectsShapes(shape, connect.ToSheet);     //检查接入的设备
InBlock.gif
                    strShapes += GetConnectsShapes(shape, connect.FromSheet);   //检查接出的设备
ExpandedSubBlockEnd.gif
                }

ExpandedBlockEnd.gif            }

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 获取与当前的图元连接(接入或接出)的所有相关设备
ExpandedBlockEnd.gif        
/// </summary>

None.gif          private   string  GetConnectsShapes(Visio.Shape shape, Visio.Shape toFromSheet)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strShapes = string.Empty;
InBlock.gif            
bool exist = VisioUtility.ShapeCellExist(toFromSheet, "设备类型");
InBlock.gif            
bool isSpecial = IsSpecialEquipTypeInShape(toFromSheet);
InBlock.gif
InBlock.gif            
if (toFromSheet != null && exist && isSpecial)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Visio图元的连接集合,会存放自己本身的,所以此处需要判断。
InBlock.gif
                if (shape.ID != toFromSheet.ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strShapes 
+= string.Format("{0};", toFromSheet.ID);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return strShapes;
ExpandedBlockEnd.gif        }

3. 获取图元的属性集合
我们知道,每个图元Shape甚至Page对象都有很多自定义属性,你可以通过在Visio的开发模式中查看ShapeSheet查看到。而所有这些属性中,每行又代表一个属性的各种定义信息,如Label是什么,Prompt(提示)是什么,Value(值)是什么,Type(类型)是什么,这就有点类似于我们在数据库定义一个字段,需要指定字段的名称,类型等等,那如果我们需要把这些信息保存下来,我们该如何获取呢?下面举例说明:
None.gif             Dictionary < string , StencilPropertyInfo >  list  =   new  Dictionary < string , StencilPropertyInfo > ();
None.gif            StencilPropertyInfo propertyInfo;
None.gif            Visio.Cell shapeCell;
None.gif            
short  shortSectionProp  =  ( short )VisSectionIndices.visSectionProp;
None.gif
None.gif            
if  (shape  !=   null )
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
for (short i = 0; i < shape.get_RowCount(shortSectionProp); 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.SortKey 
= VisioUtility.FormulaStringToString(shapeCell.FormulaU);
InBlock.gif
InBlock.gif                        shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsType);
InBlock.gif                        propertyInfo.PropType 
= (PropType)shapeCell.get_ResultInt((short)VisUnitCodes.visNumber, 0);

InBlock.gif                        shapeCell 
= shape.get_CellsSRC(shortSectionProp, i, (short)VisCellIndices.visCustPropsInvis);
InBlock.gif                        
if ("True".Equals(VisioUtility.FormulaStringToString(shapeCell.FormulaU), StringComparison.OrdinalIgnoreCase))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            propertyInfo.InVisible 
= true;
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        propertyInfo.PropRowID 
= i;                       
InBlock.gif
InBlock.gif                        
if(!list.ContainsKey(propertyInfo.Name))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            list.Add(propertyInfo.Name, propertyInfo);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

None.gif
None.gif            
return  list;

4. 关闭视图中打开的所有模具
一般来说,一个Visio文档,一般会打开很多模具窗口,用来辅助画图的,我们有时候不小心关闭一些,又有可能打开多几个,那么你是如何记住这些打开的模具文件的呢,我们要如何关闭全部呢,你可以使用TryCatch来关闭每个文件,即使它可能已经关闭了,这种才保证不会出错;我们不太喜欢暴力,还有没有更好的方法呢,让它自己知道那些可以关闭的呢?

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 关闭视图中打开的所有模具
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="visApp"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public   bool  CloseAllStencileDocument(Application visApp)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string[] strs = new string[0];
InBlock.gif            Array arr 
= strs as Array;
InBlock.gif            visApp.Documents.GetNames(
out arr);
InBlock.gif            Document visDoc;
InBlock.gif            
foreach (object file in arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (file.ToString().IndexOf(".vss", StringComparison.OrdinalIgnoreCase) > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    visDoc 
= visApp.Documents[file];
InBlock.gif                    
if (visDoc != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        visDoc.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return true;
ExpandedBlockEnd.gif        }


5. 管理Visio内置的窗口
Visio控件提供了很多内置的窗口,你可以根据需要显示或者隐藏它,如我们常常看到的图元属性窗口、形状查询窗口、模具管理窗口等等,很典型的例子,我们很多时候不需要那个形状查询窗口,想把它隐藏,那么这些该如何操作呢?
我前面写过的文章C#进行Visio二次开发的常见问题处理 有说明,如下所示:

None.gif          // Visio2007的形状窗口中去除搜索形状功能
None.gif
            VisApplication.Settings.ShowShapeSearchPane  =   false ;
None.gif
ContractedBlock.gifExpandedBlockStart.gif            Visio2003的ShowShapeSearchPane实现方式
Visio2003的ShowShapeSearchPane实现方式 #region Visio2003的ShowShapeSearchPane实现方式
InBlock.gif            
//Window searchWindow = wndVisio.Windows.get_ItemFromID(Convert.ToInt16(VisWinTypes.visWinIDShapeSearch));
InBlock.gif            
//if (searchWindow != null)
InBlock.gif            
//{
InBlock.gif            
//    searchWindow.Visible = false;
InBlock.gif            
//
ExpandedBlockEnd.gif
            #endregion


还有一种方式可以管理窗口,如下面代码,对各种内置的窗口实现了统一的管理

ExpandedBlockStart.gif ContractedBlock.gif                  for  ( int  i  =  drawingControl.Window.Windows.Count; i  >   0 ; i -- dot.gif {
InBlock.gif                    Window visWindow;
InBlock.gif                    
int windowType;
InBlock.gif
InBlock.gif                    visWindow 
= drawingControl.Window.Windows.get_ItemEx(i);
InBlock.gif                    windowType 
= visWindow.Type;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if (windowType == (int) VisWinTypes.visAnchorBarBuiltIn) dot.gif{
InBlock.gif
InBlock.gif                        
switch (visWindow.ID) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
case (int) VisWinTypes.visWinIDCustProp:
InBlock.gif                            
case (int) VisWinTypes.visWinIDDrawingExplorer:
InBlock.gif                            
case (int) VisWinTypes.visWinIDMasterExplorer:
InBlock.gif                            
case (int) VisWinTypes.visWinIDPanZoom:
InBlock.gif                            
case (int) VisWinTypes.visWinIDShapeSearch:
InBlock.gif                            
case (int) VisWinTypes.visWinIDSizePos:
InBlock.gif                            
case (int) VisWinTypes.visWinIDStencilExplorer:
InBlock.gif
InBlock.gif                                visWindow.Visible 
= false;
InBlock.gif                                
break;
InBlock.gif
InBlock.gif                            
default:
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedBlockEnd.gif                }


这下明白了多少了呢,要饮水思源哦

None.gif 转载请注明出处:
None.gif撰写人:伍华聪  http:
// www.iqidi.com 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值