Windows API学习之滚动条系列函数
Windows API中滚动条相关函数有两个:
int SetScrollInfo(
HWND hwnd,
int fnBar,
LPSCROLLINFO lpsi,
BOOL fRedraw
);
BOOL GetScrollInfo(
HWND hwnd,
int fnBar,
LPSCROLLINFO lpsi
);
见名知意,SetScrollInfo就是用来设置窗口的滚动信息,GetScrollInfo就是用来获取窗口的滚动信息。SetScrollInfo的参数含义如下:
参数 意义
hwnd
滚动条控件的句柄或带有标准滚动栏的窗口的句柄
fnBar
用于指定哪一种滚动条,只能是下面的值之一:
SB_CTL :滚动条控件
SB_HORZ:水平滚动条
SB_VERT:垂直滚动条
lpsi
滚动条信息结构体指针。下面作进一步详细介绍。
fRedraw 值为TRUE表示要Windows重新绘制计算了新信息后的滚动条,FALSE表示不绘制。
GetScrollInfo的参数含义如下:
参数 意义
hwnd
滚动条控件的句柄或带有标准滚动栏的窗口的句柄
fnBar
用于指定哪一种滚动条,只能是下面的值之一:
SB_CTL :滚动条控件
SB_HORZ:水平滚动条
SB_VERT:垂直滚动条
lpsi
滚动条信息结构体指针。下面作进一步详细介绍。
值得注意的是在调用GetScrollInfo函数时要获取相关滚动信息,需要指定SCROLLINFO结构体中的fMask成员的值。fMask取下面的值的组合值:
值 意义
SIF_PAGE 获取SCROLLINFO中的nPage成员的值(即一页的大小)。
SIF_POS 获取SCROLLINFO中的nPos成员的值。
SIF_RANGE
获取SCROLLINFO中的nPos成员的nMin 和 nMax的值。
SIF_TRACKPOS
获取SCROLLINFO中的nTrackPos成员的值。
nTrackPos
SIF_RANGE、SIF_POS、SIF_PAGE和SIF_TRACKPOS的组合。
使用例程(据petzod的《windows程序设计》第四章,仅列出主要代码片段,具体请下载源码):
view plaincopy to clipboardprint?
01.case WM_SIZE:
02. {
03. // save the width and height of window when changed the size of window
04. cxClient = LOWORD(lp); // the width
05. cyClient = HIWORD(lp); // the height
06. // set vertical scroll bar range and page size
07. si.cbSize = sizeof(SCROLLBARINFO);
08. si.fMask = SIF_RANGE|SIF_PAGE;
09. si.nMin = 0;
10. si.nMax = NUMLINES - 1;
11. si.nPage = cyClient/cyChar;
12. SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
13. // set horizontal scroll bar and page size
14. si.cbSize = sizeof(SCROLLBARINFO);
15. si.fMask = SIF_RANGE|SIF_PAGE;
16. si.nMin = 0;
17. si.nMax = 2 + nMaxWidth/cxChar;
18. si.nPage = cxClient/cxChar;
19. SetScrollInfo(hWnd,SB_HORZ,&si,TRUE);
20. return 0;
21. }
22.case WM_VSCROLL:
23. {
24.// get all vertical scroll bar information
25.si.cbSize = sizeof(SCROLLINFO);
26.si.fMask = SIF_ALL;
27.::GetScrollInfo(hWnd,SB_VERT,&si);
28. // save the position for comparison later on
29.nVertPos = si.nPos;
30. switch (LOWORD(wp))
31. {
32. case SB_LINEUP:
33. {
34. si.nPos -=1;// the height decrease 1 unit
35. break;
36. }
37. case SB_LINEDOWN:
38. {
39. si.nPos +=1;// the height increase 1 unit
40. break;
41. }
42. case SB_PAGEUP:
43. {
44. // back to prev page, the cyClient/cyChar is the number of row in one page
45. si.nPos -= cyClient/cyChar;
46. break;
47. }
48. case SB_PAGEDOWN:
49. {
50. // back to next page
51. si.nPos += cyClient/cyChar;
52. break;
53. }
54. case SB_THUMBPOSITION:
55. {
56. si.nPos = HIWORD(wp);
57. break;
58. }
59. default:
60. break;
61. }
62. // set the position and then retrieve it.Due to adjustments
63. // by Windows it may not be the same as the value set.
64. si.fMask = SIF_POS;
65. SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
66. GetScrollInfo(hWnd,SB_VERT,&si);
67. //if the position has changed,scroll the window update it
68. if (si.nPos!=nVertPos)
69. {
70. ::ScrollWindow(hWnd,0,cyChar*(nVertPos-si.nPos),NULL,NULL);
71. ::UpdateWindow(hWnd);
72. }
73. return 0;
74. }
75.case WM_HSCROLL:
76. {
77. // get all the vertical scroll bar information
78. si.cbSize = sizeof(si);
79. si.fMask = SIF_ALL;
80. // save the position for comparison later on
81. ::GetScrollInfo(hWnd,SB_HORZ,&si);
82. nHorzPos = si.nPos;
83. switch (LOWORD(wp))
84. {
85. case SB_LINELEFT:
86. {
87. si.nPos -=1;
88. break;
89. }
90. case SB_LINERIGHT:
91. {
92. si.nPos +=1;
93. break;
94. }
95. case SB_PAGELEFT:
96. {
97. si.nPos -= si.nPage;
98. break;
99. }
100. case SB_PAGERIGHT:
101. {
102. si.nPos += si.nPage;
103. break;
104. }
105. case SB_THUMBPOSITION:
106. {
107. si.nPos = si.nTrackPos;
108. break;
109. }
110. default:
111. break;
112. }
113. // set the position and then retrieve it.due to adjustments
114. // by windows it may not be the same as the value set
115. si.fMask = SIF_POS;
116. ::SetScrollInfo(hWnd,SB_HORZ,&si,TRUE);
117. ::GetScrollInfo(hWnd,SB_HORZ,&si);
118. // if the postion has changed ,scroll the window
119. if (si.nPos!=nHorzPos)
120. {
121. ::ScrollWindow(hWnd,cxChar*(nHorzPos-si.nPos),0,NULL,NULL);
122. }
123. return 0;
124. }
125.case WM_PAINT:
126. {
127. hdc = ::BeginPaint(hWnd,&ps);
128. // get vertical scroll bar position
129. si.cbSize = sizeof(si);
130. si.fMask = SIF_POS;
131. ::GetScrollInfo(hWnd,SB_VERT,&si);
132. nVertPos = si.nPos;
133. // get horizontal scroll bar position
134. GetScrollInfo(hWnd,SB_HORZ,&si);
135. nHorzPos = si.nPos;
136. // find painting limits
137. int nPaintBeg = max(0,nVertPos+ps.rcPaint.top/cyChar); // the begin row
138. int nPaintEnd = min(NUMLINES-1,nVertPos+ps.rcPaint.bottom/cyChar); // the end row
139. for (int i =nPaintBeg;i<=nPaintEnd;i++)
140. {
141. // calculate the y position of draw region, when y position less 0,skip
142. int x = cxChar*(1-nHorzPos);
143. int y = cyChar*(i-nVertPos);
144. ::TextOut(hdc,x,y,sysmetrics[i].szLabel,lstrlen(sysmetrics[i].szLabel));
145. ::TextOut(hdc,x+22*cxCaps,y,sysmetrics[i].szDesc,lstrlen(sysmetrics[i].szDesc));
146. ::SetTextAlign(hdc,TA_RIGHT|TA_TOP);
147. ::TextOut(hdc,x+22*cxCaps+40*cxChar,y,szBuffer,wsprintf(szBuffer,_T("%5d"),::GetSystemMetrics(sysmetrics[i].Index)));
148. ::SetTextAlign(hdc,TA_LEFT|TA_TOP);
149. }
150. ::EndPaint(hWnd,&ps);
151. return 0;
152. }