남윤형

각 도형의 위치를 마우스로 클릭해서 이동하게 해줌

1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 +using System.Drawing;
3 using System.Linq; 4 using System.Linq;
4 using System.Text; 5 using System.Text;
5 using System.Threading.Tasks; 6 using System.Threading.Tasks;
...@@ -11,32 +12,97 @@ namespace flowchart ...@@ -11,32 +12,97 @@ namespace flowchart
11 // 마우스 클릭을 놓으면 어떤 도형을 선택했는지 판단해서, 해당 자료구조를 생성하고, 페인트 함수를 통해 그린다. 12 // 마우스 클릭을 놓으면 어떤 도형을 선택했는지 판단해서, 해당 자료구조를 생성하고, 페인트 함수를 통해 그린다.
12 class CustomPanel : Panel 13 class CustomPanel : Panel
13 { 14 {
14 - private String _selectName; 15 + private String _figName; // NONE, RECTANGLE, RHOMBUS
15 - private List<FigBase> _shapes = new List<FigBase>(); 16 + private List<FigBase> _shapes = new List<FigBase>(); // 선택된 도형을 저장한 리스트 자료구조
17 + private String _state; // NONE, DRAW, MOVE
18 + private Point _dragStartPoint = new Point(-1, -1); // 도형을 움직일때 시작위치
19 + private FigBase _findShape = null; // 자료구조에서 찾은 도형을 임시 저장하는 변수
16 20
17 - public string SelectName { get => _selectName; set => _selectName = value; } 21 + // 변수를 클래스 밖에서 읽고 쓰게 하는 함수
22 + public string FigName { get { return _figName; } set { _figName = value; } }
23 + public String State { get { return _state; } set { _state = value; } }
18 24
19 - protected override void OnMouseUp(MouseEventArgs e) 25 + // 마우스를 판넬에서 움질일때 호출되는 이벤트
26 + protected override void OnMouseMove(MouseEventArgs e)
20 { 27 {
21 - //MessageBox.Show("Added shape. " + SelectName + " " + e.Location.ToString()); 28 + // System.Diagnostics.Trace.WriteLine("debug >>>" + e.Location);
29 + // 마우스 버튼을 누르지 않은 상태일 경우
30 + if (e.Button == MouseButtons.None )
31 + {
32 + bool find = false;
33 +
34 + foreach (FigBase s in _shapes)
35 + {
36 + // 현재 마우스 위치가 지금껏 그린 리스트 자료구조 안의 도형의 영역안에 있는지 확인
37 + if (s.PointInRegion(e.Location))
38 + {
39 + find = true;
40 + }
41 + }
42 + // 그려진 도형위에 마우스가 오면 마우스 모양을 변경
43 + if (find)
44 + Cursor = Cursors.SizeAll;
45 + else
46 + Cursor = Cursors.Default;
22 47
23 - if (SelectName == "RECTANGLE") 48 + }
49 + // 마우스를 클릭했을때
50 + else if (e.Button == MouseButtons.Left)
24 { 51 {
25 - // 사각형에 대한 정보를 자료구조에 삽입한다. 52 + foreach (FigBase s in _shapes)
26 - FigRectangle rectangle = new FigRectangle(e.Location, new System.Drawing.Size(100, 100)); 53 + {
27 - _shapes.Add(rectangle); 54 + // 자료구조안의 위치와 동일할 경우 해당 자료구조를 다시 가져온다.
55 + if (s.PointInRegion(e.Location))
56 + {
57 + _findShape = s;
58 + break;
59 + }
60 + }
61 + // 도형을 찾았으면 움직이는 상태로 변경 후, 현재 마우스 위치를 저장함.
62 + if (_findShape != null)
63 + {
64 + State = "MOVE";
65 + _dragStartPoint = e.Location;
66 + }
28 } 67 }
29 - else if (SelectName == "ELLIPSE") 68 +
69 + base.OnMouseMove(e);
70 + }
71 +
72 + // 실제 도형을 그리거나 이동하는 함수, 최종 OnPaint() 함수가 호출되어 실행됨
73 + protected override void OnMouseUp(MouseEventArgs e)
74 + {
75 + // 그리기 상태
76 + if (State == "DRAW")
30 { 77 {
31 - // TODO : 원 그리기 78 +
79 + if (FigName == "RECTANGLE")
80 + {
81 + // 사각형에 대한 정보를 자료구조에 삽입한다.
82 + FigRectangle rectangle = new FigRectangle(e.Location, new System.Drawing.Size(100, 100));
83 + _shapes.Add(rectangle);
84 + }
85 + else if (FigName == "RHOMBUS")
86 + {
87 + FigRhombus rhombus = new FigRhombus(e.Location, new System.Drawing.Size(100, 100));
88 + _shapes.Add(rhombus);
89 + }
90 + else if (FigName == "TRIANGLE")
91 + {
92 + // TODO : 삼각형 그리기
93 + }
32 } 94 }
33 - else if (SelectName == "TRIANGLE") 95 + // 움직이는 상태
96 + else if (State == "MOVE")
34 { 97 {
35 - // TODO : 삼각형 그리기 98 + if (_findShape != null)
99 + {
100 + _findShape.Location = e.Location;
101 + }
36 } 102 }
37 103
38 - this.Cursor = Cursors.Default; 104 + State = "NONE";
39 - this.Refresh(); // 다시 그리기 요청 105 + this.Refresh(); // 다시 그리기 요청: OnPaint()
40 base.OnMouseUp(e); 106 base.OnMouseUp(e);
41 } 107 }
42 108
......
...@@ -31,5 +31,12 @@ namespace flowchart ...@@ -31,5 +31,12 @@ namespace flowchart
31 31
32 32
33 } 33 }
34 +
35 + // 현재 마우스의 위치가 해당 영역내에 있는지를 판단하는 함수
36 + public virtual bool PointInRegion(Point mousePoint)
37 + {
38 + Rectangle rect = new Rectangle(_location, _size);
39 + return rect.Contains(mousePoint);
40 + }
34 } 41 }
35 } 42 }
......
...@@ -7,8 +7,6 @@ using System.Threading.Tasks; ...@@ -7,8 +7,6 @@ using System.Threading.Tasks;
7 7
8 namespace flowchart 8 namespace flowchart
9 { 9 {
10 - // 프로세스 마름모를 그리는 클래스(FigureBase를 상속)
11 - // Draw 함수에서 마름모를 그린다.
12 class FigRhombus : FigBase 10 class FigRhombus : FigBase
13 { 11 {
14 // 생성자도 상속해서 사용. 12 // 생성자도 상속해서 사용.
...@@ -21,21 +19,19 @@ namespace flowchart ...@@ -21,21 +19,19 @@ namespace flowchart
21 { 19 {
22 using (Pen pen = new Pen(Color.Red, 1)) 20 using (Pen pen = new Pen(Color.Red, 1))
23 { 21 {
24 - System.Drawing.Rectangle rect = new System.Drawing.Rectangle(this.Location, this.Size); 22 + //Rectangle rect = new Rectangle(this.Location, this.Size);
25 - // 마름모 그리는 함수
26 int x1, x2, x3, y1, y2, y3; 23 int x1, x2, x3, y1, y2, y3;
27 x1 = Location.X; 24 x1 = Location.X;
28 - x2 = x1 + Size.Width / 2; 25 + x2 = Location.X + Size.Width / 2;
29 - x3 = x1+Size.Width; 26 + x3 = Location.X + Size.Width;
30 y1 = Location.Y; 27 y1 = Location.Y;
31 - y2 = y1 - Size.Height / 2; 28 + y2 = Location.Y + Size.Height / 2;
32 - y3 = y1 - Size.Height; 29 + y3 = Location.Y + Size.Height;
33 30
34 g.DrawLine(pen, x1, y2, x2, y1); 31 g.DrawLine(pen, x1, y2, x2, y1);
35 g.DrawLine(pen, x2, y1, x3, y2); 32 g.DrawLine(pen, x2, y1, x3, y2);
36 g.DrawLine(pen, x3, y2, x2, y3); 33 g.DrawLine(pen, x3, y2, x2, y3);
37 g.DrawLine(pen, x2, y3, x1, y2); 34 g.DrawLine(pen, x2, y3, x1, y2);
38 -
39 } 35 }
40 36
41 base.Draw(g); 37 base.Draw(g);
......
...@@ -17,16 +17,25 @@ namespace flowchart ...@@ -17,16 +17,25 @@ namespace flowchart
17 InitializeComponent(); 17 InitializeComponent();
18 } 18 }
19 19
20 - private void btn_rec_Click(object sender, EventArgs e) 20 + private void btn_default_Click(object sender, EventArgs e)
21 + {
22 + CustomPanel.State = "NONE";
23 + CustomPanel.FigName = "NONE";
24 + CustomPanel.Cursor = Cursors.Default;
25 + }
26 +
27 + private void btn_rectangle_Click(object sender, EventArgs e)
21 { 28 {
22 - CustomPanel.SelectName = "RECTANGLE"; 29 + CustomPanel.State = "DRAW"; // 도형을 그리겠다고 선택
30 + CustomPanel.FigName = "RECTANGLE"; // 사각형을 선택
23 CustomPanel.Cursor = Cursors.Hand; 31 CustomPanel.Cursor = Cursors.Hand;
24 } 32 }
25 33
26 - private void btn_default_Click(object sender, EventArgs e) 34 + private void btn_rhombus_Click(object sender, EventArgs e)
27 { 35 {
28 - CustomPanel.SelectName = "NONE"; 36 + CustomPanel.State = "DRAW";
29 - CustomPanel.Cursor = Cursors.Default; 37 + CustomPanel.FigName = "RHOMBUS";
38 + CustomPanel.Cursor = Cursors.Hand;
30 } 39 }
31 } 40 }
32 } 41 }
......