1.修改滚动的范围,设置nMax的值,此处设置为3倍窗口高度。
CRect rc; GetClientRect(&rc); SCROLLINFO si = {sizeof(SCROLLINFO)}; si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE; si.nPos = si.nMin = 1; si.nMax = rc.Height() * 3; si.nPage = rc.Height(); SetScrollInfo(SB_VERT, &si, FALSE);
2.对话框添加WM_VSCROLL消息响应函数.
void CPrintDlgAppDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { SetVScrool(SB_VERT, nSBCode); CDialog::OnVScroll(nSBCode, nPos, pScrollBar); }
3.代码中添加以下3个辅助函数
void CPrintDlgAppDlg::ScrollClient(int nBar, int nPos) { static int s_prevx = 1; static int s_prevy = 1; int cx = 0; int cy = 0; int& delta = cy; int& prev = s_prevy; delta = prev - nPos; prev = nPos; if(cx || cy) { ScrollWindow(cx, cy, NULL, NULL); } } int CPrintDlgAppDlg::GetScrollPos(int nBar, UINT nSBCode) { SCROLLINFO si; si.cbSize = sizeof(SCROLLINFO); si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS; GetScrollInfo(nBar, &si); const int minPos = si.nMin; const int maxPos = si.nMax - (si.nPage - 1); int result = -1; switch(nSBCode) { case SB_LINEUP /*SB_LINELEFT*/: result = max(si.nPos - 1, minPos); break; case SB_LINEDOWN /*SB_LINERIGHT*/: result = min(si.nPos + 1, maxPos); break; case SB_PAGEUP /*SB_PAGELEFT*/: result = max(si.nPos - (int)si.nPage, minPos); break; case SB_PAGEDOWN /*SB_PAGERIGHT*/: result = min(si.nPos + (int)si.nPage, maxPos); break; case SB_THUMBPOSITION: // do nothing break; case SB_THUMBTRACK: result = si.nTrackPos; break; case SB_TOP /*SB_LEFT*/: result = minPos; break; case SB_BOTTOM /*SB_RIGHT*/: result = maxPos; break; case SB_ENDSCROLL: // do nothing break; } return result; } void CPrintDlgAppDlg::SetVScrool(int nBar, UINT nSBCode) { const int scrollPos = GetScrollPos(nBar, nSBCode); if(scrollPos == -1) return; SetScrollPos(nBar, scrollPos, TRUE); ScrollClient(nBar, scrollPos); }
时间: 2024-10-29 12:58:58