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
Dim WithEvents TimerDoubleclick As New Timer
Dim firstclick As Boolean = False
Dim droitier As Boolean = True
<DllImport("user32.dll")> _
Public Shared Function GetDoubleClickTime() As Integer
End Function
<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 WM_RBUTTONDOWN = &H204 WM_MBUTTONDOWN = &H207 End Enum
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.