如何使用鼠标滚轮将 ListView 滚动一行而不是默认的三行? [英] How can I scroll a ListView by one row instead of the default three using the mouse wheel?
问题描述
我有一个修改过的 ListView.当我使用鼠标滚轮滚动时,它会滚动三行.
I have a modified ListView. When I scroll using the mouse wheel, it scrolls three rows.
我希望它使用鼠标滚轮一次滚动一行.
I want it to scroll one row at a time using the mouse wheel.
Public Class listviewEx
Inherits ListView
Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As IntPtr, ByVal wBar As Integer,
ByVal bShow As Boolean) As Integer
' Constants
Private Const SB_HORZ As Integer = 0
Private Const WM_HSCROLL As Integer = &H114
Private Const WM_VSCROLL As Integer = &H115
Public Event Scroll As ScrollEventHandler
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
ShowScrollBar(MyBase.Handle, SB_HORZ, False)
If m.Msg = &H115 Then
' Trap WM_VSCROLL
End If
End Sub
Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.EnableNotifyMessage, True)
End Sub
End Class
推荐答案
您可以向 ListView 添加行为,使其滚动一行而不是默认的三行,使用 Ctrl 键 (因为此修饰符通常用于更改此类行为)与鼠标滚轮结合使用.
You can add a behavior to your ListView, to make it scroll one row instead of the default three, using the Ctrl key (as this modifier is often used to change this kind of behaviors) in combination with the mouse wheel.
然后,您可以在 Ctrl 未按下时进行标准的三行滚动,而在按下时进行单行滚动.
You can then have the standard three-rows scroll when Ctrl is not pressed and one-row scroll behavior when it's pressed.
覆盖 WndProc(正如您已经在做的那样),以处理 WM_MOUSEWHEEL
并验证 Ctrl 键是否按下,检查 WParam 的低位字是否为 MK_CONTROL = &H08
.
Override WndProc (as you're already doing), to handle WM_MOUSEWHEEL
and verify that the Ctrl key is pressed, checking whether the low-word of WParam is MK_CONTROL = &H08
.
按下时,确定 delta 是正还是负,并递增 ListView.TopItem.Index 然后设置 TopItem 基于计算的偏移量(添加最小/最大检查以避免溢出):
When it's pressed, determine whether the delta is positive or negative and increment the value returned by ListView.TopItem.Index to then set the TopItem based on the calculated offset (adding a minimum/maximum check to avoid overflows):
Imports System.Windows.Forms
Public Class ListViewEx
Inherits ListView
Private Const WM_MOUSEWHEEL As Integer = &H20A
Private Const MK_CONTROL As Integer = &H8
Public Sub New()
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case WM_MOUSEWHEEL
If Items.Count > 0 AndAlso (m.WParam.ToInt64() And &HFF) = MK_CONTROL Then
Dim offset = If((m.WParam.ToInt64() >> 16) > 0, -1, 1) + TopItem.Index
offset = Math.Max(Math.Min(offset, Items.Count - 1), 0)
TopItem = Items(offset)
m.Result = IntPtr.Zero
End If
End Select
End Sub
End Class
这篇关于如何使用鼠标滚轮将 ListView 滚动一行而不是默认的三行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!