如何检测如果滚动条是否可见控件上

控件 (如 RichTextBox、 TreeView、 ListView、 列表框、 DirListBox,和 FileListBox不提供内置的功能,以确定在水平或垂直滚动条是否可见。本文演示如何检索有关可用于确定滚动条是否可见的控件的窗口样式信息。

滚动条的可见性存储为控件的窗口样式。 若要从控件检索窗口样式信息,您可以调用GetWindowLong 函数。

分步示例

  1. 在 Visual Basic 中开始一个新的标准 EXE 项目。默认情况下创建 Form1。
  2. 在 项目 菜单上单击 组件。在 组件 对话框中选中 Microsoft Windows 公共控件 6.0 复选框,然后单击 确定
  3. 向 Form1 中添加一个 树视图 和两个 命令按钮 控件。
  4. 下面的代码添加到 Form1 的通用声明部分:
          
            
    Option Explicit

    Private Const GWL_STYLE = ( - 16 )
    Private Const WS_HSCROLL = & H100000
    Private Const WS_VSCROLL = & H200000

    Private Declare Function GetWindowLong Lib " user32 " Alias " GetWindowLongA " & _
    (
    ByVal hwnd As Long , ByVal nIndex As Long ) As Long

    Private Sub Command1_Click()
    Dim wndStyle As Long

    ' Retrieve the window style of the control.
    wndStyle = GetWindowLong(TreeView1.hwnd, GWL_STYLE)

    ' Test if the horizontal scroll bar style is present
    ' in the window style, indicating that a horizontal
    ' scroll bar is visible.
    If (wndStyle And WS_HSCROLL) <> 0 Then
    MsgBox " A horizontal scroll bar is visible. "
    Else
    MsgBox " A horizontal scroll bar is NOT visible. "
    End If

    ' Test if the vertical scroll bar style is present
    ' in the window style, indicating that a vertical
    ' scroll bar is visible.
    If (wndStyle And WS_VSCROLL) <> 0 Then
    MsgBox " A vertical scroll bar is visible. "
    Else
    MsgBox " A vertical scroll bar is NOT visible. "
    End If
    End Sub

    Private Sub Command2_Click()
    ' Make the size of the control smaller
    ' so that the scroll bars will appear.
    TreeView1.Move 250 , 900 , 1000 , 1000
    End Sub

    Private Sub Form_Load()
    ' Size and position the form and controls.
    Form1.ScaleMode = 1
    Form1.Move
    0 , 0 , 5100 , 5040
    Command1.Caption
    = " Scroll Bar Test "
    Command1.Move
    120 , 120 , 1700 , 500
    Command2.Caption
    = " Size Control "
    Command2.Move
    2000 , 120 , 1700 , 500
    TreeView1.Move
    250 , 900 , 3000 , 1500

    ' Add sample text to the TreeView control.
    TreeView1.Nodes.Add , , , " 1: Sample Text "
    TreeView1.Nodes.Add , , ,
    " 2: Sample Text "
    TreeView1.Nodes.Add , , ,
    " 3: Sample Text "
    TreeView1.Nodes.Add , , ,
    " 4: Sample Text "
    End Sub
  5. 运行该项目。请注意任何滚动条是出现在 树视图 控件上。
  6. 单击 测试滚动条。一个消息框指示水平滚动条不存在。另一个消息框指示垂直滚动条不存在。
  7. 单击 大小控制。请注意高度和宽度 树视图 控件的较小,并且这两个水平和垂直滚动条可见。
  8. 单击 测试滚动条。一个消息框指示水平滚动条存在,其中后跟另一个消息框,表明在垂直滚动条存在。

转载于:http://support.microsoft.com/kb/299686/zh-cn

你可能感兴趣的:(滚动条)