■ 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 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 |
<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:ProcessRing}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ProcessRing}"> <Grid Background="{TemplateBinding Background}"> <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Width="{TemplateBinding RingWidth}" Height="{TemplateBinding RingHeight}"> <Grid Name="PART_ContainerGrid"> <Path Name="PART_BackgroundRingPath" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="{TemplateBinding RingThickness}" Stroke="{TemplateBinding RingBrush}" Opacity="0.3"> <Path.Data> <PathGeometry> <PathFigure x:Name="PART_BackgroundPathFigure"> <ArcSegment x:Name="PART_BackgroundArcSegment" Point="50 0" Size="50 50" SweepDirection="Clockwise" /> </PathFigure> </PathGeometry> </Path.Data> </Path> <Path Name="PART_RingPath" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="{TemplateBinding RingThickness}" Stroke="{TemplateBinding RingBrush}" RenderTransformOrigin="0.5 0.5"> <Path.Data> <PathGeometry> <PathFigure x:Name="PART_PathFigure"> <ArcSegment x:Name="PART_ArcSegment" Point="50 0" Size="50 50" SweepDirection="Clockwise" /> </PathFigure> </PathGeometry> </Path.Data> <Path.RenderTransform> <RotateTransform x:Name="PART_RotationTransform" /> </Path.RenderTransform> </Path> </Grid> </Viewbox> </Grid> </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 19 20 21 22 |
<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:ProcessRing Width="150" Height="150" RingWidth="100" RingHeight="100" RingStartAngle="0" RingEndAngle="270" RingThickness="10" RingBrush="Blue" IsProcessing="True" /> </Window> |
▶ ProcessRing.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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace TestProject; /// <summary> /// 프로세스 링 컨트롤 /// </summary> public class ProcessRing : Control { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 링 너비 속성 - RingWidthProperty /// <summary> /// 링 너비 속성 /// </summary> public static readonly DependencyProperty RingWidthProperty = DependencyProperty.Register ( "RingWidth", typeof(double), typeof(ProcessRing), new PropertyMetadata(80.0) ); #endregion #region 링 높이 속성 - RingHeightProperty /// <summary> /// 링 높이 속성 /// </summary> public static readonly DependencyProperty RingHeightProperty = DependencyProperty.Register ( "RingHeight", typeof(double), typeof(ProcessRing), new PropertyMetadata(80.0) ); #endregion #region 링 시작 각도 속성 - RingStartAngleProperty /// <summary> /// 링 시작 각도 속성 /// </summary> public static readonly DependencyProperty RingStartAngleProperty = DependencyProperty.Register ( "RingStartAngle", typeof(double), typeof(ProcessRing), new PropertyMetadata(0.0, RingAnglePropertyChangedCallback) ); #endregion #region 링 종료 각도 속성 - RingEndAngleProperty /// <summary> /// 링 종료 각도 속성 /// </summary> public static readonly DependencyProperty RingEndAngleProperty = DependencyProperty.Register ( "RingEndAngle", typeof(double), typeof(ProcessRing), new PropertyMetadata(360.0, RingAnglePropertyChangedCallback) ); #endregion #region 링 두께 속성 - RingThicknessProperty /// <summary> /// 링 두께 속성 /// </summary> public static readonly DependencyProperty RingThicknessProperty = DependencyProperty.Register ( "RingThickness", typeof(double), typeof(ProcessRing), new PropertyMetadata(5.0, RingThicknessPropertyChangedCallback) ); #endregion #region 링 브러시 속성 - RingBrushProperty /// <summary> /// 링 브러시 속성 /// </summary> public static readonly DependencyProperty RingBrushProperty = DependencyProperty.Register ( "RingBrush", typeof(Brush), typeof(ProcessRing), new PropertyMetadata(Brushes.Black) ); #endregion #region 처리 여부 속성 - IsProcessingProperty /// <summary> /// 처리 여부 속성 /// </summary> public static readonly DependencyProperty IsProcessingProperty = DependencyProperty.Register ( "IsProcessing", typeof(bool), typeof(ProcessRing), new PropertyMetadata(false, IsProcessingPropertyChangedCallback) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 회전 변환 /// </summary> private RotateTransform rotateTransform; /// <summary> /// 링 패스 /// </summary> private Path ringPath; /// <summary> /// 패스 피규어 /// </summary> private PathFigure pathFigure; /// <summary> /// 아크 세그먼트 /// </summary> private ArcSegment arcSegment; /// <summary> /// 배경 링 패스 /// </summary> private Path backgroundRingPath; /// <summary> /// 배경 패스 피규어 /// </summary> private PathFigure backgroundPathFigure; /// <summary> /// 배경 아크 세그먼트 /// </summary> private ArcSegment backgroundArcSegment; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 링 너비 - RingWidth /// <summary> /// 링 너비 /// </summary> public double RingWidth { get => (double)GetValue(RingWidthProperty); set => SetValue(RingWidthProperty, value); } #endregion #region 링 높이 - RingHeight /// <summary> /// 링 높이 /// </summary> public double RingHeight { get => (double)GetValue(RingHeightProperty); set => SetValue(RingHeightProperty, value); } #endregion #region 링 시작 각도 - RingStartAngle /// <summary> /// 링 시작 각도 /// </summary> public double RingStartAngle { get => (double)GetValue(RingStartAngleProperty); set => SetValue(RingStartAngleProperty, value); } #endregion #region 링 종료 각도 - RingEndAngle /// <summary> /// 링 종료 각도 /// </summary> public double RingEndAngle { get => (double)GetValue(RingEndAngleProperty); set => SetValue(RingEndAngleProperty, value); } #endregion #region 링 두께 - RingThickness /// <summary> /// 링 두께 /// </summary> public double RingThickness { get => (double)GetValue(RingThicknessProperty); set => SetValue(RingThicknessProperty, value); } #endregion #region 링 브러시 - RingBrush /// <summary> /// 링 브러시 /// </summary> public Brush RingBrush { get => (Brush)GetValue(RingBrushProperty); set => SetValue(RingBrushProperty, value); } #endregion #region 처리 여부 - IsProcessing /// <summary> /// 처리 여부 /// </summary> public bool IsProcessing { get => (bool)GetValue(IsProcessingProperty); set => SetValue(IsProcessingProperty, value); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - ProcessRing() /// <summary> /// 생성자 /// </summary> static ProcessRing() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ProcessRing), new FrameworkPropertyMetadata(typeof(ProcessRing))); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ProcessRing() /// <summary> /// 생성자 /// </summary> public ProcessRing() { Width = 100; Height = 100; RingWidth = 80; RingHeight = 80; RingThickness = 5; Background = Brushes.Transparent; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 링 각도 속성 변경시 콜백 처리하기 - RingAnglePropertyChangedCallback(d, e) /// <summary> /// 링 각도 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void RingAnglePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ProcessRing processRing = d as ProcessRing; processRing.UpdateRing(); } #endregion #region 링 두께 속성 변경시 콜백 처리하기 - RingThicknessPropertyChangedCallback(d, e) /// <summary> /// 링 두께 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void RingThicknessPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ProcessRing processRing = d as ProcessRing; processRing.RingThickness = Math.Min((double)e.NewValue, Math.Min(processRing.RingWidth, processRing.RingHeight) / 2); processRing.UpdateRing(); } #endregion #region 처리 여부 속성 변경시 콜백 처리하기 - IsProcessingPropertyChangedCallback(d, e) /// <summary> /// 처리 여부 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void IsProcessingPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ProcessRing processRing = d as ProcessRing; processRing.UpdateAnimation(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 템플리트 적용시 처리하기 - OnApplyTemplate() /// <summary> /// 템플리트 적용시 처리하기 /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); this.rotateTransform = GetTemplateChild("PART_RotationTransform" ) as RotateTransform; this.ringPath = GetTemplateChild("PART_RingPath" ) as Path; this.pathFigure = GetTemplateChild("PART_PathFigure" ) as PathFigure; this.arcSegment = GetTemplateChild("PART_ArcSegment" ) as ArcSegment; this.backgroundRingPath = GetTemplateChild("PART_BackgroundRingPath" ) as Path; this.backgroundPathFigure = GetTemplateChild("PART_BackgroundPathFigure") as PathFigure; this.backgroundArcSegment = GetTemplateChild("PART_BackgroundArcSegment") as ArcSegment; UpdateRing(); UpdateAnimation(); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 포인트 회전하기 - RotatePoint(point, angle) /// <summary> /// 포인트 회전하기 /// </summary> /// <param name="point">포인트</param> /// <param name="angle">각도</param> /// <returns>회전 포인트</returns> private Point RotatePoint(Point point, double angle) { double radians = angle * (Math.PI / 180); return new Point ( point.X * Math.Cos(radians) - point.Y * Math.Sin(radians) + RingThickness / 2, point.X * Math.Sin(radians) + point.Y * Math.Cos(radians) + RingThickness / 2 ); } #endregion #region 링 업데이트하기 - UpdateRing() /// <summary> /// 링 업데이트하기 /// </summary> private void UpdateRing() { if(this.pathFigure != null && this.arcSegment != null) { double radius = Math.Min(RingWidth, RingHeight) / 2 - RingThickness / 2; double angleDifference = (RingEndAngle - RingStartAngle + 360) % 360; #region 링를 설정한다. Point startPoint = new(radius, 0); startPoint = RotatePoint(startPoint, RingStartAngle); this.pathFigure.StartPoint = new Point(startPoint.X + radius, startPoint.Y + radius); Point endPoint = new Point(radius, 0); endPoint = RotatePoint(endPoint, RingEndAngle); this.arcSegment.Point = new Point(endPoint.X + radius, endPoint.Y + radius); this.arcSegment.Size = new Size(radius, radius); this.arcSegment.IsLargeArc = angleDifference > 180; #endregion #region 배경 링를 설정한다. Point backgroundStartPoint = new(radius, 0d); backgroundStartPoint = RotatePoint(backgroundStartPoint, 0d); this.backgroundPathFigure.StartPoint = new Point(backgroundStartPoint.X + radius, backgroundStartPoint.Y + radius); Point backgroundEndPoint = new Point(radius, 0); backgroundEndPoint = RotatePoint(backgroundEndPoint, 359.9); this.backgroundArcSegment.Point = new Point(backgroundEndPoint.X + radius, backgroundEndPoint.Y + radius); this.backgroundArcSegment.Size = new Size(radius, radius); this.backgroundArcSegment.IsLargeArc = angleDifference > 180; #endregion } } #endregion #region 애니메이션 업데이트하기 - UpdateAnimation() /// <summary> /// 애니메이션 업데이트하기 /// </summary> private void UpdateAnimation() { if(this.rotateTransform != null && this.ringPath != null) { if(IsProcessing) { DoubleAnimation rotateAnimation = new() { RepeatBehavior = RepeatBehavior.Forever, Duration = TimeSpan.FromSeconds(2), From = 0, To = 360 }; this.rotateTransform.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation); } else { this.rotateTransform.BeginAnimation(RotateTransform.AngleProperty, null); } } } #endregion } |