我正在为我创建的自定义ContextMenuStrip控件添加一些功能。
我需要做的第一件事就是增加子菜单上下文项的鼠标悬停时间。以下代码适用于主要上下文项,但不会触发子菜单项的鼠标悬停事件。
我认为问题是我需要将tme.hWnd句柄设置为子菜单。如果是这种情况,如何在打开子菜单时获取子菜单的句柄?
谢谢!
Friend Const WM_MOUSEMOVE As Integer = &H200
Friend Const WM_MOUSELEAVE As Integer = &H2A3
Friend Const TME_LEAVE As Integer = &H2
Private _mouseOver As Boolean = False
Private _mouseOverHandel As IntPtr = Me.Handle
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_MOUSEMOVE
If Not _mouseOver Then
Dim tme As New TRACKMOUSEEVENTR()
tme.hWnd = _mouseOverHandel
tme.cbSize = Marshal.SizeOf(GetType(TRACKMOUSEEVENTR))
tme.dwFlags = TMEFlags.TME_HOVER
tme.dwHoverTime = 1000 * 3
TrackMouseEvent(tme)
_mouseOver = True
End If
MyBase.WndProc(m)
Exit Select
Case WM_MOUSELEAVE
_mouseOver = False
MyBase.WndProc(m)
Exit Select
Case Else
MyBase.WndProc(m)
End Select
End Sub
<DllImport("user32.dll")> _
Private Shared Function TrackMouseEvent(ByRef lpEventTrack As TRACKMOUSEEVENTR) As Integer
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure TRACKMOUSEEVENTR
Public cbSize As Int32
' using Int32 instead of UInt32 is safe here, and this avoids casting the result of Marshal.SizeOf()
<MarshalAs(UnmanagedType.U4)> _
Public dwFlags As TMEFlags
Public hWnd As IntPtr
Public dwHoverTime As UInt32
Public Sub New(dwFlags As Int32, hWnd As IntPtr, dwHoverTime As UInt32)
Me.cbSize = Marshal.SizeOf(GetType(TRACKMOUSEEVENTR))
Me.dwFlags = dwFlags
Me.hWnd = hWnd
Me.dwHoverTime = dwHoverTime
End Sub
End Structure
''' <summary>
''' The services requested. This member can be a combination of the following values.
''' </summary>
<Flags()> _
Public Enum TMEFlags As UInteger
TME_CANCEL = &H80000000UI
TME_HOVER = &H1
TME_LEAVE = &H2
TME_NONCLIENT = &H10
TME_QUERY = &H40000000
End Enum
答案 0 :(得分:0)
我不得不采取另一种方式来解决这个问题。我会把这个留给那些需要它的人。
我为动态子菜单项创建了一个MouseMove和MouseLeave处理程序。
MouseMove内部
If Not _mouseSubOver Then
_tsmi = sender
_itemDelayTimer = New Timer()
_itemDelayTimer.Interval = Math.Max(1, 1500)
AddHandler _itemDelayTimer.Tick, AddressOf OnDelayTimerExpire
_itemDelayTimer.Start()
_mouseSubOver = True
End If
Inside MouseLeave
If _itemDelayTimer IsNot Nothing Then
_itemDelayTimer.Stop()
_itemDelayTimer.Dispose()
_itemDelayTimer = Nothing
End If
_mouseSubOver = False
OnDelayTimerExpire内部
Private Sub OnDelayTimerExpire(sender As Object, e As EventArgs)
If _itemDelayTimer IsNot Nothing Then
_itemDelayTimer.Stop()
_itemDelayTimer.Dispose()
_itemDelayTimer = Nothing
End If
'Do your sub menu stuff here
End Sub