첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

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="DispatcherTimer 클래스 : 원 형태으로 이동시키기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Canvas Name="canvas" Margin="10">
        <Ellipse Name="ellipse"
            Width="30"
            Height="30"
            Fill="Orange" />
        <Button Name="button" Canvas.Right="10" Canvas.Bottom="10"
            Width="100"
            Height="30"
            Content="실행" />
    </Canvas>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 타이머
        /// </summary>
        private DispatcherTimer timer;

        /// <summary>
        /// 캔버스 중심 X
        /// </summary>
        private double canvasCenterX;

        /// <summary>
        /// 캔버스 중심 Y
        /// </summary>
        private double canvasCenterY;

        /// <summary>
        /// 타원 1/2 너비
        /// </summary>
        private double ellipseHalfWidth;

        /// <summary>
        /// 타원 1/2 높이
        /// </summary>
        private double ellipseHalfHeight;

        /// <summary>
        /// 반지름
        /// </summary>
        private double radius = 250;

        /// <summary>
        /// 각도
        /// </summary>
        private double angle = 0;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.timer = new DispatcherTimer();

            this.timer.Interval = TimeSpan.FromMilliseconds(100);

            SizeChanged       += Window_SizeChanged;
            Loaded            += Window_Loaded;
            this.timer.Tick   += timer_Tick;
            this.button.Click += button_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 크기 변경시 처리하기 - Window_SizeChanged(sender, e)

        /// <summary>
        /// 윈도우 크기 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.canvasCenterX = this.canvas.ActualWidth  / 2;
            this.canvasCenterY = this.canvas.ActualHeight / 2;
        }

        #endregion
        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.ellipseHalfWidth  = this.ellipse.Width  / 2;
            this.ellipseHalfHeight = this.ellipse.Height / 2;

            double ellipseLeft = this.canvasCenterX               - this.ellipseHalfWidth;
            double ellipseTop  = this.canvasCenterY + this.radius - this.ellipseHalfHeight;

            Canvas.SetLeft(this.ellipse, ellipseLeft);
            Canvas.SetTop (this.ellipse, ellipseTop );
        }

        #endregion
        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

        /// <summary>
        /// 타이머 틱 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.angle = this.angle + 10;

            if(this.angle > 360)
            {
                this.angle = 0;
            }

            double radian = this.angle * Math.PI / 180;

            double ellipseLeft = this.canvasCenterX + this.radius * Math.Sin(radian) - this.ellipseHalfWidth;
            double ellipseTop  = this.canvasCenterY + this.radius * Math.Cos(radian) - this.ellipseHalfHeight;

            Canvas.SetLeft(this.ellipse, ellipseLeft);
            Canvas.SetTop (this.ellipse, ellipseTop );
        }

        #endregion
        #region 실행 버튼 클릭시 처리하기 - button_Click(sender, e)

        /// <summary>
        /// 실행 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.angle = 0;

            double ellipseLeft = this.canvasCenterX               - this.ellipseHalfWidth;
            double ellipseTop  = this.canvasCenterY + this.radius - this.ellipseHalfHeight;

            Canvas.SetLeft(this.ellipse, ellipseLeft);
            Canvas.SetTop (this.ellipse, ellipseTop );

            this.timer.Start();
        }

        #endregion
    }
}

 

728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요