繁体   English   中英

在WPF中OnMouseMove不会在画布上触发

[英]OnMouseMove does not fire on canvas in WPF

我已经完成了自定义图表控件,我想在光标之后绘制一个简单的十字。 该图表被实现为Canvas上的折线,并且我在Canvas的OnMouseMove事件中绘制了两条更改其坐标的线。

令人惊讶的是,当MainGUI线程处于空闲状态时(UI完全响应,如果我暂停应用程序,则主线程位于App mainForm.ShowDialog()) ,该事件仅每10秒左右调用一次。

关于如何找到这种情况的任何想法? 使用OnMouseMove或PreviewOnMouseMove可以获得相同的性能。

编辑:这就是我绘制十字架的方式(无论如何,如果我在OnMouseMove上放置一个断点,它只会不时停止)。

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

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

仅当区域具有一些显式背景画笔\\填充集时, FrameworkElement.MouseMove起作用。

在您的情况下,设置Canvas.Background="Transparent"

WPF也不会在CaptureMouse()之后发送MouseMove事件。

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

不管是什么,它都没有在MSDN上进行记录,这对于许多UI设计人员来说都是令人困惑的。

我最终编写了这样的鼠标全局鼠标处理程序:

using System.Windows.Interop;

private const int WM_MOUSEMOVE = 0x0200;
public delegate void Del_MouseMovedEvent(Point mousePosition);

// Relative to this control, the mouse position will calculated
public IInputElement Elmt_MouseMovedRelativeElement = null;

// !! This is static; needs special treatment in a multithreaded application !!
public static event Del_MouseMovedEvent Evt_TheMouseMoved = null;

// your main function call
public MyMainWindows()
{
    // install the windows message filter first
    ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;

    InitializeComponent();

    ...
}   

// filtering the windows messages
private void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled)
{
    if(msg.message == WM_MOUSEMOVE)
    {
        this.Evt_TheMouseMoved?.Invoke(Mouse.GetPosition(this.Elmt_MouseMovedRelativeElement));
    }
}

// individual event for mouse movement
private void MyMouseMove(Point mousePoint)
{
    // called on every mouse move when event is assigned
    Console.WriteLine(mousePoint.X + " " + mousePoint.Y);
}

private void AnyFunctionDeeperInTheCode()
{
    // assign the handler to the static var of the main window
    MyMainWindows.Evt_TheMouseMoved += MyMouseMove;

    // set the element / control to which the mouse position should be calculated; 
    MyMainWindows.Elmt_MouseMovedRelativeElement = this;

    ...

    // undassign the handler from the static var of the main window
    MyMainWindows.Evt_TheMouseMoved -= MyMouseMove;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM