平时在处理滚动条程序时,总觉得代码很长,敲得手都累了,今天在试了试,也还行。
总结起来大致有三步:
- 设置滚动条的范围之类的基本信息,如可以再WM_SIZE消息中处理
- 响应滚动条消息并设置变化中的滚动条
- 更新界面变化
看个实例代码,当然,代码还是很长,可以想办法帮封装一下:
case WM_SIZE:
cx = LOWORD(lp);
cy = HIWORD(lp);si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = NUMLINES - 1;
si.nPage = cy / cyChar;
SetScrollInfo( hwnd, SB_VERT, &si, TRUE );si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = 2 + iMaxWidth / cxChar;
si.nPage = cx / cxChar;
SetScrollInfo( hwnd, SB_HORZ, &si, TRUE );
return 0;case WM_VSCROLL:
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo( hwnd, SB_VERT, &si );
iVertPos = si.nPos;
switch(LOWORD(wp))
{
case SB_TOP:
si.nPos = si.nMin;
break;case SB_BOTTOM:
si.nPos = si.nMax;
break;case SB_LINEUP:
si.nPos -= 1;
break;
case SB_LINEDOWN:
si.nPos += 1;
break;case SB_PAGEUP:
si.nPos -= si.nPage;
break;case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;default:
break;
}
si.fMask = SIF_POS;
SetScrollInfo( hwnd, SB_VERT, &si, TRUE );
GetScrollInfo( hwnd, SB_VERT, &si);if ( si.nPos != iVertPos )
{
ScrollWindow( hwnd, 0, cxChar * (iVertPos - si.nPos), NULL, NULL );
UpdateWindow( hwnd );
}return 0;
case WM_HSCROLL:
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo( hwnd, SB_HORZ, &si );
iHorPos = 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;
}
si.fMask = SIF_POS;
SetScrollInfo( hwnd, SB_HORZ, &si, TRUE );
GetScrollInfo( hwnd, SB_HORZ, &si);if ( si.nPos != iHorPos )
{
ScrollWindow( hwnd, 0, cxChar * (iHorPos - si.nPos), NULL, NULL );
UpdateWindow( hwnd );
}return 0;