OnMouseMove does not fire on canvas in WPF

后端 未结 2 1613
时光说笑
时光说笑 2020-12-16 14:45

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 c

2条回答
  •  没有蜡笔的小新
    2020-12-16 15:27

    I ended up in writing a mouse global mouse handler like this:

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

提交回复
热议问题