要点: 使用GetScrollPos() 获取当前滑块位置,使用SetScrollPos()设置滑块位置,使用ScrollWindow()来滚动客户区,
不要使用OnHScroll() 方法中的pScrollBar参数,因为对话框好像m_HWnd变量是空,所以这个参数的方法
pScrollBar->GetScrollPos() / pScrollBar->SetScrollPos() 有问题.
并且在对话框的属性页中有垂直/水平滚动条的属性选项,在这里添加的滚动条在项目中是看不到的ID的,在对话框上也不能选中添加 变量的.
BOOL CTestDlgDlg::OnInitDialog()
{
SetScrollRange(SB_HORZ, 0, 100);
SetScrollRange(SB_VERT, 0, 100);
}
void CTestDlgDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int nCurPos;
int nPrevPos;
CRect rect;
GetClientRect(&rect);
nPrevPos = GetScrollPos(SB_HORZ);
nCurPos = nPrevPos;
switch(nSBCode)
{
case SB_LEFT:
SetScrollPos(SB_HORZ, 0);
ScrollWindow(0, 0);
break;
case SB_RIGHT:
SetScrollPos(SB_HORZ, 100);
ScrollWindow(-rect.Width(), 0);
break;
case SB_PAGELEFT:
nCurPos = nPrevPos - 10;
if(nCurPos < 0)
nCurPos = 0;
SetScrollPos(SB_HORZ, nCurPos);
break;
case SB_PAGERIGHT:
nCurPos = nPrevPos + 10;
if(nCurPos > 100)
nCurPos = 100;
SetScrollPos(SB_HORZ, nCurPos);
break;
case SB_THUMBPOSITION:
SetScrollPos(SB_HORZ, nPos);
break;
case SB_THUMBTRACK:
break;
case SB_LINELEFT:
nCurPos = nPrevPos - 2;
if(nCurPos < 0)
nCurPos = 0;
SetScrollPos(SB_HORZ, nCurPos);
break;
case SB_LINERIGHT:
nCurPos = nPrevPos + 2;
if(nCurPos > 100)
nCurPos = 100;
SetScrollPos(SB_HORZ, nCurPos);
break;
case SB_ENDSCROLL:
break;
}
nCurPos = GetScrollPos(SB_HORZ);
ScrollWindow(rect.Width() * (nPrevPos - nCurPos) / 100, 0);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CTestDlgDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int nCurPos;
int nPrevPos;
CRect rect;
GetClientRect(&rect);
nPrevPos = GetScrollPos(SB_VERT);
nCurPos = nPrevPos;
switch(nSBCode)
{
case SB_LEFT:
SetScrollPos(SB_VERT, 0);
ScrollWindow(0, 0);
break;
case SB_RIGHT:
SetScrollPos(SB_VERT, 100);
ScrollWindow(-rect.Height(), 0);
break;
case SB_PAGELEFT:
nCurPos = nPrevPos - 10;
if(nCurPos < 0)
nCurPos = 0;
SetScrollPos(SB_VERT, nCurPos);
break;
case SB_PAGERIGHT:
nCurPos = nPrevPos + 10;
if(nCurPos > 100)
nCurPos = 100;
SetScrollPos(SB_VERT, nCurPos);
break;
case SB_THUMBPOSITION:
SetScrollPos(SB_VERT, nPos);
break;
case SB_THUMBTRACK:
break;
case SB_LINELEFT:
nCurPos = nPrevPos - 2;
if(nCurPos < 0)
nCurPos = 0;
SetScrollPos(SB_VERT, nCurPos);
break;
case SB_LINERIGHT:
nCurPos = nPrevPos + 2;
if(nCurPos > 100)
nCurPos = 100;
SetScrollPos(SB_VERT, nCurPos);
break;
case SB_ENDSCROLL:
break;
}
nCurPos = GetScrollPos(SB_VERT);
ScrollWindow(0, rect.Height() * (nPrevPos - nCurPos) / 100);
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}