첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
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="마우스 애니메이션 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <Style TargetType="Ellipse">
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <ScaleTransform ScaleX="1" ScaleY="1" />
                </Setter.Value>
            </Setter>
            <Setter Property="RenderTransformOrigin" Value="0.5 0.5" />
        </Style>
        <Storyboard x:Key="EllipseStoryBoardKey">
            <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" To="8" />
            <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" To="8" />
            <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"                                 To="0" />
        </Storyboard>
    </Window.Resources>
    <Canvas x:Name="canvas"/>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

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

        #region Field

        /// <summary>
        /// 난수기
        /// </summary>
        private Random random = new Random(DateTime.Now.Millisecond);

        /// <summary>
        /// 마지막 추가 경과 시간
        /// </summary>
        private TimeSpan lastAddedTimeSpan;

        #endregion

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

        #region 생성자 - MainWindow()

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

            MouseMove += TestWindow_MouseMove;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 테스트 윈도우 마우스 이동시 처리하기 - TestWindow_MouseMove(sender, e)

        /// <summary>
        /// 테스트 윈도우 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void TestWindow_MouseMove(object sender, MouseEventArgs e)
        {
            Point mousePoint = e.GetPosition(this);

            AddAnimationEllipse(mousePoint.X, mousePoint.Y);
        }

        #endregion
        #region 원 스토리보드 완료시 처리하기 - pEllipseStoryboard_Completed(sender, e)

        /// <summary>
        /// 원 스토리보드 완료시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void ellipseStoryboard_Completed(object sender, EventArgs e)
        {
            Storyboard storyboard = (sender as ClockGroup).Timeline as Storyboard;

            Ellipse ellipse = FindName(Storyboard.GetTargetName(storyboard)) as Ellipse;

            this.canvas.UnregisterName(ellipse.Name);

            this.canvas.Children.Remove(ellipse);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 애니메이션 원 추가하기 - AddAnimationEllipse(x, y)

        /// <summary>
        /// 애니메이션 원 추가하기
        /// </summary>
        /// <param name="x">X 좌표</param>
        /// <param name="y">Y 좌표</param>
        private void AddAnimationEllipse(double x, double y)
        {
            if((DateTime.Now.TimeOfDay - this.lastAddedTimeSpan).Milliseconds < 50)
            {
                return;
            }

            this.lastAddedTimeSpan = DateTime.Now.TimeOfDay;

            Ellipse ellipse = new Ellipse();

            ellipse.Stroke = GetRandomSolidColorBrush();

            int size = (Byte)this.random.Next(5, 15);

            ellipse.Width = ellipse.Height = size;

            ellipse.Name = "_" + ellipse.GetHashCode().ToString();

            Canvas.SetLeft(ellipse, x - size / 2);
            Canvas.SetTop (ellipse, y - size / 2);

            this.canvas.RegisterName(ellipse.Name, ellipse);

            this.canvas.Children.Add(ellipse);

            Storyboard ellipseStoryboard = (Resources["EllipseStoryBoardKey"] as Storyboard).Clone();

            Storyboard.SetTargetName(ellipseStoryboard, ellipse.Name);

            ellipseStoryboard.Completed += new EventHandler(ellipseStoryboard_Completed);

            ellipse.BeginStoryboard(ellipseStoryboard); 
        }

        #endregion
        #region 임의 SolidColorBrush 구하기 - GetRandomSolidColorBrush()

        /// <summary>
        /// 임의 SolidColorBrush 구하기
        /// </summary>
        /// <returns>임의 SolidColorBrush</returns>
        private SolidColorBrush GetRandomSolidColorBrush()
        {
            Color color = Color.FromArgb
            (
                (Byte)this.random.Next(80, 255),
                (Byte)this.random.Next( 0, 255),
                (Byte)this.random.Next( 0, 255),
                (Byte)this.random.Next( 0, 255)
            );

            return new SolidColorBrush(color);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요