■ Polyline 클래스를 사용해 다각선을 그리는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="Polyline 클래스 : 다각선 그리기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Canvas Name="canvas" Background="White" /> </Grid> </Window> |
▶ MainWindow.xaml.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 |
using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 신규 다각선 /// </summary> private Polyline newPolyline = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.canvas.MouseDown += canvas_MouseDown; this.canvas.MouseMove += canvas_MouseMove; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 캔버스 마우스 DOWN 처리하기 - canvas_MouseDown(sender, e) /// <summary> /// 캔버스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvas_MouseDown(object sender, MouseButtonEventArgs e) { if(this.newPolyline != null) { if(e.ChangedButton == MouseButton.Left) { this.newPolyline.Points.Add(e.GetPosition(this.canvas)); } else { this.newPolyline = null; } } else { this.newPolyline = new Polyline(); this.newPolyline.Stroke = Brushes.LightGreen; this.newPolyline.StrokeThickness = 5; this.newPolyline.Points.Add(e.GetPosition(this.canvas)); this.newPolyline.Points.Add(e.GetPosition(this.canvas)); this.canvas.Children.Add(this.newPolyline); } } #endregion #region 캔버스 마우스 이동시 처리하기 - canvas_MouseMove(sender, e) /// <summary> /// 캔버스 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvas_MouseMove(object sender, MouseEventArgs e) { if(this.newPolyline == null) { return; } this.newPolyline.Points[this.newPolyline.Points.Count - 1] = e.GetPosition(this.canvas); } #endregion } } |