Public Class NoContextNumericUpDown
Inherits NumericUpDown
Private Const WH_GETMESSAGE As Integer = 3
Private Const WM_CONTEXTMENU As UShort = &H7B
Private Const WM_RBUTTONDOWN As UShort = &H204
Private Const WM_RBUTTONUP As UShort = &H205
Private Const WM_RBUTTONDBLCLK As UShort = &H206
Private hhook As IntPtr
Private fook As HookProc
Private Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
Private Class MSG
Public hwnd As IntPtr
Public message As UShort
Public wParam As IntPtr
Public lParam As IntPtr
Public time As UInt32
Public point_x As Integer
Public point_y As Integer
End Class
Public Sub New()
If Not DesignMode Then
fook = New HookProc(AddressOf CallGetMsgProc)
Me.hhook = NoContextNumericUpDown.SetWindowsHookEx(WH_GETMESSAGE, fook, IntPtr.Zero, GetCurrentThreadId)
If Me.hhook = 0 Then Throw New Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("x"))
End If
MyBase.ContextMenu = Nothing
MyBase.ContextMenuStrip = Nothing
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If Me.hhook <> IntPtr.Zero Then
NoContextNumericUpDown.UnhookWindowsHookEx(Me.hhook)
hhook = IntPtr.Zero
End If
MyBase.Dispose(disposing)
End Sub
Public Overrides Property ContextMenu() As System.Windows.Forms.ContextMenu
Get
Return Nothing
End Get
Set(ByVal value As System.Windows.Forms.ContextMenu)
End Set
End Property
Public Overrides Property ContextMenuStrip() As System.Windows.Forms.ContextMenuStrip
Get
Return Nothing
End Get
Set(ByVal value As System.Windows.Forms.ContextMenuStrip)
End Set
End Property
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_CONTEXTMENU Then m.Msg = 0
MyBase.DefWndProc(m)
End Sub
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function SetWindowsHookEx(ByVal idhook As Integer, ByVal lpfn As HookProc, ByVal hmod As IntPtr, ByVal dwthreadid As Integer) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function UnhookWindowsHookEx(ByVal hhook As IntPtr) As Integer
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function CallNextHookEx(ByVal hhook As IntPtr, ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Public Shared Function GetCurrentThreadId() As UInt32
End Function
Public Function CallGetMsgProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
If code < 0 Then Return CallNextHookEx(hhook, code, wParam, lParam)
Dim msg As New MSG
System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, msg)
If msg.hwnd <> Me.Handle AndAlso msg.hwnd <> Me.Controls(0).Handle AndAlso msg.hwnd <> Me.Controls(1).Handle Then Return CallNextHookEx(hhook, code, wParam, lParam)
If msg.message = WM_CONTEXTMENU OrElse (msg.message >= WM_RBUTTONDOWN AndAlso msg.message <= WM_RBUTTONDBLCLK) Then
System.Runtime.InteropServices.Marshal.Copy(New Byte() {0, 0}, 0, New IntPtr(lParam.ToInt32 + 4), 2)
End If
Return CallNextHookEx(hhook, code, wParam, lParam)
End Function
End Class
|