Private Declare Function SetScrollPos Lib "user32" _
   (ByVal hwnd As Long, _
    ByVal nBar As Long, _
    ByVal nPos As Long, _
    ByVal bRedraw As Long) As LongPrivate Const SB_HORZ = 0
Private Const SB_VERT = 1
Private Const SB_BOTH = 3Private Sub Command1_Click()
    SetScrollPos Text1.hwnd, SB_HORZ, 2, True  '水平
    SetScrollPos Text1.hwnd, SB_VERT, 20, True '垂直
End Sub

解决方案 »

  1.   

    如果要设置MAX,MIN值,还要用到SetScrollInfo函数。
      

  2.   

    楼上的老兄,能不能说一下SetScrollInfo的用法和参数.谢谢.
      

  3.   

    SetScrollInfo
    The SetScrollInfo function sets the parameters of a scroll bar, including the minimum and maximum scrolling positions, the page size, and the position of the scroll box (thumb). The function also redraws the scroll bar, if requested.int SetScrollInfo(
      HWND hwnd,    // handle to window with scroll bar
      int fnBar,    // scroll bar flag
      LPSCROLLINFO lpsi,
                    // pointer to structure with scroll parameters
      BOOL fRedraw  // redraw flag
    );
     
    Parameters
    hwnd 
    Handle to a scroll bar control or a window with a standard scroll bar, depending on the value of the fnBar parameter. 
    fnBar 
    Specifies the type of scroll bar for which to set parameters. This parameter can be one of the following values: Value Meaning 
    SB_CTL Sets the parameters of a scroll bar control. The hwnd parameter must be the handle to the scroll bar control.  
    SB_HORZ Sets the parameters of the given window's standard horizontal scroll bar.  
    SB_VERT Sets the parameters of the given window's standard vertical scroll bar.  
    lpsi 
    Pointer to a SCROLLINFO structure. Before calling SetScrollInfo, set the cbSize member of the structure to sizeof(SCROLLINFO), set the fMask member to indicate the parameters to set, and specify the new parameter values in the appropriate members. 
    The fMask member can be a combination of the following values: Value Meaning 
    SIF_DISABLENOSCROLL Disables the scroll bar instead of removing it, if the scroll bar's new parameters make the scroll bar unnecessary. 
    SIF_PAGE Sets the scroll page to the value specified in the nPage member of the SCROLLINFO structure pointed to by lpsi. 
    SIF_POS Sets the scroll position to the value specified in the nPos member of the SCROLLINFO structure pointed to by lpsi. 
    SIF_RANGE Sets the scroll range to the value specified in the nMin and nMax members of the SCROLLINFO structure pointed to by lpsi. fRedraw 
    Specifies whether the scroll bar is redrawn to reflect the changes to the scroll bar. If this parameter is TRUE, the scroll bar is redrawn, otherwise, it is not redrawn. 
    Return Values
    The return value is the current position of the scroll box. Res
    The SetScrollInfo function performs range checking on the values specified by the nPage and nPos members of the SCROLLINFO structure. The nPage member must specify a value from 0 to nMax - nMin +1. The nPos member must specify a value between nMin and nMax - max(nPage – 1, 0). If either value is beyond its range, the function sets it to a value that is just within the range. Windows CE: In Windows CE 2.0, if you pass a null pointer in the lpsi parameter, SetScrollInfo returns zero, rather than the current position of the scroll box. QuickInfo
      Windows NT: Requires version 3.51 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Requires version 1.0 or later.
      Header: Declared in winuser.h.
      Import Library: Use user32.lib.
      

  4.   

    Public Declare Function SetScrollInfo Lib "user32" (ByVal hwnd As Long, ByVal n As Long, lpcScrollInfo As SCROLLINFO, ByVal bool As Boolean) As LongPublic Type SCROLLINFO
        cbSize As Long
        fMask As Long
        nMin As Long
        nMax As Long
        nPage As Long
        nPos As Long
        nTrackPos As Long
    End TypePublic Const SB_BOTH = 3
    Public Const SB_HORZ = 0
    Public Const SB_VERT = 1
    Public Const SIF_ALL = 23
    Public Const SIF_RANGE = 1
    Public Const SIF_PAGE = 2
    Public Const SIF_POS = 4
    Public Const SIF_TRACKPOS = 16'用法示例
    'Dim si As SCROLLINFO
    'si.cbSize = Len(si)
    'si.fMask = SIF_ALL
    'si.nMax = 100
    'si.nMin = 0
    'si.nPage = 50
    'si.nPos = 20
    'SetScrollInfo hwnd, SB_VERT, si, True
      

  5.   

    MSDN我也看了,可是在我的程序中就是不能起作用.我用的是MDI窗口,我要控制MDI窗口的滚动条,因为当子窗口的尺寸大于MDI窗口时MDI窗口就会出现滚动条(MDI的ScrollBar属性设为True),可是我在GetScrollInfo主窗口时SCROLLINFO结构的内容总是为空,即全为0。同样我在设置SetScrollPos主窗口时也没有反应!!(注:我确实用的是主窗口的hwnd,请不要猜测这个错误).是不是这几个API对MDI窗口不起作用呢?真是奇怪,高手指点。分不够再加.
      

  6.   

    当然不会起作用“MDI窗体的滚动条”实际上不在MDI窗体上!
    而是在MDI窗体的“MDIClient”控件上你可以用FindWindowEx去查找它的句柄
      

  7.   

    我用FindWiondowEx找到了MDIClient的权柄,用SendMessage也可以操作滚动条,可是用GetScrollInfo还是不行,返回值为0,SCROLLINFO结构也还是空,当然SetScrollPos也不能起作用了,为什么呢?
      

  8.   

    看看我的问题
    http://www.csdn.net/expert/topic/1050/1050384.xml?temp=.4546167
      

  9.   

    建立一个MDI窗体(MDIForm1)、一个MDI子窗体(Form1)
    在MDI子窗体中放置一个按键(Command1)、一个文本框(Text1),文本框的MultiLine设为True
    MDIForm1:Private Sub MDIForm_Load()
        Form1.Show
        
    End Sub
    Form1:Private Declare Function GetScrollInfo Lib "user32" (ByVal hWnd As Long, ByVal n As Long, lpScrollInfo As SCROLLINFO) As LongPrivate Type SCROLLINFO
        cbSize As Long
        fMask As Long
        nMin As Long
        nMax As Long
        nPage As Long
        nPos As Long
        nTrackPos As Long
    End TypePrivate Const SB_HORZ = 0
    Private Const SB_VERT = 1
    Private Const SB_CTL = 2
    Private Const SB_BOTH = 3Private Const SIF_RANGE = 1
    Private Const SIF_PAGE = 2
    Private Const SIF_POS = 4
    Private Const SIF_TRACKPOS = &H10
    Private Const SIF_ALL = SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOSPrivate Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPrivate Sub Command1_Click()
        Dim SI As SCROLLINFO
        Dim TemphWnd As Long
        
        TemphWnd = FindWindowEx(MDIForm1.hWnd, 0, "MDIClient", vbNullString)
        'Debug.Print TemphWnd
        
        Text1.Text = ""
        SI.cbSize = Len(SI)
        SI.fMask = SIF_ALL
        GetScrollInfo TemphWnd, SB_HORZ, SI
        Text1.Text = Text1.Text + "水平滚动条:" + vbCrLf
        Text1.Text = Text1.Text & "nMin:" & SI.nMin & vbCrLf
        Text1.Text = Text1.Text & "nMax:" & SI.nMax & vbCrLf
        Text1.Text = Text1.Text & "nPage:" & SI.nPage & vbCrLf
        Text1.Text = Text1.Text & "nPos:" & SI.nPos & vbCrLf
        Text1.Text = Text1.Text & "nTrackPos:" & SI.nTrackPos & vbCrLf
        
        Text1.Text = Text1.Text + vbCrLf
        SI.cbSize = Len(SI)
        SI.fMask = SIF_ALL
        GetScrollInfo TemphWnd, SB_VERT, SI
        Text1.Text = Text1.Text + "垂直滚动条:" + vbCrLf
        Text1.Text = Text1.Text & "nMin:" & SI.nMin & vbCrLf
        Text1.Text = Text1.Text & "nMax:" & SI.nMax & vbCrLf
        Text1.Text = Text1.Text & "nPage:" & SI.nPage & vbCrLf
        Text1.Text = Text1.Text & "nPos:" & SI.nPos & vbCrLf
        Text1.Text = Text1.Text & "nTrackPos:" & SI.nTrackPos & vbCrLf
        
    End SubPrivate Sub Form_Load()
        Command1.Caption = "取得信息"
        
    End Sub
      

  10.   

    试过了,可以取出SCROLLINFO,不过为什么不管滚动块的位置在哪,nPos总是为0?难道nPos不是滚动块的位置吗?(设置的滚动条是正确的,即读取的是相应的滚动条)
      

  11.   

    昏……你有没有注意nMin、nMax
    MDI窗体的滚动条就是通过nMin、nMax设定的
    而不是像一般程序那样用nPos
      

  12.   

    那不对,我看了看,当我设了si.nPos后调用SetScrollInfo是可以移动滚动条的,而且我发现用手动调整滚动条后调用GetScrollInfo后nPos为0,变化的是nMin和nMax,而我用程序控制滚动条后(我调用了10次 SendMessage hwnd,WM_VSCROLL,SB_LINEDOWN)再使用GetScrollInfo后nMin和nMax没有变化,而nPos则发生了变动.
    另外,不论用SetScrollInfo还是SetScrollPos来移动滚动条后,滚动块的位置是变化了,可是窗口内容并没有随着卷动.在没有找到其它的方法来自动同时移动滚动条和窗口内容的情况下我只好做了个循环调用SendMessage来一次卷动一行,可是太不专业了,有谁有更好的办法请告诉我.
      

  13.   

    给MDI窗体发送WM_HSCROLL、WM_VSCROLL消息试试
    (不要用SetScrollInfo,只发送消息,估计移动的量去改变wParam)
    WM_HSCROLL
    The WM_HSCROLL message is sent to a window when a scroll event occurs in the window's standard horizontal scroll bar. This message is also sent to the owner of a horizontal scroll bar control when a scroll event occurs in the control. WM_HSCROLL 
    nScrollCode = (int) LOWORD(wParam);  // scroll bar value 
    nPos = (short int) HIWORD(wParam);   // scroll box position 
    hwndScrollBar = (HWND) lParam;       // handle to scroll bar 
     
    Parameters
    nScrollCode 
    Value of the low-order word of wParam. Specifies a scroll bar value that indicates the user's scrolling request. This parameter can be one of the following values: Value Meaning 
    SB_ENDSCROLL Ends scroll. 
    SB_LEFT Scrolls to the upper left. 
    SB_RIGHT Scrolls to the lower right. 
    SB_LINELEFT Scrolls left by one unit. 
    SB_LINERIGHT Scrolls right by one unit. 
    SB_PAGELEFT Scrolls left by the width of the window. 
    SB_PAGERIGHT Scrolls right by the width of the window. 
    SB_THUMBPOSITION The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation. 
    SB_THUMBTRACK The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to. 
    nPos 
    Value of the high-order word of wParam. Specifies the current position of the scroll box if the nScrollCode parameter is SB_THUMBPOSITION or SB_THUMBTRACK; otherwise, nPos is not used. 
    hwndScrollBar 
    Value of lParam. If the message is sent by a scroll bar, then hwndScrollBar is the handle to the scroll bar control. If the message is not sent by a scroll bar, hwndScrollBar is NULL. 
    Return Values
    If an application processes this message, it should return zero. Res
    The SB_THUMBTRACK notification message is typically used by applications that provide feedback as the user drags the scroll box. If an application scrolls the content of the window, it must also reset the position of the scroll box by using the SetScrollPos function. Note that the WM_HSCROLL message carries only 16 bits of scroll box position data. Thus, applications that rely solely on WM_HSCROLL (and WM_VSCROLL) for scroll position data have a practical maximum position value of 65,535. However, because the SetScrollInfo, SetScrollPos, SetScrollRange, GetScrollInfo, GetScrollPos, and GetScrollRange functions support 32-bit scroll bar position data, there is a way to circumvent the 16-bit barrier of the WM_HSCROLL and WM_VSCROLL messages. See GetScrollInfo for a description of the technique. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Requires version 1.0 or later.
      Header: Declared in winuser.h.
      

  14.   

    可是WM_HSCROLL和WM_VSCROLL消息只能移动一次,我试了SB_THUMBPOSITION和SB_THUMBTRACK可是没有什么反应。我想移到我想让它去的地方,并且窗口内容也跟着变化。