![[C#] 21일차 - 159. WPF DispatcherTimer와 깜박이는 프로그램](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FsnGWS%2FbtqYE7uJWsi%2Fj0kNkDq3MJeygFvaYSI8ck%2Fimg.png)
[C#] 21일차 - 159. WPF DispatcherTimer와 깜박이는 프로그램C#/C#200제2021. 3. 1. 16:23
Table of Contents
WPF는 타이머 기능을 DispatcherTimer를 사용해 구현한다.
<Window x:Class="A159_WPF_Blinker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:A159_WPF_Blinker"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="170"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Name="btnGreen" Background="Green" Margin="40" Click="btnGreen_Click"></Button>
<Button Name="btnRed" Grid.Column="1" Background="Red" Margin="40" Click="btnRed_Click"></Button>
<TextBlock Grid.Row="1" FontSize="20" HorizontalAlignment="Center">Strat Blinking!</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" FontSize="20" HorizontalAlignment="Center">Stop Blinking!</TextBlock>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; // DispatcherTimer
namespace A159_WPF_Blinker
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer t = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
t.Interval = new TimeSpan(5000000); // 단위 Tick, 0.5초
t.Tick += T_Tick;
}
private void T_Tick(object sender, EventArgs e)
{
if(btnRed.Background == Brushes.Red)
{
btnRed.ClearValue(Button.BackgroundProperty);
btnGreen.Background = Brushes.Green;
}else
{
btnGreen.ClearValue(Button.BackgroundProperty);
btnRed.Background = Brushes.Red;
}
}
private void btnGreen_Click(object sender, RoutedEventArgs e)
{
t.Start();
}
private void btnRed_Click(object sender, RoutedEventArgs e)
{
t.Stop();
}
}
}
반응형
'C# > C#200제' 카테고리의 다른 글
[C#] 22일차 - 161. WinForm 윈도우 표준 계산기 (0) | 2021.03.05 |
---|---|
[C#] 22일차 - 160. WPF 간단한 계산기 (0) | 2021.03.05 |
[C#] 21일차 - 157. WPF의 레이아웃 (0) | 2021.03.01 |
[C#] 21일차 - 156. WPF로 Hello World 프로그램 만들기 (0) | 2021.03.01 |
[C#] 21일차 - 155. GDH+와 ToolStrip, StatusStrip을 사용한 그래픽 프로그램 (0) | 2021.02.25 |
@반나무 :: 반나무_뿌리
3년차 WPF 개발자입니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!