Missing MouseDown/up using DrawingVisual

Ajeje Brazorf 106 Reputation points
2021-10-14T11:03:52.297+00:00

I'm building a custom chart app using drawingvisual; I draw the chart with lines and the highlight some regions with rectangles; also added a Vertical line as a cursor/mousepointer; The user can grab the point and change it so I manage to use Mouse Events. MouseMove Work fine, the Up/Down work only when I click outside this rectangles; I done some debug and find that if I comment the call to redraw the screen (that I call at the end of the each mouse event), from mousemove, I did not see the grabbing and the cursor, but the event Up/down start to fire. Any advice?

I don't use HitTest, I Get the coordinate on the canvas, mod the data and then redraw the chart.
My VisualHost Class is very similar to this one, but in vb.net and more things to draw:

    Public Class VisualHost

    Inherits FrameworkElement

    Dim Visual As New DrawingVisual()
    Dim Context As DrawingContext
    Dim Pen As Pen

    Dim rand As New Random

    Public Sub New()

        Pen = New Pen(Brushes.Black, 0.25)
        Pen.Freeze()

        AddVisualChild(Visual)

    End Sub

    Public Sub Draw()

        Dim Geometry As New StreamGeometry()

        Using GeometryContext As StreamGeometryContext = Geometry.Open()

            GeometryContext.BeginFigure(New Point(rand.NextDouble * 500, rand.NextDouble * 500), False, False)

            For i As Integer = 1 To 1000

                GeometryContext.LineTo(New Point(rand.NextDouble * 500, rand.NextDouble * 500), True, True)

            Next

        End Using

        Geometry.Freeze()

        Using Context = Visual.RenderOpen()
            Context.DrawGeometry(Nothing, Pen, Geometry)
        End Using

    End Sub

    Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
        Get
            Return 1
        End Get
    End Property

    Protected Overrides Function GetVisualChild(index As Integer) As Visual
        Return Visual
    End Function

End Class




    Public Shared Sub RefreshDraw()
            Dim Host As New VisualHost
            MainWindow.myGrah2d.pbx.Children.Clear()
            MainWindow.myGrah2d.pbx.Children.Add(Host)
            Host.Draw()
        End Sub

     Private Sub pbx_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
                    ' System.Diagnostics.Debug.WriteLine("Right left Down")
    'Do Other Stuff
                   'RefreshDraw()
        End Sub

    Private Sub pbx_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
                    ' System.Diagnostics.Debug.WriteLine("Mouse is Moving")
    'Do Other Stuff and Calculations
                   'RefreshDraw()
        End Sub
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,678 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
{count} votes