作者:xenogear
http://www.cnblogs.com/xenogear/
這個控件直接暴露出來的事件並不是很多,但是也可以完成大部分的功能了。我沒有用過的就不敢亂說了,下面說幾個我用到的事件。
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);
但是,我記得這樣試過沒有成功,也許是我自己的問題,後來因爲不怎麼需要,就沒有研究了。