的OnMouseMove不会触发在WPF画布 [英] OnMouseMove does not fire on canvas in WPF

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

问题描述

我已经做了我的自定义图表控件,我想画一个简单的交叉光标以下。图表是作为一个折线在一个画布,我画两条线在画布的OnMouseMove事件改变自己的坐标。



令人惊讶的是发现,当MainGUI线程空闲时被调用只有各10秒左右事件(UI是完全响应,如果我暂停应用程序的主线程的应用程序 mainForm.ShowDialog())



如何找到任何想法,为什么会出现这种情况?我开始使用的OnMouseMove或PreviewOnMouseMove相同的性能。



编辑:这是我画的十字架(反正如果我把在的OnMouseMove它只能从停止断点。不时)



XAML:

 < BORDER了borderThickness =1BorderBrush =白Grid.Column =1Grid.Row =0Grid.RowSpan =2Grid.ColumnSpan =2> 
<帆布X:NAME =DrawAreaPreviewMouseMove =DrawArea_PreviewMouseMove/>
< /边框>



CS:

 公共图()
{
_line =新折线();
_line.Stroke = Brushes.Orange;
_crossX =新线();
_crossY =新线();
_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);
}

私人无效DrawArea_MouseMove(对象发件人,MouseEventArgs E)
{
点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;
}


解决方案

这是奇怪的,我不知道为什么...



FrameworkElement.MouseMove 适用于当地区有\填充设置一些明确的背景画笔



在你的情况设置 Canvas.Background =透明,它应该工作。



有另一种修复这个以及...的 WPF不发送的MouseMove事件CaptureMouse()之后;



这可能是可能是因为的HitTest 取决于颜色的像素和他们的反馈意见。



不管是什么,它​​的东西没有记录在MSDN和是混淆了很多UI设计师。


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画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆