[C#/WPF] Shape 클래스 : 환형 진행바 사용하기
■ Shape 클래스를 사용해 환형 진행바를 만드는 방법을 보여준다. ▶ DoubleToPercentageStringConverter.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 |
using System; using System.Globalization; using System.Windows.Data; namespace TestProject { /// <summary> /// 실수→백분율 문자열 변환자 /// </summary> public class DoubleToPercentageStringConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { double value = (double)sourceValue; string targetValue = string.Format("{0}%", value); return targetValue; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { throw new NotImplementedException(); } #endregion } } |
▶ CircularProgressBar.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 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 환형 진행바 /// </summary> public class CircularProgressBar : Shape { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 값 속성 - ValueProperty /// <summary> /// 값 속성 /// </summary> public static readonly DependencyProperty ValueProperty = DependencyProperty.Register ( "Value", typeof(double), typeof(CircularProgressBar), new FrameworkPropertyMetadata ( 0.0, FrameworkPropertyMetadataOptions.AffectsRender, null, new CoerceValueCallback(ValuePropertyCoerceValueCallback) ) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 값 - Value /// <summary> /// 값 /// </summary> public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 정의 지오메트리 - DefiningGeometry /// <summary> /// 정의 지오메트리 /// </summary> protected override Geometry DefiningGeometry { get { double startAngle = 90.0; double endAngle = 90.0 - ((Value / 100.0) * 360.0); double maximumWidth = Math.Max(0.0, RenderSize.Width - StrokeThickness); double maximumHeight = Math.Max(0.0, RenderSize.Height - StrokeThickness); double xStart = maximumWidth / 2.0 * Math.Cos(startAngle * Math.PI / 180.0); double yStart = maximumHeight / 2.0 * Math.Sin(startAngle * Math.PI / 180.0); double xEnd = maximumWidth / 2.0 * Math.Cos(endAngle * Math.PI / 180.0); double yEnd = maximumHeight / 2.0 * Math.Sin(endAngle * Math.PI / 180.0); StreamGeometry streamGeometry = new StreamGeometry(); using(StreamGeometryContext context = streamGeometry.Open()) { context.BeginFigure ( new Point ( (RenderSize.Width / 2.0) + xStart, (RenderSize.Height / 2.0) - yStart ), true, false ); context.ArcTo ( new Point ( (RenderSize.Width / 2.0) + xEnd, (RenderSize.Height / 2.0) - yEnd ), new Size(maximumWidth / 2.0, maximumHeight / 2), 0.0, (startAngle - endAngle) > 180, SweepDirection.Clockwise, true, false ); } return streamGeometry; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - CircularProgressBar() /// <summary> /// 생성자 /// </summary> static CircularProgressBar() { StrokeThicknessProperty.OverrideMetadata ( typeof(CircularProgressBar), new FrameworkPropertyMetadata(10.0) ); Brush brush = new SolidColorBrush(Color.FromArgb(255, 6, 176, 37)); brush.Freeze(); StrokeProperty.OverrideMetadata ( typeof(CircularProgressBar), new FrameworkPropertyMetadata(brush) ); FillProperty.OverrideMetadata ( typeof(CircularProgressBar), new FrameworkPropertyMetadata(Brushes.Transparent) ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 값 속성 값 강제 콜백 처리하기 - ValuePropertyCoerceValueCallback(d, value) /// <summary> /// 값 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="value">값</param> /// <returns>처리 결과</returns> private static object ValuePropertyCoerceValueCallback(DependencyObject d, object value) { double valueDouble = (double)value; valueDouble = Math.Min(valueDouble, 99.999); valueDouble = Math.Max(valueDouble, 0.0 ); return valueDouble; } #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 |
<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="Shape 클래스 : 환형 진행바 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:DoubleToPercentageStringConverter x:Key="DoubleToPercentageStringConverterKey" /> <Style x:Key="CircularProgressBarStyleKey" TargetType="ProgressBar"> <Setter Property="Width" Value="150" /> <Setter Property="Height" Value="150" /> <Setter Property="Foreground" Value="#01d328" /> <Setter Property="Maximum" Value="100" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ProgressBar"> <Grid Name="rootGrid" SnapsToDevicePixels="true"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="32" Foreground="DarkGray" Text="{TemplateBinding Value, Converter={StaticResource DoubleToPercentageStringConverterKey}}" /> <local:CircularProgressBar Stroke="{TemplateBinding Foreground}" Value="{TemplateBinding Value}" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <ProgressBar Style="{DynamicResource CircularProgressBarStyleKey}" Value="55" /> </Grid> </Window> |
TestProject.zip