Recent Posts
Recent Comments
Link
관리 메뉴

NaggingMachine

WPF 타이머 생성하기 (Timer in WPF) 본문

TechnoBabbler

WPF 타이머 생성하기 (Timer in WPF)

naggingmachine 2006. 10. 15. 16:51



I want to make a sample program to try the WPF by controlling contols' angle as times go by. I need to know the following technologies.



1. Timer

2. Controlling a control's angle



1. You can create a System.Windows.Threading.DispatcherTimer object. Whenever the tick event is occurred, the tick event handler will be called



2. To change the contol's angle I just examined the xmal file. Xmal file has all the information of the design. Angle value can be set at Angle attribute of RotateTransform element. But you cannot set the angle value by using the btnRotate.TransformGroup.RotateTransform.Angle = 50. You should make a TransformGroup and RotateTransform object.



Source codes are like followings.



WPF를 처음으로 다루어보면서 우선 간단하게 나마 컨트롤이 회전하는 응용 프로그램을 만들어보고 싶었다. 필요한 기술은 다음과 같다.



1. 타이머

2. 컨트롤의 각도 변경



1. Timer는 System.Windows.Threading.DispatcherTimer 객체를 이용해서 생성이 가능하다. 일반적인 타이머와 마찬가지로 EventHandler를 설치하여 사용할 수 있다.



2. 컨트롤의 각도에 영향을 미치는 값이 무엇인지를 확인하기 위하여 디자인에 사용한 xmal 파일을 살펴보았는데, 각도 값은 RotateTransform 엘리먼트의 Angle 애트리뷰트로 설정이 가능하다. 코드에서는 btnRotate.TransformGroup.RotateTransform.Angle = 50 과 같이 설정할 수 없고 대신 TransformGroup과 RotateTransform 객체를 만들어서 설정해야 한다.



전체 소스 코드는 다음과 같다.



Scene1.xmal



<Grid

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"

mc:Ignorable="d"

Background="#FFFFFFFF"

x:Name="DocumentRoot"

x:Class="UntitledProject1.Scene1"

Width="640" Height="480">

<Grid.Resources>

<Storyboard x:Key="OnLoaded"/>

</Grid.Resources>

<Grid.Triggers>

<EventTrigger RoutedEvent="FrameworkElement.Loaded">

  <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>

</EventTrigger>

</Grid.Triggers>



<Grid.ColumnDefinitions>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition/>

</Grid.RowDefinitions>

<Button HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="324,0,261,104" Width="Auto" Height="32" x:Name="btnRotate" RenderTransformOrigin="0.5,0.5" Content="Button">

<Button.RenderTransform>

  <TransformGroup>

   <TranslateTransform X="0" Y="0"/>

   <ScaleTransform ScaleX="1" ScaleY="1"/>

   <SkewTransform AngleX="0" AngleY="0"/>

   <RotateTransform Angle="-776.032282436724"/>

   <TranslateTransform X="0" Y="0"/>

   <TranslateTransform X="0" Y="0"/>

  </TransformGroup>

</Button.RenderTransform>

</Button>

<TextBox VerticalAlignment="Top" Margin="268,133,225,0" Height="27" x:Name="txtRotate" Text="TextBox" TextWrapping="Wrap"/>

</Grid>



Scene1.xmal.cs



using System;

using System.IO;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Navigation;

namespace UntitledProject1

{

public partial class Scene1

{

private TransformGroup group;

private RotateTransform rotate;



private int angle = 0;



public Scene1()

{

  this.InitializeComponent();

  // Insert code required on object creation below this point.

 

  group = new TransformGroup();

  rotate = new RotateTransform();

  group.Children.Add(rotate);

 

  btnRotate.RenderTransform = group;

  txtRotate.RenderTransform = group;

 

  StartTimer();

}



private void StartTimer( )

{

  System.Windows.Threading.DispatcherTimer TimerClock = new System.Windows.Threading.DispatcherTimer();

  TimerClock.Interval = new TimeSpan ( 0, 0, 0, 0, 200 ); // 200 milliseconds

  TimerClock.IsEnabled = true;

  TimerClock.Tick += new EventHandler(TimerClock_Tick);

}



void TimerClock_Tick ( object sender, EventArgs e )

{

  // Rotate  controls

  angle += 10;

  rotate.Angle = angle;

}



}

}



Source Code:

'TechnoBabbler' 카테고리의 다른 글

기쁘다, WPF  (0) 2006.10.19
TimeTracker with WPF  (0) 2006.10.16
앞으로 3개월 동안 WPF만 공부  (0) 2006.10.02
WPF Killer Application, TimesReader!  (0) 2006.09.26
Install the lastest Vista, Build #5728  (0) 2006.09.26