MFC入门示例之水平滚动条和垂直滚动条(CScroll Bar)
初始化滚动条
1 //初始化滚动条 2 SCROLLINFO si = { 0 }; 3 si.cbSize = sizeof(si); 4 si.fMask = SIF_RANGE | SIF_PAGE; 5 si.nMin = 0; 6 si.nMax = 100; 7 si.nPage = 10; 8 m_scroll.SetScrollInfo(&si);
滚动条事件
1 void CMFCApplication6Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 2 { 3 SCROLLINFO si = { 0 }; 4 si.cbSize = sizeof(si); 5 si.fMask = SIF_ALL; 6 pScrollBar->GetScrollInfo(&si); 7 8 int nNewPos = si.nPos; 9 switch (nSBCode) { 10 case SB_THUMBTRACK: 11 nNewPos = nPos; 12 break; 13 case SB_LINELEFT: 14 nNewPos -= 1; 15 break; 16 case SB_LINERIGHT: 17 nNewPos += 1; 18 break; 19 case SB_PAGELEFT: 20 nNewPos -= si.nPage; 21 break; 22 case SB_PAGERIGHT: 23 nNewPos += si.nPage; 24 break; 25 default: 26 break; 27 } 28 if (nNewPos > si.nMax) 29 nNewPos = si.nMax; 30 if (nNewPos < si.nMin) 31 nNewPos = si.nMin; 32 pScrollBar->SetScrollPos(nNewPos); 33 34 CString s; 35 s.Format(TEXT("%d"), nNewPos); 36 m_pos = s; 37 UpdateData(FALSE); 38 CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar); 39 }