私が抱えている問題を示すために、曲線とカーソルに沿った黒い線を描く以下の簡単な例をコーディングしました。
「numPointsMul」を増やすと、マウスとの相互作用とカーソル線の描画が非常に遅くなる理由を誰かが説明できますか?
グラフ内のポイントの数がこのようにポインターの描画に影響するのはなぜですか? すべてのヒット テストをオフにしたことに注意してください。グラフは でのみ再描画されていOnRender
ます。
ポイントの数を増やすと関数の時間が長くなることがわかりますが、カーソルが移動したときに呼び出されずに無効化イベントに呼び出されるOnRender
ため、ポインターの描画が遅れる理由がわかりません。OnRender
ヒットテストはまだ行われているようですか?
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="800" Width="1024" WindowStartupLocation="CenterScreen">
<local:UserControl1/>
</Window>
<UserControl x:Class="WpfApplication4.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
IsEnabled="False" IsHitTestVisible="False">
<Line Name="m_line" Stroke="Black"/>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Application.Current.MainWindow.PreviewMouseMove += new MouseEventHandler(MainWindow_PreviewMouseMove);
}
void MainWindow_PreviewMouseMove(object sender, MouseEventArgs e)
{
m_line.X1 = m_line.X2 = Mouse.GetPosition(this).X;
m_line.Y1 = 0;
m_line.Y2 = ActualHeight;
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRectangle(Brushes.Green, null, new Rect(0, 0, ActualWidth, ActualHeight));
Pen p = new Pen(Brushes.Black, 1);
StreamGeometry streamGeometry = new StreamGeometry();
using (StreamGeometryContext geometryContext = streamGeometry.Open())
{
double numPointsMul = 1;
int count = (int)(ActualWidth * numPointsMul);
for (int i = 0; i < count; ++i)
{
double y = ActualHeight / 2 + 20 * Math.Sin(i * 0.1);
if (i == 0)
{
geometryContext.BeginFigure(new Point(i , y), true, true);
}
else
{
geometryContext.LineTo(new Point(i, y), true, true);
}
}
}
drawingContext.DrawGeometry(Brushes.Red, p, streamGeometry);
}
}