使用Visio 2003 Drawing Control开发应用(2)

Posted on 2007-10-25 18:38  ant-boss  阅读(656)  评论(0编辑  收藏  举报

这个控件直接暴露出来的事件并不是很多,但是也可以完成大部分的功能了。我没有用过的就不敢乱说了,下面说几个我用到的事件。

BeforeSelectionDelete:在Visio中,Selection是一个选中shape的集合。这个事件在删除前发生。这里因为我对于每个图形上的Shape都有关联的一些属性。所以为了实现Undo的时候恢复原来的内容,必须要在删除前做保留。比如:
if(this.visClickedShape != null)
{
 for(int i = 0; i < busResource.Components.Count; i++)
 {
  BusinessProcessComponent temp = busResource.Components[i] as BusinessProcessComponent;
  if(temp.ComponentName.Trim() == this.visClickedShape.Data1.Trim() &&
   temp.RunNumber == Convert.ToInt32(this.visClickedShape.Data2))
  {
   componentDeleted = temp.Clone() as BusinessProcessComponent;
   componentDeleted.Parameters.Clear();
   for(int j = 0; j < temp.Parameters.Count; j++)
    componentDeleted.Parameters.Add((temp.Parameters[j] as BusinessProcessComponentParameter).Clone());
   componentDeleted.Results.Clear();
   for(int j = 0; j < temp.Results.Count; j++)
    componentDeleted.Results.Add((temp.Results[j] as BusinessProcessComponentResult).Clone());
  }
 }
 System.Diagnostics.Debug.WriteLine("Set Delete flag");
 this.isDeleted = true;
}
这里我处理的是我定义的一个visClickedShape,而不是e.selection,因为是暂时处理了其中一个shape的内容备份,如果是对所有选中内容备份,就应该在e.selection中取出所有的shape进行备份。另外,这里用到了Data1和Data2,这里我放了一些数据,关

于Data1和Data2,我将在后面在讲操纵visio shapesheet和xml的时候说明。

KeyUpEvent,KeyPressEvent,KeyDownEvent:这些事件是处理键盘事件的。但是我发现有些时候,这些事件并不一定能正确响应。

MouseUpEvent等:处理鼠标事件,我用他来处理自定义菜单。
this.visClickedShape = VisioUtility.GetClickedShape(visPage, e.x, e.y); // 根据鼠标点确定选择的Shape
// 如果为鼠标右键点击
if(e.button == (int)Visio.VisKeyButtonFlags.visMouseRight)
{
 e.cancelDefault = true; // 取消Visio的鼠标右键事件响应
 if(this.visClickedShape != null)
 {
  this.contextComponent.Show(this, PageUnitsToPixels(ref visDrawing, e.x, e.y)); // 显示组件属性右键菜单
 }
 else
  this.contextBusinessProcess.Show(this, PageUnitsToPixels(ref visDrawing, e.x, e.y)); // 显示流程属性右键菜单
这里的PageUnitsToPixels是计算鼠标右键点对应Visio图形区内的鼠标点位置

ShapeAdded:这个事件在图形区有新的shape添加进去时激发。
事件的参数e中的shape就是添加的shape,可以对这个shape进行一些设置。比如:
if(e.shape.Master == null)
 return;
if(e.shape.Master.NameU == "Dynamic connector")
{
 // 如果是动态连接线,设置终点的箭头属性
 e.shape.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineEndArrow).FormulaU = "8";
 e.shape.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineArrowSize).FormulaU = "4";
 return;
}

Visio.Shape shapeAdded = e.shape;

还有其他事件,可以根据需要来使用

除了这些事件外,对各种对象还有单独事件可以利用。
比如:
this.axDrawingControl1.Window.Application.ActivePage.ConnectionsAdded += new Microsoft.Office.Interop.Visio.EPage_ConnectionsAddedEventHandler(ActivePage_ConnectionsAdded);
但是,我记得这样试过没有成功,也许是我自己的问题,后来因为不怎么需要,就没有研究了。

下一节介绍 对象的ShapeSheet和visio xml的处理