■ Control 클래스를 사용해 확대/축소/이동 컨트롤을 만드는 방법을 보여준다.
▶ ControlDictionary.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 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject"> <Style TargetType="{x:Type local:ZoomableCanvas}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ZoomableCanvas}"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <Canvas Name="PART_Canvas" Width="{TemplateBinding CanvasWidth}" Height="{TemplateBinding CanvasHeight}" Background="LightGray"> </Canvas> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> |
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ControlDictionary.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <local:ZoomableCanvas Margin="10" Background="LightYellow" CanvasWidth="500" CanvasHeight="400" ClipToBounds="True" /> </Window> |
▶ ZoomableCanvas.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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace TestProject; /// <summary> /// 확대/축소 컨트롤 /// </summary> public class ZoomableCanvas : Control { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 캔버스 너비 속성 - CanvasWidthProperty /// <summary> /// 캔버스 너비 속성 /// </summary> public static readonly DependencyProperty CanvasWidthProperty = DependencyProperty.Register ( "CanvasWidth", typeof(double), typeof(ZoomableCanvas), new PropertyMetadata(300.0, CanvasSizePropertyChangedCallback) ); #endregion #region 캔버스 높이 속성 - CanvasHeightProperty /// <summary> /// 캔버스 높이 속성 /// </summary> public static readonly DependencyProperty CanvasHeightProperty = DependencyProperty.Register ( "CanvasHeight", typeof(double), typeof(ZoomableCanvas), new PropertyMetadata(200.0, CanvasSizePropertyChangedCallback) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 캔버스 너비 - CanvasWidth /// <summary> /// 캔버스 너비 /// </summary> public double CanvasWidth { get => (double)GetValue(CanvasWidthProperty); set => SetValue(CanvasWidthProperty, value); } #endregion #region 캔버스 높이 - CanvasHeight /// <summary> /// 캔버스 높이 /// </summary> public double CanvasHeight { get => (double)GetValue(CanvasHeightProperty); set => SetValue(CanvasHeightProperty, value); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 캔버스 /// </summary> private Canvas canvas; /// <summary> /// 스케일 변환 /// </summary> private ScaleTransform scaleTransform; /// <summary> /// 이동 변환 /// </summary> private TranslateTransform translateTransform; /// <summary> /// 시작 포인트 /// </summary> private Point startPoint; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - ZoomableCanvas() /// <summary> /// 생성자 /// </summary> static ZoomableCanvas() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomableCanvas), new FrameworkPropertyMetadata(typeof(ZoomableCanvas))); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 캔버스 크기 속성 변경시 콜백 처리하기 - CanvasSizePropertyChangedCallback(d, e) /// <summary> /// 캔버스 크기 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void CanvasSizePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ZoomableCanvas zoomableCanvas = d as ZoomableCanvas; if(zoomableCanvas != null && zoomableCanvas.canvas != null) { zoomableCanvas.canvas.Width = zoomableCanvas.CanvasWidth; zoomableCanvas.canvas.Height = zoomableCanvas.CanvasHeight; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 템플리트 적용시 처리하기 - OnApplyTemplate() /// <summary> /// 템플리트 적용시 처리하기 /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); this.canvas = GetTemplateChild("PART_Canvas") as Canvas; if(this.canvas != null) { this.scaleTransform = new ScaleTransform(); this.translateTransform = new TranslateTransform(); TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(this.scaleTransform ); transformGroup.Children.Add(this.translateTransform); this.canvas.RenderTransformOrigin = new Point(0.5, 0.5); this.canvas.RenderTransform = transformGroup; } } #endregion //////////////////////////////////////////////////////////////////////////////// Protected #region 마우스 휠 처리하기 - OnMouseWheel(e) /// <summary> /// 마우스 휠 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseWheel(MouseWheelEventArgs e) { base.OnMouseWheel(e); if(this.canvas == null) { return; } double zoom = e.Delta > 0 ? 1.1 : 0.9; Point mousePoition = e.GetPosition(this.canvas); this.scaleTransform.ScaleX *= zoom; this.scaleTransform.ScaleY *= zoom; this.translateTransform.X += (1 - zoom) * mousePoition.X; this.translateTransform.Y += (1 - zoom) * mousePoition.Y; } #endregion #region 마우스 왼쪽 버튼 DOWN 처리하기 - OnMouseLeftButtonDown(e) /// <summary> /// 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); this.startPoint = e.GetPosition(this); CaptureMouse(); } #endregion #region 마우스 이동시 처리하기 - OnMouseMove(e) /// <summary> /// 마우스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if(IsMouseCaptured) { Point currentPoint = e.GetPosition(this); Vector deltaVector = currentPoint - this.startPoint; this.translateTransform.X += deltaVector.X; this.translateTransform.Y += deltaVector.Y; this.startPoint = currentPoint; } } #endregion #region 마우스 왼쪽 버튼 UP 처리하기 - OnMouseLeftButtonUp(e) /// <summary> /// 마우스 왼쪽 버튼 UP 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); ReleaseMouseCapture(); } #endregion } |