1.消息映射
ON_WM_HSCROLL()
2.初始化
BOOL CTextEditorDlg::OnInitDialog()
{
CScrollBar* csc = (CScrollBar*)GetDlgItem(IDC_SCROLLBAR1);
csc->SetScrollRange(1, 100);
csc->SetScrollPos(20);
SetDlgItemInt(IDC_EDIT2, 20);
return TRUE;
}
3.消息处理函数
afx_msg void CTextEditorDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int pos = pScrollBar->GetScrollPos();
switch (nSBCode)
{
case SB_LINELEFT:
pos -= 1;
break;
case SB_LINERIGHT:
pos += 1;
break;
case SB_PAGELEFT:
pos -= 10;
break;
case SB_PAGERIGHT:
pos += 10;
break;
case SB_LEFT:
pos = 1;
break;
case SB_RIGHT:
pos = 100;
break;
case SB_THUMBPOSITION:
pos = nPos;
break;
default:
SetDlgItemInt(IDC_EDIT2,pos);
return;
}
pScrollBar->SetScrollPos(pos);
CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}