728x90
728x170
▶ 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">
<Rectangle Name="rectangle" Canvas.Left="0" Canvas.Top="200"
Width="100"
Height="100"
Fill="Orange" />
<Button Name="button" Canvas.Right="10" Canvas.Bottom="10"
Width="100"
Height="30"
Content="실행" />
</Canvas>
</Window>
728x90
▶ 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;
#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 left = Canvas.GetLeft(this.rectangle);
double target = this.canvas.ActualWidth - 5;
if(left + this.rectangle.Width > target)
{
this.timer.Stop();
return;
}
// 개체 X축 = 개체 X축 + 0.1 * (종착 X축 - 개체 X축)
left = left + 0.1 * (target - left);
Canvas.SetLeft(this.rectangle, left);
}
#endregion
#region 실행 버튼 클릭시 처리하기 - button_Click(sender, e)
/// <summary>
/// 실행 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void button_Click(object sender, RoutedEventArgs e)
{
Canvas.SetLeft(this.rectangle, 0);
this.timer.Start();
}
#endregion
}
}
728x90
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] ScaleTransform 클래스 사용하기 (0) | 2020.11.24 |
---|---|
[C#/WPF] RotateTransform 클래스 사용하기 (0) | 2020.11.24 |
[C#/WPF] 확대/축소/이동 가능한 캔버스 만들기 (0) | 2020.11.15 |
[C#/WPF] DispatcherTimer 클래스 : 원 형태으로 이동시키기 (0) | 2020.11.15 |
[C#/WPF] DispatcherTimer 클래스 : 파동 형태로 이동시키기 (0) | 2020.11.15 |
[C#/WPF] Control 엘리먼트 : Background 속성에서 null과 Transparent 값 설정시 차이점 비교하기 (0) | 2020.11.06 |
[C#/WPF] LinearGradientBrush 클래스 : 오프셋 값을 사용해 색상 구하기 (0) | 2020.10.28 |
[C#/WPF] 누겟 설치 : FFME.Windows (0) | 2020.10.25 |
[C#/WPF] 커스텀 윈도우 크롬 사용하기 (0) | 2020.10.13 |
[C#/WPF] 파노라마 뷰 사용하기 (0) | 2020.09.24 |