using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace Sample_Performance_OnRender
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var squares = new List<Square>();
// 適当なシードで乱数を生成する
var randH = new Random(17280489);
var randV = new Random(399594);
// Canvas1
for (var i = 0; i < 200000; ++i)
{
var s = new Square();
Canvas.SetLeft(s, randH.Next(100000));
Canvas.SetTop(s, randV.Next(300));
s.Width = 5;
s.Height = 5;
xCanvas1.Children.Add(s);
squares.Add(s);
}
// Canvas2
xSquares.Objects = squares;
xSquares.InvalidateVisual();
}
}
}
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace Sample_Performance_OnRender
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var squares = new List<Square>();
// 適当なシードで乱数を生成する
var randH = new Random(17280489);
var randV = new Random(399594);
// Canvas1
for (var i = 0; i < 200000; ++i)
{
var s = new Square();
Canvas.SetLeft(s, randH.Next(100000));
Canvas.SetTop(s, randV.Next(300));
s.Width = 5;
s.Height = 5;
xCanvas1.Children.Add(s);
squares.Add(s);
}
// Canvas2
xSquares.Objects = squares;
xSquares.InvalidateVisual();
}
}
}
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Sample_Performance_OnRender
{
class Square : Control
{
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRectangle(Brushes.Crimson, new Pen(Brushes.Crimson, 1), new Rect(0, 0, Width, Height));
}
}
class Squares : Control
{
public List<Square> Objects;
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (Objects == null)
{
return;
}
foreach (Square s in Objects)
{
var rect = new Rect(Canvas.GetLeft(s), Canvas.GetTop(s), s.Width, s.Height);
drawingContext.DrawRectangle(Brushes.CadetBlue, new Pen(Brushes.CadetBlue, 1), rect);
}
}
}
}
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Sample_Performance_OnRender
{
class Square : Control
{
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRectangle(Brushes.Crimson, new Pen(Brushes.Crimson, 1), new Rect(0, 0, Width, Height));
}
}
class Squares : Control
{
public List<Square> Objects;
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (Objects == null)
{
return;
}
foreach (Square s in Objects)
{
var rect = new Rect(Canvas.GetLeft(s), Canvas.GetTop(s), s.Width, s.Height);
drawingContext.DrawRectangle(Brushes.CadetBlue, new Pen(Brushes.CadetBlue, 1), rect);
}
}
}
}
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Sample_Performance_OnRender
{
class Square : Control
{
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRectangle(Brushes.Crimson, new Pen(Brushes.Crimson, 1), new Rect(0, 0, Width, Height));
}
}
class Squares : Control
{
public ScrollViewer SV;
public List<Square> Objects;
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (SV == null || Objects == null)
{
return;
}
// ScrollViewerで表示されている領域
var viewRect = new Rect(SV.HorizontalOffset, SV.VerticalOffset, SV.ViewportWidth, SV.ViewportHeight);
foreach (Square s in Objects)
{
var rect = new Rect(Canvas.GetLeft(s), Canvas.GetTop(s), s.Width, s.Height);
// 四角形が表示領域内に含まれる場合のみ描画する
if (viewRect.IntersectsWith(rect))
{
drawingContext.DrawRectangle(Brushes.CadetBlue, new Pen(Brushes.CadetBlue, 1), rect);
}
}
}
}
}
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Sample_Performance_OnRender
{
class Square : Control
{
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRectangle(Brushes.Crimson, new Pen(Brushes.Crimson, 1), new Rect(0, 0, Width, Height));
}
}
class Squares : Control
{
public ScrollViewer SV;
public List<Square> Objects;
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (SV == null || Objects == null)
{
return;
}
// ScrollViewerで表示されている領域
var viewRect = new Rect(SV.HorizontalOffset, SV.VerticalOffset, SV.ViewportWidth, SV.ViewportHeight);
foreach (Square s in Objects)
{
var rect = new Rect(Canvas.GetLeft(s), Canvas.GetTop(s), s.Width, s.Height);
// 四角形が表示領域内に含まれる場合のみ描画する
if (viewRect.IntersectsWith(rect))
{
drawingContext.DrawRectangle(Brushes.CadetBlue, new Pen(Brushes.CadetBlue, 1), rect);
}
}
}
}
}
ディスカッション
コメント一覧
まだ、コメントがありません