■ AddFlow 클래스를 사용해 다이어그램을 생성하는 방법을 보여준다. (커스텀 그리기)
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using Lassalle.Flow; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.addFlow.NodeOwnerDraw += addFlow_NodeOwnerDraw; this.addFlow.LinkOwnerDraw += addFlow_LinkOwnerDraw; this.addFlow.DiagramOwnerDraw += addFlow_DiagramOwnerDraw; CreateDiagram(this.addFlow); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region AddFlow 다이어그램 소유자 그리기 - addFlow_DiagramOwnerDraw(sender, e) /// <summary> /// AddFlow 다이어그램 소유자 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_DiagramOwnerDraw(object sender, DiagramOwnerDrawEventArgs e) { Graphics graphics = e.Graphics; GraphicsState graphicsState = graphics.Save(); int swimLineHeight = 50; int width = (int)(this.addFlow.ClientRectangle.Size.Width + this.addFlow.Extent.Width ); int height = (int)(this.addFlow.ClientRectangle.Size.Height + this.addFlow.Extent.Height); int swimlineCount = height / swimLineHeight; Rectangle rectangle; for(int i = 0; i <= swimlineCount; i++) { rectangle = new Rectangle(0, i * swimLineHeight, width, swimLineHeight); if(i % 2 == 0) { graphics.FillRectangle(Brushes.LightYellow, rectangle); } else { graphics.FillRectangle(Brushes.LightBlue, rectangle); } } graphics.Restore(graphicsState); } #endregion #region AddFlow 노드 소유자 그리기 - addFlow_NodeOwnerDraw(sender, e) /// <summary> /// AddFlow 노드 소유자 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_NodeOwnerDraw(object sender, ContentItemOwnerDrawEventArgs e) { Graphics graphics = e.Graphics; Node node = e.ContentItem as Node; GraphicsState graphicsState = graphics.Save(); HatchBrush hatchBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.White, Color.Silver); RectangleF nodeRectangle = new RectangleF(node.Location, node.Size); graphics.FillEllipse(hatchBrush, nodeRectangle); graphics.Restore(graphicsState); // AddFlow에게 노드를 칠하지 말라고 알린다. e.Flags = ~ContentItemDrawFlags.Fill; } #endregion #region AddFlow 링크 소유자 그리기 - addFlow_LinkOwnerDraw(sender, e) /// <summary> /// AddFlow 링크 소유자 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_LinkOwnerDraw(object sender, LinkOwnerDrawEventArgs e) { Graphics graphics = e.Graphics; Link link = e.Link; if(link.Points.Count > 2) { GraphicsState graphicsState = graphics.Save(); int degree = 0; Brush brush = new SolidBrush(link.TextColor); for(int i = 0; i < link.Points.Count - 1; i++) { float x1 = link.Points[i ].X; float y1 = link.Points[i ].Y; float x2 = link.Points[i + 1].X; float y2 = link.Points[i + 1].Y; float x = x2 - x1; float y = -(y2 - y1); float distance = (float)Math.Sqrt(x * x + y * y); float cosA = x / distance; float sinA = y / distance; if(distance == 0) { degree = 0; } else { degree = (int)(180.0 / 3.1415899999999999 * Math.Acos(cosA)); if(sinA < 0) { degree = -degree; } if(cosA < 0) { degree += 180; } } PointF point = new PointF ( (link.Points[i].X + link.Points[i + 1].X) / 2, (link.Points[i].Y + link.Points[i + 1].Y) / 2 ); graphics.TranslateTransform(point.X, point.Y); if(degree != 0) { graphics.RotateTransform(-degree); } graphics.DrawString(link.Text, link.Font, brush, new PointF(0, 0)); if(degree != 0) { graphics.RotateTransform(degree); } graphics.TranslateTransform(-point.X, -point.Y); } graphics.Restore(graphicsState); // AddFlow에게 텍스트를 표시하지 말라고 알린다. // 그러나, 링크의 다른 부분들은 (라인, 화살표) AddFlow에 의해 표시될 것이다. e.Flags = ~LinkDrawFlags.Text; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 다이어그램 생성하기 - CreateDiagram(addFlow) /// <summary> /// 다이어그램 생성하기 /// </summary> /// <param name="addFlow">AddFlow</param> private void CreateDiagram(AddFlow addFlow) { addFlow.PageUnit = GraphicsUnit.Point; addFlow.IsOwnerDraw = true; addFlow.NodeModel.IsOwnerDraw = true; addFlow.NodeModel.ShapeFamily = ShapeFamily.Ellipse; addFlow.NodeModel.Font = new Font("Calibri", 12); addFlow.LinkModel.IsOwnerDraw = true; // 2개의 노드들과 링크를 생성한다. Node startNode = new Node(30 , 30, 50, 50, "노드 1", addFlow); Node endNode = new Node(330, 30, 50, 50, "노드 2", addFlow); addFlow.AddNode(startNode); addFlow.AddNode(endNode ); Link link = new Link(startNode, endNode, "링크", addFlow); link.DrawColor = Color.Red; addFlow.AddLink(link); link.AddPoint(new PointF(55 , 160)); link.AddPoint(new PointF(355, 160)); } #endregion } } |