첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 원 위에 문자 그리기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<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

 

 

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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요