79768664

Date: 2025-09-18 15:55:21
Score: 0.5
Natty:
Report link

Consider performing a HitTest to accurately determine when the mouse enters your Canvas bounds

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MouseMove += OnMouseMove;
    }

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        // Retrieve the coordinate of the mouse position.
        Point point = e.GetPosition((UIElement)sender);

        // Perform the hit test against a given portion of the visual object tree.
        HitTestResult hittestresult = VisualTreeHelper.HitTest(CanvasControl, point);

        if (hittestresult != null)
        {
            // Perform action on hit visual object.
        }
    }
}

Take the time and read this fantastic document which details how to HitTest the Visual layer

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/hit-testing-in-the-visual-layer

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Dragan Radovac