Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c# - OnMouseMove does not fire on canvas in WPF

I have done my custom chart control and I want to draw a simple cross following the cursor. The chart is implemented as a PolyLine over a Canvas and I'm drawing two lines changing their coordinates at the OnMouseMove event of the Canvas.

The surprise was to found that the event get called only each 10 seconds or so event when the MainGUI thread is idle (the UI is completely responsive and if I pause the application the main thread is at the App mainForm.ShowDialog()).

Any idea on how to find why is this happening? I get the same performance using the OnMouseMove or the PreviewOnMouseMove.

EDIT: This is how I paint the cross (anyway if I put a breakpoint at the OnMouseMove it only stops from time to time).

XAML:

<Border BorderThickness="1" BorderBrush="White" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" >
    <Canvas x:Name="DrawArea" PreviewMouseMove="DrawArea_PreviewMouseMove" />
</Border>

CS:

 public Chart()
 {
    _line = new Polyline();
    _line.Stroke = Brushes.Orange;
    _crossX = new Line();
    _crossY = new Line();
    _crossX.Stroke = Brushes.Orange;
    _crossY.Stroke = Brushes.Orange;
    _crossX.StrokeThickness = 1;
    _crossY.StrokeThickness = 1;

    InitializeComponent();

    this.DrawArea.Children.Add(_line);
    this.DrawArea.Children.Add(_crossX);
    this.DrawArea.Children.Add(_crossY);
}     

private void DrawArea_MouseMove(object sender, MouseEventArgs e)
{
    Point mousePosition = System.Windows.Input.Mouse.GetPosition(this.DrawArea);

    _crossX.X1 = 0;
    _crossX.X2 = this.DrawArea.ActualWidth;
    _crossX.Y1 = _crossX.Y2 = mousePosition.Y;

    _crossY.Y1 = 0;
    _crossY.Y2 = this.DrawArea.ActualHeight;
    _crossY.X1 = _crossY.X2 = mousePosition.X;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is wierd and I dont know why...

FrameworkElement.MouseMove works only if the region has some explicit background brush fill set.

In your case set the Canvas.Background="Transparent", it should work.

There is another fix to this as well... WPF Not sending MouseMove events after CaptureMouse();

This could be possibly because the HitTest depends upon colored pixels and their feedback.

Whatever it is, its something not documented over MSDN and is confusing for many UI designers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...