作者:朱金燦
來源:http://blog.csdn.net/clever101
Windows API中滾動條相關(guān)函數(shù)有兩個:
int SetScrollInfo(
HWND hwnd,
int fnBar,
LPSCROLLINFO lpsi,
BOOL fRedraw
);
BOOL GetScrollInfo(
HWND hwnd,
int fnBar,
LPSCROLLINFO lpsi
);
見名知意,SetScrollInfo就是用來設(shè)置窗口的滾動信息,GetScrollInfo就是用來獲取窗口的滾動信息。SetScrollInfo的參數(shù)含義如下:
參數(shù) 意義
hwnd
滾動條控件的句柄或帶有標(biāo)準(zhǔn)滾動欄的窗口的句柄
fnBar
用于指定哪一種滾動條,只能是下面的值之一:
SB_CTL :滾動條控件
SB_HORZ:水平滾動條
SB_VERT:垂直滾動條
lpsi
滾動條信息結(jié)構(gòu)體指針。下面作進(jìn)一步詳細(xì)介紹。
fRedraw 值為TRUE表示要Windows重新繪制計(jì)算了新信息后的滾動條,F(xiàn)ALSE表示不繪制。
GetScrollInfo的參數(shù)含義如下:
參數(shù) 意義
hwnd
滾動條控件的句柄或帶有標(biāo)準(zhǔn)滾動欄的窗口的句柄
fnBar
用于指定哪一種滾動條,只能是下面的值之一:
SB_CTL :滾動條控件
SB_HORZ:水平滾動條
SB_VERT:垂直滾動條
lpsi
滾動條信息結(jié)構(gòu)體指針。下面作進(jìn)一步詳細(xì)介紹。
值得注意的是在調(diào)用GetScrollInfo函數(shù)時要獲取相關(guān)滾動信息,需要指定SCROLLINFO結(jié)構(gòu)體中的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的組合。
使用例程(據(jù)petzod的《windows程序設(shè)計(jì)》第四章,僅列出主要代碼片段,具體請下載源碼):
- case WM_SIZE:
- {
- // save the width and height of window when changed the size of window
- cxClient = LOWORD(lp); // the width
- cyClient = HIWORD(lp); // the height
- // set vertical scroll bar range and page size
- si.cbSize = sizeof(SCROLLBARINFO);
- si.fMask = SIF_RANGE|SIF_PAGE;
- si.nMin = 0;
- si.nMax = NUMLINES - 1;
- si.nPage = cyClient/cyChar;
- SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
- // set horizontal scroll bar and page size
- si.cbSize = sizeof(SCROLLBARINFO);
- si.fMask = SIF_RANGE|SIF_PAGE;
- si.nMin = 0;
- si.nMax = 2 + nMaxWidth/cxChar;
- si.nPage = cxClient/cxChar;
- SetScrollInfo(hWnd,SB_HORZ,&si,TRUE);
- return 0;
- }
- case WM_VSCROLL:
- {
- // get all vertical scroll bar information
- si.cbSize = sizeof(SCROLLINFO);
- si.fMask = SIF_ALL;
- ::GetScrollInfo(hWnd,SB_VERT,&si);
- // save the position for comparison later on
- nVertPos = si.nPos;
- switch (LOWORD(wp))
- {
- case SB_LINEUP:
- {
- si.nPos -=1;// the height decrease 1 unit
- break;
- }
- case SB_LINEDOWN:
- {
- si.nPos +=1;// the height increase 1 unit
- break;
- }
- case SB_PAGEUP:
- {
- // back to prev page, the cyClient/cyChar is the number of row in one page
- si.nPos -= cyClient/cyChar;
- break;
- }
- case SB_PAGEDOWN:
- {
- // back to next page
- si.nPos += cyClient/cyChar;
- break;
- }
- case SB_THUMBPOSITION:
- {
- si.nPos = HIWORD(wp);
- break;
- }
- default:
- break;
- }
- // set the position and then retrieve it.Due to adjustments
- // by Windows it may not be the same as the value set.
- si.fMask = SIF_POS;
- SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
- GetScrollInfo(hWnd,SB_VERT,&si);
- //if the position has changed,scroll the window update it
- if (si.nPos!=nVertPos)
- {
- ::ScrollWindow(hWnd,0,cyChar*(nVertPos-si.nPos),NULL,NULL);
- ::UpdateWindow(hWnd);
- }
- return 0;
- }
- case WM_HSCROLL:
- {
- // get all the vertical scroll bar information
- si.cbSize = sizeof(si);
- si.fMask = SIF_ALL;
- // save the position for comparison later on
- ::GetScrollInfo(hWnd,SB_HORZ,&si);
- nHorzPos = si.nPos;
- switch (LOWORD(wp))
- {
- case SB_LINELEFT:
- {
- si.nPos -=1;
- break;
- }
- case SB_LINERIGHT:
- {
- si.nPos +=1;
- break;
- }
- case SB_PAGELEFT:
- {
- si.nPos -= si.nPage;
- break;
- }
- case SB_PAGERIGHT:
- {
- si.nPos += si.nPage;
- break;
- }
- case SB_THUMBPOSITION:
- {
- si.nPos = si.nTrackPos;
- break;
- }
- default:
- break;
- }
- // set the position and then retrieve it.due to adjustments
- // by windows it may not be the same as the value set
- si.fMask = SIF_POS;
- ::SetScrollInfo(hWnd,SB_HORZ,&si,TRUE);
- ::GetScrollInfo(hWnd,SB_HORZ,&si);
- // if the postion has changed ,scroll the window
- if (si.nPos!=nHorzPos)
- {
- ::ScrollWindow(hWnd,cxChar*(nHorzPos-si.nPos),0,NULL,NULL);
- }
- return 0;
- }
- case WM_PAINT:
- {
- hdc = ::BeginPaint(hWnd,&ps);
- // get vertical scroll bar position
- si.cbSize = sizeof(si);
- si.fMask = SIF_POS;
- ::GetScrollInfo(hWnd,SB_VERT,&si);
- nVertPos = si.nPos;
- // get horizontal scroll bar position
- GetScrollInfo(hWnd,SB_HORZ,&si);
- nHorzPos = si.nPos;
- // find painting limits
- int nPaintBeg = max(0,nVertPos+ps.rcPaint.top/cyChar); // the begin row
- int nPaintEnd = min(NUMLINES-1,nVertPos+ps.rcPaint.bottom/cyChar); // the end row
- for (int i =nPaintBeg;i<=nPaintEnd;i++)
- {
- // calculate the y position of draw region, when y position less 0,skip
- int x = cxChar*(1-nHorzPos);
- int y = cyChar*(i-nVertPos);
- ::TextOut(hdc,x,y,sysmetrics[i].szLabel,lstrlen(sysmetrics[i].szLabel));
- ::TextOut(hdc,x+22*cxCaps,y,sysmetrics[i].szDesc,lstrlen(sysmetrics[i].szDesc));
- ::SetTextAlign(hdc,TA_RIGHT|TA_TOP);
- ::TextOut(hdc,x+22*cxCaps+40*cxChar,y,szBuffer,wsprintf(szBuffer,_T("%5d"),::GetSystemMetrics(sysmetrics[i].Index)));
- ::SetTextAlign(hdc,TA_LEFT|TA_TOP);
- }
- ::EndPaint(hWnd,&ps);
- return 0;
- }
相關(guān)源碼下載:
Windows API學(xué)習(xí)之滾動條系列函數(shù)演示程序
|