C#完成流程圖設計器。本站提示廣大學習愛好者:(C#完成流程圖設計器)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成流程圖設計器正文
比來一個有時的機遇居然完成了一個簡略的流程圖設計器(固然其功效還有許多不完美的地方,然則心中照樣異常愉快,滿滿的造詣感)。
話不多說,先看一下完成的主界面後果:
右邊是一個ListView(listView1),左邊的畫布是一個Panel(panel1)。上面將重要思緒引見以下:
1)許可拖放,listView1和panel1設置其AllowDrop=true;
2)非銜接線類型的圖形拖放處置:右邊的listView1的項目被選中後,可以獲得其圖形類型(是路由器、是辦事器照樣雲等),並在全局變量中記載下以後的操尴尬刁難象類型,然後拖放到panel1後,panel1取得對應的圖形類型,起首斷定圖的類型能否為非銜接線,假如是則獲得對應的圖片,用g.DrawImage將其繪制到畫布中,圖片的坐標參考自以後鼠標(拖放到panel1最初松開鼠標左鍵時的坐標)的坐標。
3)銜接線類型的圖形處置:假如是銜接線,應當要有兩個點來肯定一條直線。被選中listView1的銜接線時,會在全局變量中記載下以後的操尴尬刁難象類型是銜接線,當在panel1上單擊時,首選斷定以後的操尴尬刁難象類型的全局對象能否為銜接線,假如是,則記載第一次單擊的點,然後期待記載單擊的第二個點,當第二次單擊完成後,挪用繪制直線的辦法在畫布中停止繪制直線。
4)當線和圖形綁定後,拖放圖形時,直線從屬在圖形的誰人點會跟著圖形地位的變更而變更,當最初定位後,panel1會重繪網格和流程圖。
5)編纂圖形信息:在panel1上雙擊時,法式獲得雙擊的坐標點離一切的圖形區域中比來的圖形,然後盤算間隔,看能否知足設置的阈值,假如小於阈值,則以為是在該圖形上雙擊,是要停止編纂操作。
上面給出繪制網格的代碼:
/// <summary> /// 繪制網格 /// </summary> private void renderGrid() { //全局變量存儲最年夜最小值,作為繪制區域 Graphics g = this.panel1.CreateGraphics(); Color color = Color.DarkGray; Pen p = new Pen(color, 1); p.DashStyle = DashStyle.Dash; for (int x = 0; x <= this.panel1.Width; x = x + 20) { PointF p1 = new PointF(x, 0); PointF p2 = new PointF(x, Height); g.DrawLine(p, p1, p2); } for (int y = 0; y <= panel1.Height; y = y + 20) { PointF p1 = new PointF(0, y); PointF p2 = new PointF(Width, y); g.DrawLine(p, p1, p2); } }
上面給出在panel1長進行鼠標單擊的處置法式:
private void panel1_MouseClick(object sender, MouseEventArgs e) { int X = e.X; int Y = e.Y; if (this.__gObjType== "") { return; } if (this.__gObjType != "Line") { AddObjectFromMouseLocation(X, Y, 0, 0, this.__gObjType); } else { //line if (__lineMouseClickedCount == 1) { __lineX2 = e.X; __lineY2 = e.Y; AddObjectFromMouseLocation(__lineX1, __lineY1, __lineX2, __lineY2, this.__gObjType); //銜接線偏向斷定 __lineMouseClickedCount = 0; __lineX1 = 0; __lineY1 = 0; __lineX2 = 0; __lineY2 = 0; } else if (__lineMouseClickedCount == 0) { __lineX1 = e.X; __lineY1 = e.Y; __lineMouseClickedCount = 1; } else { __lineMouseClickedCount = 0; __lineX1 = 0; __lineY1 = 0; __lineX2 = 0; __lineY2 = 0; } } }
上面給出重繪的法式:
private void ReDrawAll() { renderGrid(); Graphics g = this.panel1.CreateGraphics(); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; GObject CurrObj = new GObject(); Rectangle Rct = new Rectangle(); Pen p = new Pen(Color.Black); //p.Width = 2; p.Width = __penWidth * __zoomTimes; Image ObjImg; int xm = 0; int ym = 0; int _maxX = 0; int _maxY = 0; int _minX = 0; int _minY = 0; int _oldW = this.panel1.Width; int _oldH = this.panel1.Height; string IsLine = ""; //Nobj==50為以後畫布最年夜的對象個數 for (int i = 0; i < GNetworkFlow.Nobj; i++) { CurrObj = GNetworkFlow.GObjects[i]; //以後對象類型斷定 if (CurrObj.Type == "") IsLine = "N/D"; if (CurrObj.Type == "Line") IsLine = "Y"; if ((CurrObj.Type != "Line") && (CurrObj.Type != "")) IsLine = "N"; // #region old panel1作為畫布,將其嵌套在panel2中完成超越界限湧現轉動條 if (_maxX < CurrObj.x2) { _maxX = CurrObj.x2; } if (_maxY < CurrObj.y2) { _maxY = CurrObj.y2; } if (_minX > CurrObj.x1) { _minX = CurrObj.x1; } if (_minY > CurrObj.y1) { _minY = CurrObj.y1; } if (_oldW < _maxX - _minX) { this.panel1.Width = _maxX - _minX; } if (_oldH < _maxY - _minY) { this.panel1.Height = _maxY - _minY; } if (this.panel1.Height < this.panel2.Height) { this.panel1.Height = this.panel2.Height; } if (this.panel1.Width < this.panel2.Width) { this.panel1.Width = this.panel2.Width; } #endregion switch (IsLine) { case "Y": arrow.DrawArrow(g, p, p.Brush, CurrObj.x1, CurrObj.y1, CurrObj.x2, CurrObj.y2); xm = (CurrObj.x1 + CurrObj.x2) / 2; ym = (CurrObj.y1 + CurrObj.y2) / 2; AddText(xm, ym, CurrObj.Name, false); break; case "N": Rct.X = CurrObj.x1; Rct.Y = CurrObj.y1; Rct.Width = CurrObj.x2 - CurrObj.x1; Rct.Height = CurrObj.y2 - CurrObj.y1; if (CurrObj.Type != String.Empty) { ObjImg = FindGObjectTypeImage(CurrObj.Type); g.DrawImage(ObjImg, Rct); AddText(CurrObj.x1, CurrObj.y1, CurrObj.Name, true); GNetworkFlow.AdjustLinkedTo(CurrObj.Name); } break; } } }
上面將持續完美以下幾個功效:
1)序列化:可以將圖形序列化和反序列化,將序列化的信息保留到數據庫,也能夠從數據庫加載圖形;
2)圖形節點必需要附加其他屬性和辦法,為流程記載更多的信息、例如權限設置裝備擺設、以後處置的人、下一步是甚麼節點等;
3)畫圖功效的增強,畫圖可以靜態修正色彩,如許可以辨別流程在分歧節點的色彩顯示;
4)結構優化算法,可否依據畫布年夜小,主動分列圖形...
如今又將界面做了丑化,界面以下:
以上就是C#完成流程圖設計器的全體步調,還分享了完美設計器的技能,願望年夜家愛好。