Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Click or DoubleClick

0.00/5 (No votes)
24 Sep 2013 1  
This is an alternative for Click or DoubleClick.

Introduction

I have created a custom panel to receive video from LibVLC (VideoLan). VLC only raises a Click event. This code can be easily transposed for other controls.

Using the code

The code raises click or double-click. It retrieves the left-handed parameter (also for the contextual click) and the double-click time, transmits the location of (double) click.

Imports System.Runtime.InteropServices

Public Class panelVideo
    Inherits System.Windows.Forms.Panel

    'timer du doubleclick
    Dim WithEvents TimerDoubleclick As New Timer
    'booleen pour savoir si il y a deja un click
    Dim firstclick As Boolean = False
    Dim droitier As Boolean = True

    'recuperation du temps limite du doubleclick
    <DllImport("user32.dll")> _
    Public Shared Function GetDoubleClickTime() As Integer
    End Function

    'recuperation de l'inversion des boutons de la souris
    '0   -> pas dinversion (droitier)
    '>0  -> inversion (gaucher) 
    <DllImport("user32.dll")> _
    Public Shared Function GetSystemMetrics(ByVal swapbutton As Integer) As Integer
    End Function

    Dim buttons As New MouseButtons
    Dim PointDown As Point
    Const WM_PARENTNOTIFY As Integer = &H210
    Public Shadows Event MouseDown(ByVal Sender As Object, ByVal e As MouseEventArgs)
    Public Shadows Event MouseDoubleClick(ByVal Sender As Object, ByVal e As MouseEventArgs)


    Public Enum MouseEvent
        WM_LBUTTONDOWN = &H201 'bouton gauche down
        WM_RBUTTONDOWN = &H204 'bouton left down
        WM_MBUTTONDOWN = &H207 'bouton Middle down
    End Enum


    'initialisation du timer à la construction du panel
    Public Sub New()
        TimerDoubleclick.Interval = GetDoubleClickTime()
        TimerDoubleclick.Enabled = True
        droitier = (GetSystemMetrics(23) = 0)
    End Sub

    Private Sub TimerDoubleClick_Tick() Handles TimerDoubleclick.Tick
        firstclick = False
        TimerDoubleclick.Stop()
        RaiseEvent MouseDown(Me, New MouseEventArgs(buttons, 2, PointDown.X, PointDown.Y, 0))
    End Sub


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = WM_PARENTNOTIFY Then
            Dim testclick As Integer = m.WParam.ToInt32
            If testclick = MouseEvent.WM_LBUTTONDOWN Or _
                testclick = MouseEvent.WM_MBUTTONDOWN Or _
                testclick = MouseEvent.WM_RBUTTONDOWN Then

                buttons = GetMouseButtonFlag(testclick)
                PointDown = New Point(m.LParam.ToInt32)

                If (buttons = MouseButtons.Left And droitier) OrElse _
                        (buttons = MouseButtons.Right And Not (droitier)) Then
                    If firstclick Then
                        firstclick = False
                        TimerDoubleclick.Stop()
                        RaiseEvent MouseDoubleClick(Me, _
                              New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
                    Else
                        firstclick = True
                        TimerDoubleclick.Start()
                    End If
                Else
                    firstclick = False
                    TimerDoubleclick.Stop()
                    RaiseEvent MouseDown(Me, New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
                End If
            End If
        ElseIf m.Msg = MouseEvent.WM_LBUTTONDOWN Or _
                 m.Msg = MouseEvent.WM_MBUTTONDOWN Or _
                 m.Msg = MouseEvent.WM_RBUTTONDOWN Then
            If m.WParam.ToInt32 = &H1 Then
                buttons = MouseButtons.Left
            End If
            If m.WParam.ToInt32 = &H2 Then
                buttons = MouseButtons.Right
            End If
            If m.WParam.ToInt32 = &H10 Then
                buttons = MouseButtons.Middle
            End If
            Dim PointDown As New Point(m.LParam.ToInt32)
            If (buttons = MouseButtons.Left And droitier) _
                     OrElse (buttons = MouseButtons.Right And Not (droitier)) Then
                If firstclick Then
                    firstclick = False
                    TimerDoubleclick.Stop()
                    RaiseEvent MouseDoubleClick(Me, _
                        New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
                Else
                    firstclick = True
                    TimerDoubleclick.Start()
                End If
            Else
                firstclick = False
                TimerDoubleclick.Stop()
                RaiseEvent MouseDown(Me, _
                    New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
            End If

        End If

        MyBase.WndProc(m)
    End Sub

    Private Shared Function GetMouseButtonFlag(ByVal wParam As Integer) As MouseButtons
        Select Case (wParam)
            Case MouseEvent.WM_LBUTTONDOWN
                Return MouseButtons.Left
            Case MouseEvent.WM_MBUTTONDOWN
                Return MouseButtons.Middle
            Case MouseEvent.WM_RBUTTONDOWN
                Return MouseButtons.Right
            Case Else
                Return MouseButtons.None
        End Select
    End Function
End Class

Points of Interest  

This code is more complete and more closed to the behavior of Windows.

License  

This code is a part of ZViewTV.NET, a GPL 2 software.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here