mouse event not fire on shapes

Damien.darhk

Member
Joined
Aug 13, 2021
Messages
9
Programming Experience
3-5
I'm new in wpf, I need to recreate the behavior of an old winform drawing app.
In the winform app I used this event
MouseMove
MouseClick
MouseDown
MouseUp
Basically there are 2 main behavior: single click managed from MouseClick(that contains drawing logic) and "press and move" managed by MouseDown (set a flag) MouseUp(clear a falg) and Mouse Move(that contains the drawing logic).
In saw that in WPF there are some changes so after few test I used on the canvas:
PreviewMouseDown to set the flag,
PreviewMouseMove as MouseMove in winform,
PreviewMouseUp to clear the flag and to handle the "sigle click".
So, I did not like too much to drawing with the PreviewMouseUp because for user experience seem lagging, but this is not the main problem:
I checked in debug(also putting a System.Diagnostics.Debug.WriteLine("...") in the mouses event), and I have several missing fire for both PreviewMouseDown and PreviewMouseUp; I did not found any logic in this behavior,at first I would say they are random, but then I realize that I've this missing event when I click on other shapes drawed on the canvas (rectangles for example doesn't matter if alpha color is 255 or not).
I've to say also that the canvas is inside an usercontrol, and that the PreviewMouseMove work correctly.
I've find the msdn page about it but I've not fully understand it. anyone have experiece similar iusses? Is my approach wrong?
I'm building a minimal example that I'll post soon if needed.
 
THIS IS AN EXAMPLE, HOPE IT HELPS.

Mouse Event Handling in WPF Canvas:
Public Class CustomCanvas
    Inherits UserControl

    Private isDrawing As Boolean = False

    Public Sub New()
        InitializeComponent()
        AddHandler Me.PreviewMouseDown, AddressOf OnPreviewMouseDown
        AddHandler Me.PreviewMouseMove, AddressOf OnPreviewMouseMove
        AddHandler Me.PreviewMouseUp, AddressOf OnPreviewMouseUp
    End Sub

    Private Sub OnPreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
        isDrawing = True
        ' Drawing logic here
        System.Diagnostics.Debug.WriteLine("Mouse Down")
    End Sub

    Private Sub OnPreviewMouseMove(sender As Object, e As MouseEventArgs)
        If isDrawing Then
            ' Drawing logic here
            System.Diagnostics.Debug.WriteLine("Mouse Move")
        End If
    End Sub

    Private Sub OnPreviewMouseUp(sender As Object, e As MouseButtonEventArgs)
        isDrawing = False
        ' Handle single click logic here
        System.Diagnostics.Debug.WriteLine("Mouse Up")
    End Sub
End Class
 
Back
Top