码迷,mamicode.com
首页 > 其他好文 > 详细

SetScrollInfo()函数实现滚动条

时间:2015-04-15 13:34:15      阅读:566      评论:0      收藏:0      [点我收藏+]

标签:

SetScrollInfo可以同时实现SetScrollRange和SetScrollPos的功能。而且SetScrollInfo可以使滚动块的大小随内容的多少而改变。
其中SetScrollInfo的参数SCROLLINFO结构体中的fMask可控制有效信息。
typedef struct tagSCROLLINFO
 { UINT cbSize;    //SCROLLINFO结构体本身的字节大小
UINT fMask;       //见下面的说明
 int nMin;           //最小滚动位置
 int nMax;         //最大滚动位置
UINT nPage;        //页面尺寸
 int nPos;               //滚动块的位置
int nTrackPos;         //滚动块当前被拖动的位置,不能在SetScrollInfo中指定
} SCROLLINFO; 
fMask表示设置或获取哪些数据,如:SIF_ALL所有数据成员都有效、SIF_PAGE(nPage有效)、SIF_POS(nPos有效)、SIF_RANGE(nMin和nMax有效)、SIF_TRACKPOS(nTrackPos有效)。

在使用滚动条功能时,如果要设置它的范围和位置可以用以前的函数,例如: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:拖动时滚动框的位置,该参数只能查询,不能设置。

SetScrollInfo()函数实现滚动条

标签:

原文地址:http://blog.csdn.net/ghevinn/article/details/45057385

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!