如何确定VB.NET ListView是否向用户显示垂直滚动条

时间:2014-06-30 16:06:35

标签: vb.net listview scrollbar visibility

我觉得这应该是简单的事情,但我似乎无法找到如何做到这一点。

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

我尝试过以下链接中的解决方案:

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

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

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

如果有人有任何想法我会非常感激。

2 个答案:

答案 0 :(得分:5)

这是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

答案 1 :(得分:1)

尝试使用此代码,所有.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