您可以通过在每个控件触发单击事件时引发 Click 事件来做到这一点,但这很冗长,如果您添加新控件,则必须记住添加处理程序。
更好的方法是使用递归方法为用户控件中的每个控件添加点击事件处理程序:
Public Class UserControl1
'declare the event (must shadow as the usercontrol already has it's own click event)
Public Shadows Event Click(sender As Object, e As EventArgs)
Private Sub ClickEventHandlerForAllControls(sender As Object, e As EventArgs)
RaiseEvent Click(sender, e)
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddClickHandlersForControl(Me)
End Sub
Private Sub AddClickHandlersForControl(ctl As Control)
'add the click event handler for this control
AddHandler ctl.Click, AddressOf ClickEventHandlerForAllControls
'if the control has children (e.g. panel, form) then iterate through those and add the click event for each one
If ctl.HasChildren Then
For Each childCtl As Control In ctl.Controls
AddClickHandlersForControl(childCtl)
Next
End If
End Sub
End Class
编辑:正如您所说,您希望能够单击任何地方,包括标题行等。实现此目的的方法是使用全局鼠标挂钩(非托管代码不太理想)。
这是一个工作示例:
Public Class UserControl1
Private Structure MSLLHOOKSTRUCT
Public pt As Point
Public mouseData As Int32
Public flags As Int32
Public time As Int32
Public extra As IntPtr
End Structure
Private _mouseHook As IntPtr
Private Const WH_MOUSE_LL As Int32 = 14
Private Const LEFT_MOUSE_DOWN = 513
Private Const LEFT_MOUSE_UP = 514
Private Delegate Function MouseHookDelegate(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
<Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.FunctionPtr)> Private _mouseProc As MouseHookDelegate
Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As MouseHookDelegate, ByVal hInstance As IntPtr, ByVal wParam As Int32) As IntPtr
Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hook As IntPtr) As Boolean
Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Private Declare Function GetModuleHandleW Lib "kernel32.dll" (ByVal fakezero As IntPtr) As IntPtr
'declare the event (must shadow as the usercontrol already has it's own click event)
Public Shadows Event Click(sender As Object, e As EventArgs)
Private Sub UserControl1_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated
HookMouse()
End Sub
Private Sub UserControl1_HandleDestroyed(sender As Object, e As EventArgs) Handles Me.HandleDestroyed
UnHookMouse()
End Sub
Public Function HookMouse() As Boolean
If _mouseHook = IntPtr.Zero Then
_mouseProc = New MouseHookDelegate(AddressOf MouseHookProc)
_mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
End If
Return _mouseHook <> IntPtr.Zero
End Function
Public Sub UnHookMouse()
If _mouseHook = IntPtr.Zero Then Return
UnhookWindowsHookEx(_mouseHook)
_mouseHook = IntPtr.Zero
End Sub
Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Static downLocation As Point = New Point(-1, -1)
Select Case wParam.ToInt32
Case LEFT_MOUSE_DOWN
downLocation = lParam.pt
Case LEFT_MOUSE_UP
'don't raise click event if we have dragged
If lParam.pt = downLocation Then
'check the mouse location is inside the bounding rectangle of the usercontrol
Dim usercontrolLocation = New Point(Me.Parent.Location.X + Me.Location.X, Me.Parent.Location.Y + Me.Location.Y)
Dim usercontrolRect = New Rectangle(usercontrolLocation, Me.Size)
If usercontrolRect.Contains(lParam.pt) Then RaiseEvent Click(Me, New EventArgs())
End If
End Select
Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
End Function
End Class
您的点击事件将由表单上的用户控件引发:
Public Class Form1
Private Sub UserControl11_Click(sender As Object, e As EventArgs) Handles UserControl11.Click
MessageBox.Show("Usercontrol was clicked")
End Sub
End Class