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

728x90
반응형

TestProject.zip
3.97MB

▶ DripAnimationControl.xaml

<UserControl Name="userControl" x:Class="TestProject.DripAnimationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Canvas
        Width="{Binding ElementName=userControl, Path=ActualWidth}"
        Height="{Binding ElementName=userControl, Path=ActualHeight}"
        ClipToBounds="True">
        <Canvas Name="animationCanvas"
            Width="{Binding ElementName=userControl, Path=ActualWidth}"
            Height="{Binding ElementName=userControl, Path=ActualHeight}" />
        <Image Name="image"
            Width="{Binding ElementName=userControl, Path=ActualWidth}"
            Height="{Binding ElementName=userControl, Path=ActualHeight}"
            Stretch="Fill" />
    </Canvas>
</UserControl>

 

728x90

 

▶ DripAnimationControl.xaml.cs

ng System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace TestProject
{
    /// <summary>
    /// 물방울 애니메이션 컨트롤
    /// </summary>
    public partial class DripAnimationControl : UserControl
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 이미지 속성 - ImageSourceProperty

        /// <summary>
        /// 이미지 소스 속성
        /// </summary>
        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register
        (
            "ImageSource",
            typeof(ImageSource),
            typeof(DripAnimationControl),
            new UIPropertyMetadata
            (
                null,
                null,
                null,
                false
            )
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

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

        /// <summary>
        /// 임시 이미지
        /// </summary>
        private Image temporaryImage = null;

        /// <summary>
        /// 왼쪽 위치
        /// </summary>
        private double leftPosition = 0;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 이미지 소스 - ImageSource

        /// <summary>
        /// 이미지 소스
        /// </summary>
        public ImageSource ImageSource
        {
            get
            {
                return (ImageSource)GetValue(ImageSourceProperty);
            }
            set
            {
                SetValue(ImageSourceProperty, value);
            }
        }

        #endregion

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

        #region 생성자 - DripAnimationControl()

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

            this.image.Visibility           = Visibility.Collapsed;
            this.animationCanvas.Visibility = Visibility.Collapsed;

            Loaded += UserControl_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public
        //////////////////////////////////////////////////////////////////////////////// Function

        #region 시작하기 - Start()

        /// <summary>
        /// 시작하기
        /// </summary>
        public void Start()
        {
            if(ImageSource == null)
            {
                return;
            }

            if(this.timer.IsEnabled)
            {
                return;
            }

            this.leftPosition = 0;

            this.image.Visibility           = Visibility.Visible;
            this.animationCanvas.Visibility = Visibility.Visible;

            this.timer.Start();
        }

        #endregion
        #region 중단하기 - Stop()

        /// <summary>
        /// 중단하기
        /// </summary>
        public void Stop()
        {
            if(ImageSource == null)
            {
                return;
            }

            if(!this.timer.IsEnabled)
            {
                return;
            }

            this.timer.Stop();
        }

        #endregion
        
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 사용자 컨트롤 로드시 처리하기 - UserControl_Loaded(sender, e)

        /// <summary>
        /// 사용자 컨트롤 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.timer = new DispatcherTimer();

            this.timer.Interval = TimeSpan.FromSeconds(0.03);

            this.timer.Tick += new EventHandler(timer_Tick);

            this.temporaryImage = new Image();

            this.temporaryImage.Width   = ActualWidth;
            this.temporaryImage.Height  = ActualHeight;
            this.temporaryImage.Stretch = Stretch.Fill;
            this.temporaryImage.Source  = ImageSource;
            this.temporaryImage.Clip    = new RectangleGeometry(new Rect(10, 0, 2, ActualHeight));

            this.image.Source = ImageSource;

            VisualBrush visualBrush = new VisualBrush(this.temporaryImage as Visual);

            this.animationCanvas.Background = visualBrush;

            Canvas.SetZIndex(this.animationCanvas, 3);
            Canvas.SetZIndex(image, 2);
        }

        #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.temporaryImage.Clip = new RectangleGeometry(new Rect(this.leftPosition, 0, 2, ActualHeight));

            VisualBrush visualBrush = new VisualBrush(this.temporaryImage as Visual);

            this.animationCanvas.Background = visualBrush;

            Canvas.SetLeft(this.animationCanvas, this.leftPosition);

            this.leftPosition++;

            if(this.leftPosition >= ActualWidth)
            {
                this.timer.Stop();
            }
        }

        #endregion
    }
}

 

300x250

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="이미지 물방울 애니메이션(Drip Animation) 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Border
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            BorderThickness="1"
            BorderBrush="DarkGray">
            <local:DripAnimationControl x:Name="dripAnimationControl"
                Width="600"
                Height="400"
                ImageSource="IMAGE\sample.png" />
        </Border>
        <StackPanel
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Margin="10"
            Orientation="Horizontal">
            <Button Name="startButton"
                Width="100"
                Height="30"
                Content="Start" />
            <Button Name="stopButton"
                Margin="10 0 0 0"
                Width="100"
                Height="30"
                Content="Stop" />
        </StackPanel>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System.Windows;

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

        #region 생성자 - MainWindow()

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

            this.startButton.Click += startButton_Click;
            this.stopButton.Click  += stopButton_Click;
        }

        #endregion

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

        #region 시작 버튼 클릭시 처리하기 - startButton_Click(sender, e)

        /// <summary>
        /// 시작 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            this.startButton.IsEnabled = false;
            this.stopButton.IsEnabled  = true;

            this.dripAnimationControl.Start();
        }

        #endregion
        #region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e)

        /// <summary>
        /// 중단 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void stopButton_Click(object sender, RoutedEventArgs e)
        {
            this.startButton.IsEnabled = true;
            this.stopButton.IsEnabled  = false;

            this.dripAnimationControl.Stop();
        }

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

댓글을 달아 주세요