SCROLLINFO结构详解

  在刚开始使用SCROLLINFO结构时感觉很不顺手,尤其其中的成员fMask理解不太深刻,经过查询资料才理解一二。

 

  在使用滚动条功能时,如果要设置它的范围和位置可以用以前的函数,例如:SetScrollRange()、 SetScrollPos()、GetScrollRange()、GetScrollPos()等,但目前通常使用SetScrollInfo()与 GetScrollInfo(),使用这两个函数就要用到SCROLLINFO结构。

  

  可以先定义一个SCROLLINFO结构变量si,用&si作为以上两个函数的参数。同BeginPaint()中的&ps、GetTextMetrics()中的&tm等,这些结构都是通过这些函数被填充各域或取得各域的值。BeginPaint是填充ps的各域的值,GetTextMetrics是取得tm结构各域的值,而 SetScrollInfo()与GetScrollInfo()分别填充和取得。

  

  无论是Set还是Get,都得先设置si结构的第一个域的值,即赋给cbSize结构的大小。之后根据设置的fMask域的值进行Set或Get,当Set时,需要根据fMask的值将相关的域填充后再调用 SetScrollInfo(),这样si结构就被Set成功。当Get时,直接调用GetScrollInfo(),具体能使用哪些域的值是根据所设置的fMask域的值定的。

 

  如果要设置滚动条的范围和页面大小时,可编写以下代码:


      si.cbSize = sizeof (SCROLLINFO) ;
      si.cbMask = SIF_RANGE | SIF_PAGE ;
      si.nMin = 0 ;
      si.nMax = NUMLINES - 1 ;
      si.nPage = cyClient / cyChar ;
      SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;

    而若要用到滚动条的位置时,可以这样使用:
    先si.cbSize = sizeof (si) ;
    si.fMask = SIF_ALL ; // 表示Get后将使用si结构的位置、页面大小等量
    GetScrollInfo (hwnd, SB_VERT, &si)

  然后就可直接使用si.nPos、si.nPage、si.nTrackPos等量,这些量就是从si结构中通过Get函数获得的,

 

  Platform SDK中如下描述:

  

The SCROLLINFO structure contains scroll bar parameters to be set by the SetScrollInfo function (or SBM_SETSCROLLINFO message), or retrieved by the GetScrollInfo function (or SBM_GETSCROLLINFO message).

typedef struct tagSCROLLINFO {  // si
    UINT cbSize;
    UINT fMask;
    int  nMin;
    int  nMax;
    UINT nPage;
    int  nPos;
    int  nTrackPos;
}   SCROLLINFO;
typedef SCROLLINFO FAR *LPSCROLLINFO;
 
Members
cbSize
Specifies the size, in bytes, of this structure.
fMask
Specifies the scroll bar parameters to set or retrieve. This member can be a combination of the following values: Value Meaning
SIF_ALL Combination of SIF_PAGE, SIF_POS, SIF_RANGE, and SIF_TRACKPOS.
SIF_DISABLENOSCROLL This value is used only when setting a scroll bar's parameters. If the scroll bar's new parameters make the scroll bar unnecessary, disable the scroll bar instead of removing it.  
SIF_PAGE The nPage member contains the page size for a proportional scroll bar.
SIF_POS The nPos member contains the scroll box position, which is not updated while the user drags the scroll box.
SIF_RANGE The nMin and nMax members contain the minimum and maximum values for the scrolling range.
SIF_TRACKPOS The nTrackPos member contains the current position of the scroll box while the user is dragging it.


nMin
Specifies the minimum scrolling position.
nMax
Specifies the maximum scrolling position.
nPage
Specifies the page size. A scroll bar uses this value to determine the appropriate size of the proportional scroll box.
nPos
Specifies the position of the scroll box.
nTrackPos
Specifies the immediate position of a scroll box that the user is dragging. An application can retrieve this value while processing the SB_THUMBTRACK notification message. An application cannot set the immediate scroll position; the SetScrollInfo function ignores this member.


成员说明:
cbSize: SCROLLINFO结构长度字节数,该值在设置和查询参数时都必须填写。
fMask: 指定结构中的哪些成员是有效,该值共有如下5种选择,可以选择多种用“OR”组合起来,该值在

设置和查询参数时都必须填写。
SIF_ALL      :整个结构都有效
SIF_DISABLENOSCROLL:该值仅在设定参数时使用,视控件参数设定的需要来对本结构的成员进行取舍。
SIF_PAGE      :nPage成员有效
SIF_POS      :nPos成员有效
SIF_RANGE     :nMin和nMax成员有效
nMin:滚动范围最小值
nMax:滚动范围最大值
nPage:页尺寸,用来确定比例滚动框的大小
nPos:滚动框的位置
nTrackPos:拖动时滚动框的位置,该参数只能查询,不能设置。

posted @ 2011-04-20 18:24  Healtheon  阅读(22960)  评论(1编辑  收藏  举报