Click here to Skip to main content
15,881,866 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Click or DoubleClick

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Sep 2013GPL3 11.6K   2  
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.

VB.NET
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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --