OnMouseMove不会在WPF的画布上触发

OnMouseMove不会在WPF的画布上触发

本文介绍了OnMouseMove不会在WPF的画布上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了我的自定义图表控件,我想在光标后面绘制一个简单的十字。该图表在Canvas上实现为PolyLine,我正在绘制两行改变Canvas OnMouseMove事件的坐标。



令人惊奇的是发现当MainGUI线程处于空闲状态(UI完全响应,如果我暂停应用程序主线程在应用程序 mainForm.ShowDialog())



任何想法如何找到为什么会发生这种情况?我使用OnMouseMove或PreviewOnMouseMove获得相同的性能。



编辑:这是我画十字架的方式(无论如何,如果我在OnMouseMove上放置一个断点,时间)



XAML:

 <边框边框厚度=1BorderBrush =WhiteGrid.Column =1Grid.Row =0Grid.RowSpan =2Grid.ColumnSpan =2> 
< Canvas x:Name =DrawAreaPreviewMouseMove =DrawArea_PreviewMouseMove/>
< / Border>

CS:

 code> 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;
}


解决方案

这是wierd,我不知道为什么...



FrameworkElement.MouseMove 只有当该地区有一些明确的背景画笔\ fill set



在你的情况下,设置 Canvas.Background =Transparent,它应该可以工作。



此外还有另一个修复程序... 之后不发送MouseMove事件



这可能是因为 HitTest 取决于彩色像素及其反馈。



无论是什么,它的东西都没有记录在MSDN上,并且让许多UI设计师感到困惑。 $ b

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;
}
解决方案

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.

这篇关于OnMouseMove不会在WPF的画布上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:21