如何使用鼠标滚轮滚动ListView而不是默认滚动3到默认行? [英] How to scroll a ListView by one row instead of the default 3 using the mouse wheel?

查看:56
本文介绍了如何使用鼠标滚轮滚动ListView而不是默认滚动3到默认行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个修改过的ListView.当我使用鼠标滚轮滚动时,它会滚动3行.
我希望它使用鼠标滚轮一次滚动一行.
任何帮助将不胜感激.

I have a modified ListView. When I scroll using the Mouse wheel, it scrolls 3 rows.
I want it to scroll one row at a time using the Mouse wheel.
Any help would be appreciate it.

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

推荐答案

您可以使用 Control 键将行为添加到ListView,以使其滚动1行而不是默认滚动3行(因为此修饰符通常用于更改这种行为),与鼠标滚轮结合使用.
然后,当不按 Control 时,可以使标准的3行滚动,而当按1行时,可以使标准的1行滚动.

You can add a behavior to your ListView, to make it scroll 1 Row instead of the default 3, using the Control key (as this modifier is often used to change this kind of behaviors) in combination with the Mouse Wheel.
You can then have the standard 3-Rows scroll when Control is not pressed and 1-Row Scroll behavior when it's pressed.

重写WndProc(就像您已经在做的那样),以处理

Override WndProc (as you're already doing), to handle WM_MOUSEWHEEL and verify that the Control key is pressed, checking whether the Low-Word of WParam is MK_CONTROL = &H08.
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 Min/Max 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而不是默认滚动3到默认行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆