■ BezierSegment 엘리먼트에서 제어점을 변경하는 방법을 보여준다.
▶ MainWindow.xaml
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 |
<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="BezierSegment 엘리먼트 : 제어점 변경하기" FontFamily="나눔고딕코딩" FontSize="16"> <Canvas Name="canvas"> <Path Fill="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"> <Path.Data> <GeometryGroup> <EllipseGeometry x:Name="startPoint" RadiusX="2" RadiusY="2" /> <EllipseGeometry x:Name="control1Point" RadiusX="2" RadiusY="2" /> <EllipseGeometry x:Name="control2Point" RadiusX="2" RadiusY="2" /> <EllipseGeometry x:Name="endPoint" RadiusX="2" RadiusY="2" /> </GeometryGroup> </Path.Data> </Path> <Path Stroke="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"> <Path.Data> <PathGeometry> <PathGeometry.Figures> <PathFigure StartPoint="{Binding ElementName=startPoint, Path=Center}"> <BezierSegment Point1="{Binding ElementName=control1Point, Path=Center}" Point2="{Binding ElementName=control2Point, Path=Center}" Point3="{Binding ElementName=endPoint , Path=Center}" /> </PathFigure> </PathGeometry.Figures> </PathGeometry> </Path.Data> </Path> <Path Stroke="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"> <Path.Data> <GeometryGroup> <LineGeometry StartPoint="{Binding ElementName=startPoint , Path=Center}" EndPoint= "{Binding ElementName=control1Point, Path=Center}" /> <LineGeometry StartPoint="{Binding ElementName=endPoint , Path=Center}" EndPoint= "{Binding ElementName=control2Point, Path=Center}" /> </GeometryGroup> </Path.Data> </Path> <Label Canvas.Left="{Binding ElementName=startPoint, Path=Center.X}" Canvas.Top= "{Binding ElementName=startPoint, Path=Center.Y}" Content= "{Binding ElementName=startPoint, Path=Center}" /> <Label Canvas.Left="{Binding ElementName=control1Point, Path=Center.X}" Canvas.Top= "{Binding ElementName=control1Point, Path=Center.Y}" Content= "{Binding ElementName=control1Point, Path=Center}" /> <Label Canvas.Left="{Binding ElementName=control2Point, Path=Center.X}" Canvas.Top= "{Binding ElementName=control2Point, Path=Center.Y}" Content= "{Binding ElementName=control2Point, Path=Center}" /> <Label Canvas.Left="{Binding ElementName=endPoint, Path=Center.X}" Canvas.Top= "{Binding ElementName=endPoint, Path=Center.Y}" Content= "{Binding ElementName=endPoint, Path=Center}" /> </Canvas> </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 |
using System.Windows; using System.Windows.Input; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.canvas.SizeChanged += canvas_SizeChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 마우스 DOWN 처리하기 - OnMouseDown(e) /// <summary> /// 마우스 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseDown(e); Point mousePoint = e.GetPosition(this.canvas); if(e.ChangedButton == MouseButton.Left) { this.control1Point.Center = mousePoint; } if(e.ChangedButton == MouseButton.Right) { this.control2Point.Center = mousePoint; } } #endregion #region 마우스 이동시 처리하기 - OnMouseMove(e) /// <summary> /// 마우스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); Point mousePoint = e.GetPosition(this.canvas); if(e.LeftButton == MouseButtonState.Pressed) { this.control1Point.Center = mousePoint; } if(e.RightButton == MouseButtonState.Pressed) { this.control2Point.Center = mousePoint; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 캔버스 크기 변경시 처리하기 - canvas_SizeChanged(sender, e) /// <summary> /// 캔버스 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvas_SizeChanged(object sender, SizeChangedEventArgs e) { this.startPoint.Center = new Point( e.NewSize.Width / 4, e.NewSize.Height / 2); this.control1Point.Center = new Point( e.NewSize.Width / 2, e.NewSize.Height / 4); this.control2Point.Center = new Point( e.NewSize.Width / 2, 3 * e.NewSize.Height / 4); this.endPoint.Center = new Point(3 * e.NewSize.Width / 4, e.NewSize.Height / 2); } #endregion } } |