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

728x90
반응형

■ DispatcherTimer 클래스 : 파동 형태로 이동시키기

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


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" Canvas.Left="0" Canvas.Top="250"

            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>

        /// 진폭

        /// </summary>

        private const double AMPLITUDE = 50;

 

        /// <summary>

        /// 파장

        /// </summary>

        private const double WAVE_LENGTH = 100;

 

        /// <summary>

        /// 주기

        /// </summary>

        private const double CYCLE = 5;

 

        /// <summary>

        /// 시간

        /// </summary>

        private double time = 0;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.timer = new DispatcherTimer();

 

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

 

            this.timer.Tick   += timer_Tick;

            this.button.Click += button_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

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

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void timer_Tick(object sender, EventArgs e)

        {

            double target = this.canvas.ActualWidth;

 

            double left = Canvas.GetLeft(this.ellipse) + 10;

            double top  = AMPLITUDE * Math.Sin(2 * Math.PI * this.time / CYCLE - 2 * Math.PI * left / WAVE_LENGTH) + 250;

 

            if(left + this.ellipse.Width > target)

            {

                this.timer.Stop();

 

                return;

            }

 

            this.time = this.time + 0.05;

 

            Canvas.SetLeft(this.ellipse, left);

            Canvas.SetTop (this.ellipse, top );

        }

 

        #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.time = 0;

 

            Canvas.SetLeft(this.ellipse, 0  );

            Canvas.SetTop (this.ellipse, 250);

 

            this.timer.Start();

        }

 

        #endregion

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요