■ 원 위에 문자를 그리는 방법을 보여준다.
▶ 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 |
<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="원 위에 문자 그리기" FontFamily="나눔고딕코딩" FontSize="16" Loaded="Window_Loaded"> <Grid Background="LightGreen"> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="50" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Name="zoomLabel" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Content="100%" /> <Slider Name="slider" Grid.Row="1" Grid.Column="0" Margin="0 0 0 5" HorizontalAlignment="Center" Orientation="Vertical" Minimum="25" Maximum="500" Value="100" SmallChange="25" LargeChange="25" TickFrequency="25" TickPlacement="BottomRight" IsSnapToTickEnabled="True" ValueChanged="slider_ValueChanged" /> <ScrollViewer Name="scvGraph" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Canvas Name="canvas" HorizontalAlignment="Center" VerticalAlignment="Center" Width="400" Height="400" Background="White" /> </ScrollViewer> </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 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 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { double width = this.canvas.ActualWidth; double height = this.canvas.ActualHeight; double centerX = width / 2; double centerY = height / 2; for(int radius = 200; radius > 0; radius -= 50) { byte blue = (byte)(radius / 2 + 155); byte red = (byte)(blue / 2); byte green = (byte)(blue / 2); Ellipse ellipse = new Ellipse(); ellipse.Fill = new SolidColorBrush(Color.FromArgb(255, red, green, blue)); ellipse.Stroke = Brushes.Black; ellipse.StrokeThickness = 3; Canvas.SetLeft(ellipse, centerX - radius); Canvas.SetTop (ellipse, centerY - radius); ellipse.Width = 2 * radius; ellipse.Height = 2 * radius; this.canvas.Children.Add(ellipse); } double fontSize = 10; for(int radius = 25; radius < 200; radius += 50) { const double ANGLE_COUNT = 6; double deltaTheta = 360 / ANGLE_COUNT; double theta = deltaTheta / 2; for(int i = 0; i < ANGLE_COUNT; i++) { double angle = 90 - theta; double radians = (angle - 90) / 180 * Math.PI; double x = centerX + radius * Math.Cos(radians); double y = centerY + radius * Math.Sin(radians); string text = ((int)theta).ToString(); DrawText ( this.canvas, text, new Point(x, y), angle, fontSize, HorizontalAlignment.Center, VerticalAlignment.Center ); theta += deltaTheta; } fontSize += 3; } } #endregion #region 슬라이더 값 변경시 처리하기 - slider_ValueChanged(sender, e) /// <summary> /// 슬라이더 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { if(!IsInitialized) { return; } this.zoomLabel.Content = slider.Value + "%"; double scale = (double)(slider.Value / 100.0); this.canvas.LayoutTransform = new ScaleTransform(scale, scale); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 텍스트 그리기 - DrawText(canvas, text, location, angle, fontSize, horizontalAlignment, verticalAlignment) /// <summary> /// 텍스트 그리기 /// </summary> /// <param name="canvas">캔버스</param> /// <param name="text">텍스트</param> /// <param name="location">위치</param> /// <param name="angle">각도</param> /// <param name="fontSize">폰트 크기</param> /// <param name="horizontalAlignment">수평 정렬</param> /// <param name="verticalAlignment">수직 정렬</param> private void DrawText(Canvas canvas, string text, Point location, double angle, double fontSize, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) { Label label = new Label(); label.Content = text; label.FontSize = fontSize; canvas.Children.Add(label); if(angle != 0) { label.LayoutTransform = new RotateTransform(angle); } label.Measure(new Size(double.MaxValue, double.MaxValue)); double x = location.X; if(horizontalAlignment == HorizontalAlignment.Center) { x -= label.DesiredSize.Width / 2; } else if(horizontalAlignment == HorizontalAlignment.Right) { x -= label.DesiredSize.Width; } Canvas.SetLeft(label, x); double y = location.Y; if(verticalAlignment == VerticalAlignment.Center) { y -= label.DesiredSize.Height / 2; } else if(verticalAlignment == VerticalAlignment.Bottom) { y -= label.DesiredSize.Height; } Canvas.SetTop(label, y); } #endregion } } |