【问题标题】:How can I determine if a VB.NET ListView is displaying vertical scrollbar to user如何确定 VB.NET ListView 是否正在向用户显示垂直滚动条
【发布时间】:2014-08-21 01:51:18
【问题描述】:

我觉得这应该很简单,但我似乎不知道如何做到这一点。

我有一个列表视图控件,我希望能够确定是否向用户显示垂直滚动条。

我已经尝试了以下链接中的解决方案:

http://www.pcreview.co.uk/forums/detect-presence-listview-scrollbar-t1321101.html

http://support.microsoft.com/KB/299686

我没有运气。我正在使用 VB.NET

如果有人有任何想法,我将不胜感激。

【问题讨论】:

    标签: vb.net listview scrollbar visibility


    【解决方案1】:

    这是 MSDN 答案的 NET 更新(如果你看的话,那是 VB6 相关的):

    'Pinvokes - these are usually Shared methods in a 
    ' Win32NativeMethods class you accumulate 
    Private Const GWL_STYLE As Integer = -16
    Private Const WS_HSCROLL = &H100000
    Private Const WS_VSCROLL = &H200000
    
    <DllImport("user32.dll", SetLastError:=True)> _
    private Shared Function GetWindowLong(ByVal hWnd As IntPtr, 
                           ByVal nIndex As Integer) As Integer
    End Function
    
    ' sometimes you use wrappers since many, many, many things could call
    ' SendMessage and so that your code doesnt need to know all the MSG params
    Friend Shared Function IsVScrollVisible(ByVal ctl As Control) As Boolean
        Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
        Return ((wndStyle And WS_VSCROLL) <> 0)
    
    End Function
    
    ' to be complete:
    Friend Shared Function IsHScrollVisible(ByVal ctl As Control) As Boolean
        Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
        Return ((wndStyle And WS_HSCROLL) <> 0)
    
    End Function
    

    在其他地方,订阅 ClientSizeChanged 事件:

    Private VScrollVis As Boolean = False
    Private Sub lv_ClientSizeChanged(sender As Object, e As EventArgs) 
                   Handles myListView.ClientSizeChanged
    
        VScrollVis = IsVScrollVisible(Me)
    
        MyBase.OnClientSizeChanged(e)
    End Sub
    

    你没有表明你想对此做什么。您可以在 VScrollVis 更改时引发一个新事件,或者您可以编写代码来“修复”控件,如果 HScroll 出现仅仅是因为 VScroll 现在是可见的。


    我只想调用一个函数并让它在滚动条可见时返回 true

    ' expose PInvoke if needed, convert to non-Shared
    Public Function IsVerticalScrollVisible(ctl As Control)
       Return IsVScrollVisible(ctl)
    End Function
    
    Public Function IsHorizontalScrollVisible(ctl As Control)
       Return IsHScrollVisible(ctl)
    End Function
    

    【讨论】:

    • 我只是希望能够调用一个函数,如果滚动条可见则返回 true,否则返回 false。
    • 然后创建一个从IsVScrollVisible返回值的函数
    • 我就是这么做的。非常感谢你。
    【解决方案2】:

    试试这个代码,所有的.net,只有一个 1 行函数。

    Private Function IsVerticalScrollVisible(ByVal lst As ListView) As Boolean
        If lst.Items.Count > 0 Then _
            If (lst.Items(0).Bounds.Top - lst.TopItem.Bounds.Top < 0) Or _
            (lst.Items(lst.Items.Count - 1).Bounds.Bottom > lst.ClientSize.Height) _
            Then Return True
    End Function
    

    【讨论】: