当鼠标滚动或在列表框中的滚动条顶部时如何停止计时器
How to stop a timer when mouse is scrolling or on top of scrollbar in listbox
我正在寻找一种方法来检测并在鼠标光标滚动列表框时关闭计时器。
尽管要像这样创建一个新的 class,但有一种简单的方法吗?link
是否可以检查列表框 1 滚动条的矩形位置并说:如果鼠标在此范围内,则 timer1.stop?
编辑1:
为了创建一个矩形,我使用
If e.X >= 364 AndAlso e.X <= 446 AndAlso e.Y >= 86 AndAlso e.Y <= 144 Then
MessageBox.Show("Clicked within the rectangle")
Else
MessageBox.Show("Clicked outside the rectangle")
End If
449-359是矩形的左上角位置
而矩形大小是 x30 y156
问题是我不知道在哪个事件中让它运行!
列表框单击事件不会将滚动条识别为“列表框内部”
Form_mouse 单击事件无法将列表框滚动条识别为表单中的单击。
有一个事件,尽管您处于控制状态,但它会让您玩这个解决方法?
谢谢
这是我发布的 on MSDN using this C# code。下面没有提供重新启动计时器的代码。
Public Class BetterListBox
Inherits ListBox
' Event declaration
Public Delegate Sub BetterListBoxScrollDelegate(ByVal Sender As Object, ByVal e As BetterListBoxScrollArgs)
Public Event Scroll As BetterListBoxScrollDelegate
' WM_VSCROLL message constants
Private Const WM_VSCROLL As Integer = &H115
Private Const SB_THUMBTRACK As Integer = 5
Private Const SB_ENDSCROLL As Integer = 8
Protected Overrides Sub WndProc(ByRef m As Message)
' Trap the WM_VSCROLL message to generate the Scroll event
MyBase.WndProc(m)
If m.Msg = WM_VSCROLL Then
Dim nfy As Integer = m.WParam.ToInt32() And &HFFFF
If (nfy = SB_THUMBTRACK OrElse nfy = SB_ENDSCROLL) Then
RaiseEvent Scroll(Me, New BetterListBoxScrollArgs(Me.TopIndex, nfy = SB_THUMBTRACK))
End If
End If
End Sub
Public Class BetterListBoxScrollArgs
' Scroll event argument
Private mTop As Integer
Private mTracking As Boolean
Public Sub New(ByVal top As Integer, ByVal tracking As Boolean)
mTop = top
mTracking = tracking
End Sub
Public ReadOnly Property Top() As Integer
Get
Return mTop
End Get
End Property
Public ReadOnly Property Tracking() As Boolean
Get
Return mTracking
End Get
End Property
End Class
End Class
然后在你的表单中订阅滚动事件。在您的项目中需要上面的列表框,启用一个计时器和一个标签。
Private Sub BetterListBox1_Scroll(Sender As Object, e As BetterListBox.BetterListBoxScrollArgs) _
Handles BetterListBox1.Scroll
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Now.ToString()
End Sub
我正在寻找一种方法来检测并在鼠标光标滚动列表框时关闭计时器。 尽管要像这样创建一个新的 class,但有一种简单的方法吗?link
是否可以检查列表框 1 滚动条的矩形位置并说:如果鼠标在此范围内,则 timer1.stop?
编辑1: 为了创建一个矩形,我使用
If e.X >= 364 AndAlso e.X <= 446 AndAlso e.Y >= 86 AndAlso e.Y <= 144 Then
MessageBox.Show("Clicked within the rectangle")
Else
MessageBox.Show("Clicked outside the rectangle")
End If
449-359是矩形的左上角位置 而矩形大小是 x30 y156 问题是我不知道在哪个事件中让它运行! 列表框单击事件不会将滚动条识别为“列表框内部” Form_mouse 单击事件无法将列表框滚动条识别为表单中的单击。 有一个事件,尽管您处于控制状态,但它会让您玩这个解决方法? 谢谢
这是我发布的 on MSDN using this C# code。下面没有提供重新启动计时器的代码。
Public Class BetterListBox
Inherits ListBox
' Event declaration
Public Delegate Sub BetterListBoxScrollDelegate(ByVal Sender As Object, ByVal e As BetterListBoxScrollArgs)
Public Event Scroll As BetterListBoxScrollDelegate
' WM_VSCROLL message constants
Private Const WM_VSCROLL As Integer = &H115
Private Const SB_THUMBTRACK As Integer = 5
Private Const SB_ENDSCROLL As Integer = 8
Protected Overrides Sub WndProc(ByRef m As Message)
' Trap the WM_VSCROLL message to generate the Scroll event
MyBase.WndProc(m)
If m.Msg = WM_VSCROLL Then
Dim nfy As Integer = m.WParam.ToInt32() And &HFFFF
If (nfy = SB_THUMBTRACK OrElse nfy = SB_ENDSCROLL) Then
RaiseEvent Scroll(Me, New BetterListBoxScrollArgs(Me.TopIndex, nfy = SB_THUMBTRACK))
End If
End If
End Sub
Public Class BetterListBoxScrollArgs
' Scroll event argument
Private mTop As Integer
Private mTracking As Boolean
Public Sub New(ByVal top As Integer, ByVal tracking As Boolean)
mTop = top
mTracking = tracking
End Sub
Public ReadOnly Property Top() As Integer
Get
Return mTop
End Get
End Property
Public ReadOnly Property Tracking() As Boolean
Get
Return mTracking
End Get
End Property
End Class
End Class
然后在你的表单中订阅滚动事件。在您的项目中需要上面的列表框,启用一个计时器和一个标签。
Private Sub BetterListBox1_Scroll(Sender As Object, e As BetterListBox.BetterListBoxScrollArgs) _
Handles BetterListBox1.Scroll
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Now.ToString()
End Sub