■ CompositionTarget 클래스의 Rendering 정적 이벤트를 사용해 프레임당 간격으로 렌더링하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<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="TestProject" Background="FloralWhite" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="{x:Type Label}"> <Setter Property="HorizontalAlignment" Value="Right" /> <Setter Property="Foreground" Value="Maroon" /> </Style> </Window.Resources> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" TextWrapping="Wrap"> 아래 캔버스 객체 위로 마우스를 이동해 주시기 바랍니다.<LineBreak /> MouseMoveEvent 핸들러는 CompositionTarget.Rendering 핸들러에서 사용되는 좌표 값을 업데이트합니다. 캔버스의 배경색을 결정합니다. 아래 숫자는 경과 시간(초), 렌더링된 프레임 수 및 평균 프레임 속도를 보여줍니다. </TextBlock> <Grid Grid.Row="1" Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="경과 시간 (HH:mm:ss) :" /> <Label Grid.Row="1" Grid.Column="0" Content="프레임 수 :" /> <Label Grid.Row="2" Grid.Column="0" Content="평균 FPS :" /> <Label Name="stopwatchLabel" Grid.Row="0" Grid.Column="1" /> <Label Name="frameCounterLabel" Grid.Row="1" Grid.Column="1" /> <Label Name="frameRateLabel" Grid.Row="2" Grid.Column="1" /> </Grid> <Canvas Name="canvas" Grid.Row="2" Margin="0 10 0 0"/> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
using System; using System.Diagnostics; using System.Globalization; using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 스톱워치 /// </summary> private readonly Stopwatch stopwatch = new Stopwatch(); /// <summary> /// 프레임 카운터 /// </summary> private double frameCounter; /// <summary> /// 마우스 포인트 /// </summary> private Point mousePoint; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); CompositionTarget.Rendering += CompositionTarget_Rendering; canvas.MouseMove += canvas_MouseMove; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 컴포지션 타켓 렌더링시 처리하기 - CompositionTarget_Rendering(sender, e) /// <summary> /// 컴포지션 타켓 렌더링시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CompositionTarget_Rendering(object sender, EventArgs e) { if(this.frameCounter++ == 0) { this.stopwatch.Start(); } long frameRate = (long)(this.frameCounter / this.stopwatch.Elapsed.TotalSeconds); if(frameRate > 0) { this.stopwatchLabel.Content = this.stopwatch.Elapsed.ToString(); this.frameCounterLabel.Content = this.frameCounter.ToString(CultureInfo.InvariantCulture); this.frameRateLabel.Content = frameRate.ToString(); } byte red = (byte)(this.mousePoint.X / 3.0); byte blue = (byte)(this.mousePoint.Y / 2.0); this.canvas.Background = new SolidColorBrush(Color.FromRgb(red, 0, blue)); } #endregion #region 캔버스 마우스 이동시 처리하기 - canvas_MouseMove(sender, e) /// <summary> /// 캔버스 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvas_MouseMove(object sender, MouseEventArgs e) { this.mousePoint = e.GetPosition(sender as UIElement); } #endregion } } |