■ Adorner 클래스를 사용해 크기 조정 어도너를 만드는 방법을 보여준다.
▶ ResizingAdorner.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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
using System; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; namespace TestProject { /// <summary> /// 크기 조정 어도너 /// </summary> public class ResizingAdorner : Adorner { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 좌상단 썸 /// </summary> private Thumb topLeftThumb; /// <summary> /// 우상단 썸 /// </summary> private Thumb topRightThumb; /// <summary> /// 좌하단 썸 /// </summary> private Thumb bottomLeftThumb; /// <summary> /// 우하단 썸 /// </summary> private Thumb bottomRightThumb; /// <summary> /// 비주얼 컬렉션 /// </summary> private VisualCollection visualCollection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 비주얼 자식 수 - VisualChildrenCount /// <summary> /// 비주얼 자식 수 /// </summary> protected override int VisualChildrenCount { get { return this.visualCollection.Count; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ResizingAdorner(targetElement) /// <summary> /// 생성자 /// </summary> /// <param name="targetElement">타겟 엘리먼트</param> public ResizingAdorner(UIElement targetElement) : base(targetElement) { this.visualCollection = new VisualCollection(this); SetThumb(ref this.topLeftThumb , Cursors.SizeNWSE); SetThumb(ref this.topRightThumb , Cursors.SizeNESW); SetThumb(ref this.bottomLeftThumb , Cursors.SizeNESW); SetThumb(ref this.bottomRightThumb, Cursors.SizeNWSE); this.topLeftThumb.DragDelta += topLeftThumb_DragDelta; this.topRightThumb.DragDelta += topRightThumb_DragDelta; this.bottomLeftThumb.DragDelta += bottomLeftThumb_DragDelta; this.bottomRightThumb.DragDelta += bottomRightThumb_DragDelta; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 비주얼 자식 수 구하기 - GetVisualChild(index) /// <summary> /// 비주얼 자식 수 구하기 /// </summary> /// <param name="index">인덱스</param> /// <returns>비주얼 자식 수</returns> protected override Visual GetVisualChild(int index) { return this.visualCollection[index]; } #endregion #region 배열하기 (오버라이드) - ArrangeOverride(finalSize) /// <summary> /// 배열하기 (오버라이드) /// </summary> /// <param name="finalSize">최종 크기</param> /// <returns>크기</returns> protected override Size ArrangeOverride(Size finalSize) { double desiredWidth = AdornedElement.DesiredSize.Width; double desiredHeight = AdornedElement.DesiredSize.Height; double adornerWidth = DesiredSize.Width; double adornerHeight = DesiredSize.Height; this.topLeftThumb.Arrange ( new Rect ( -adornerWidth / 2, -adornerHeight / 2, adornerWidth, adornerHeight ) ); this.topRightThumb.Arrange ( new Rect ( desiredWidth - adornerWidth / 2, -adornerHeight / 2, adornerWidth, adornerHeight ) ); this.bottomLeftThumb.Arrange ( new Rect ( -adornerWidth / 2, desiredHeight - adornerHeight / 2, adornerWidth, adornerHeight ) ); this.bottomRightThumb.Arrange ( new Rect ( desiredWidth - adornerWidth / 2, desiredHeight - adornerHeight / 2, adornerWidth, adornerHeight ) ); return finalSize; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 좌상단 썸 드래그 델타 처리하기 - topLeftThumb_DragDelta(sender, e) /// <summary> /// 좌상단 썸 드래그 델타 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void topLeftThumb_DragDelta(object sender, DragDeltaEventArgs e) { FrameworkElement element = AdornedElement as FrameworkElement; Thumb thumb = sender as Thumb; if(element == null || thumb == null) { return; } SetSize(element); element.Width = Math.Max(element.Width - e.HorizontalChange, thumb.DesiredSize.Width ); element.Height = Math.Max(element.Height - e.VerticalChange , thumb.DesiredSize.Height); } #endregion #region 우상단 썸 드래그 델타 처리하기 - topRightThumb_DragDelta(sender, e) /// <summary> /// 우상단 썸 드래그 델타 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void topRightThumb_DragDelta(object sender, DragDeltaEventArgs e) { FrameworkElement element = AdornedElement as FrameworkElement; Thumb thumb = sender as Thumb; if(element == null || thumb == null) { return; } SetSize(element); element.Width = Math.Max(element.Width + e.HorizontalChange, thumb.DesiredSize.Width ); element.Height = Math.Max(element.Height - e.VerticalChange , thumb.DesiredSize.Height); } #endregion #region 좌하단 썸 드래그 델타 처리하기 - bottomLeftThumb_DragDelta(sender, e) /// <summary> /// 좌하단 썸 드래그 델타 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bottomLeftThumb_DragDelta(object sender, DragDeltaEventArgs e) { FrameworkElement element = AdornedElement as FrameworkElement; Thumb thumb = sender as Thumb; if(element == null || thumb == null) { return; } SetSize(element); element.Width = Math.Max(element.Width - e.HorizontalChange, thumb.DesiredSize.Width ); element.Height = Math.Max(e.VerticalChange + element.Height , thumb.DesiredSize.Height); } #endregion #region 우하단 썸 드래그 델타 처리하기 - bottomRightThumb_DragDelta(sender, e) /// <summary> /// 우하단 썸 드래그 델타 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bottomRightThumb_DragDelta(object sender, DragDeltaEventArgs e) { FrameworkElement element = AdornedElement as FrameworkElement; Thumb thumb = sender as Thumb; if(element == null || thumb == null) { return; } SetSize(element); element.Width = Math.Max(element.Width + e.HorizontalChange, thumb.DesiredSize.Width ); element.Height = Math.Max(e.VerticalChange + element.Height , thumb.DesiredSize.Height); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 썸 설정하기 - SetThumb(thumb, cursor) /// <summary> /// 썸 설정하기 /// </summary> /// <param name="thumb">코너 썸</param> /// <param name="cursor">커서</param> private void SetThumb(ref Thumb thumb, Cursor cursor) { if(thumb != null) { return; } thumb = new Thumb(); thumb.Height = thumb.Width = 10; thumb.Opacity = 0.40; thumb.Background = new SolidColorBrush(Colors.MediumBlue); thumb.Cursor = cursor; this.visualCollection.Add(thumb); } #endregion #region 크기 설정하기 - SetSize(targetElement) /// <summary> /// 크기 설정하기 /// </summary> /// <param name="targetElement">타겟 엘리먼트</param> private void SetSize(FrameworkElement targetElement) { if(targetElement.Width.Equals(Double.NaN)) { targetElement.Width = targetElement.DesiredSize.Width; } if(targetElement.Height.Equals(Double.NaN)) { targetElement.Height = targetElement.DesiredSize.Height; } FrameworkElement parentElement = targetElement.Parent as FrameworkElement; if(parentElement != null) { targetElement.MaxHeight = parentElement.ActualHeight; targetElement.MaxWidth = parentElement.ActualWidth; } } #endregion } } |
▶ 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 |
<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="Adorner 클래스 : 크기 조정 어도너 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Name="grid" ClipToBounds="True" ShowGridLines="True"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid Grid.Row="0" Grid.Column="0"> <Button Width="100" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Button" /> </Grid> <Grid Grid.Row="0" Grid.Column="1"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="TextBlock" /> </Grid> <Grid Grid.Row="0" Grid.Column="2"> <TextBox Width="100" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" Text="TextBox" /> </Grid> <Grid Grid.Row="1" Grid.Column="0"> <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" Content=" CheckBox" /> </Grid> <Grid Grid.Row="1" Grid.Column="1"> <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center"> <ComboBoxItem Content="Item 1" /> <ComboBoxItem Content="Item 2" /> <ComboBoxItem Content="Item 3" /> </ComboBox> </Grid> <Grid Grid.Column="2" Grid.Row="1"> <FlowDocumentPageViewer Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 어도너 레이어 /// </summary> private AdornerLayer adornerLayer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.adornerLayer = AdornerLayer.GetAdornerLayer(this.grid); foreach(Panel panel in grid.Children) { this.adornerLayer.Add(new ResizingAdorner(panel.Children[0])); } } #endregion } } |