■ 레이블 깜박이기

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


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="600"

    Height="450"

    Title="레이블 깜박이기"

    FontFamily="나눔고딕코딩"

    FontSize="20"

    Loaded="Window_Loaded">

    <Window.Resources>

        <Storyboard x:Key="blinkStoryboardKey"

            RepeatBehavior="Forever"

            Duration="0:0:1">

            <ColorAnimationUsingKeyFrames

                Storyboard.TargetName="storyboardLabel"

                Storyboard.TargetProperty="Background.(SolidColorBrush.Color)">

                <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Black" />

                <DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="White" />

            </ColorAnimationUsingKeyFrames>

            <ColorAnimationUsingKeyFrames

                Storyboard.TargetName="storyboardLabel"

                Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)">

                <DiscreteColorKeyFrame KeyTime="0:0:0" Value="White" />

                <DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="Black" />

            </ColorAnimationUsingKeyFrames>

        </Storyboard>

    </Window.Resources>

    <Window.Triggers>

        <EventTrigger RoutedEvent="FrameworkElement.Loaded">

            <BeginStoryboard Storyboard="{StaticResource blinkStoryboardKey}"/>

        </EventTrigger>

    </Window.Triggers>

    <StackPanel Width="150" Margin="100">

        <Label Name="timerLabel"

            HorizontalAlignment="Center"

            Content="Timer" />

        <Label Name="storyboardLabel"

            HorizontalAlignment="Center"

            Content="Storyboard" />

    </StackPanel>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Windows;

using System.Windows.Media;

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 bool isBlinking = false;

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 윈도우 로드시 처리하기

        /// </summary>

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

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

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            this.timer = new DispatcherTimer();

 

            this.timer.Interval = new TimeSpan(0, 0, 0, 0, 500);

 

            this.timer.Tick += timer_Tick;

 

            this.timer.Start();

        }

 

        #endregion

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

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

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

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

        private void timer_Tick(object sender, EventArgs e)

        {

            if(this.isBlinking)

            {

                this.timerLabel.Foreground = Brushes.Black;

                this.timerLabel.Background = Brushes.White;

            }

            else

            {

                this.timerLabel.Foreground = Brushes.White;

                this.timerLabel.Background = Brushes.Black;

            }

 

            this.isBlinking = !this.isBlinking;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요