有没有一种方法可以设置鼠标必须停留在控件上才能触发其MouseHover事件的时间? [英] Is there a way to set the period of time that the mouse must be over a control in order to trigger its MouseHover event?
问题描述
我需要一种方法来设置MouseHoverTime属性,或者通过其他方法来设置它,以便在事件开始之前,鼠标需要悬停控件一定的时间.
I need a way to set the MouseHoverTime Property, or some other way of making it so that the mouse needs to be over a control for a given amount of time before an event kicks in.
我有一组控件,当鼠标移到它们上时需要执行代码,这可以使用MouseEnter或MouseHover来完成.但是,当鼠标快速移过这些控件中的许多控件时,每个控件的代码事件就会全部运行.这使程序的运行痛苦而又不切实际地缓慢.如果我可以为鼠标停留在控件上设置一个特定的时间阈值,那么仅当鼠标停留在控件上时才调用该事件,而当鼠标短暂停留在控件上时才调用该事件.
I have a set of controls that need to execute code when the mouse passes onto them, which can be done with either MouseEnter or MouseHover. However, when the mouse passes quickly over many of these controls, the code event for each control gets run in full. This makes the running of the program painfully and impractically slow. If I could set a certain time threshold for the mouse to stay over the control, then the event would only be called when the mouse stays over the control, and not when it briefly passes over it.
Private Sub Tile_MouseHover(Sender As Object, e As EventArgs)
CODE
CODE
CODE
End Sub
代码确实给出了预期的结果.它只是使它们步履蹒跚,因为每次光标像刷一个控件一样多时,其中的所有代码都将被执行.
The code does give the intended results. It just gives them at a snails pace, because every time the cursor so much as brushes a control, all of the code within it gets executed.
目标结果如下:
当光标悬停在控件上时,它必须在该控件上停留1-2秒,然后再调用该事件.如果光标在此之前离开,则根本不会调用该事件.
When the cursor hovers over a control, it must remain there for 1-2 seconds before calling the event. If the cursor leaves before this, the event doesn't get called at all.
推荐答案
我已经发布了答案的C#版本这里还有更多细节.
I've posted the C# version of the answer here with a bit more details.
注意:答案并不能解决解决以下问题的性能问题:您分享了该问题,但说明了如何增加鼠标悬停延迟时间.
Note: The answer doesn't claim to solve the performance issue which you shared in the question, but shows how you can increase the mouse hover delay time.
The hover time has been set in NativeMethods.TRACKMOUSEEVENT
to 100 milliseconds.
您可以处理 WM_MOUSEMOVE
并致电 TrackMouseEvent
,方法是将鼠标悬停的期望超时设置为在表单的标题栏上处理鼠标悬停.
You can handle WM_MOUSEMOVE
and call TrackMouseEvent
by setting desired timeout for mouse hover as dwHoverTime
field of the TRACKMOUSEEVENT
. Also handle WM_MOUSEHOVER
and raise a custom event like MyMouseHover
, then you can subscribe for MyMouseHover
event. You can find a similar approach in my post for handling hover event on title bar of a form: Handle Mouse Hover on Titlebar of Form.
VB.NET-具有自定义悬停时间的悬停事件
她是一个控件的示例,该控件以500毫秒的延迟引发 MyMouseHover
事件:
Her is an example of a control which raises MyMouseHover
event with 500 ms delay:
Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Class SampleControl
Inherits Control
<DllImport("user32.dll")>
Private Shared Function TrackMouseEvent(ByRef lpEventTrack As TRACK_MOUSE_EVENT) _
As Integer
End Function
<StructLayout(LayoutKind.Sequential)>
Private Structure TRACK_MOUSE_EVENT
Public cbSize As UInteger
Public dwFlags As UInteger
Public hwndTrack As IntPtr
Public dwHoverTime As UInteger
Public Shared ReadOnly Empty As TRACK_MOUSE_EVENT
End Structure
Private track As TRACK_MOUSE_EVENT = TRACK_MOUSE_EVENT.Empty
Const WM_MOUSEMOVE As Integer = &H200
Const WM_MOUSEHOVER As Integer = &H2A1
Const TME_HOVER As Integer = &H1
Const TME_LEAVE As Integer = &H2
Public Event MyMouseHover As EventHandler
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_MOUSEMOVE Then
track.hwndTrack = Me.Handle
track.cbSize = CUInt(Marshal.SizeOf(track))
track.dwFlags = TME_HOVER Or TME_LEAVE
track.dwHoverTime = 500
TrackMouseEvent(track)
End If
If m.Msg = WM_MOUSEHOVER Then
RaiseEvent MyMouseHover(Me, EventArgs.Empty)
End If
MyBase.WndProc(m)
End Sub
End Class
这篇关于有没有一种方法可以设置鼠标必须停留在控件上才能触发其MouseHover事件的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!