基于dialog 的MFC程序,在添加滚动条后发现滚动范围是将对话框最大化后的范围。试问应该如何限定这个范围?通过限定对话框的最大大小?还是修改scollinfo的属性?(单独修改nmax似乎不能实现)。以下是滚动条消息响应代码:
void CtestdialogDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
SCROLLINFO scrollinfo;
GetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
scrollinfo.fMask= SIF_ALL | SIF_DISABLENOSCROLL;
//scrollinfo.nMax/=2;
switch (nSBCode)
{
case SB_LEFT:
ScrollWindow((scrollinfo.nPos-scrollinfo.nMin)*10,0);
scrollinfo.nPos = scrollinfo.nMin;
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
break;
case SB_RIGHT:
ScrollWindow((scrollinfo.nPos-scrollinfo.nMax)*10,0);
scrollinfo.nPos = scrollinfo.nMax;
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
break;
case SB_LINELEFT:
scrollinfo.nPos -= 1;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(10,0);
break;
case SB_LINERIGHT:
scrollinfo.nPos += 1;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(-10,0);
break;
case SB_PAGELEFT:
scrollinfo.nPos -= 5;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(10*5,0);
break;
case SB_PAGERIGHT:
scrollinfo.nPos += 5;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(-10*5,0);
break;
case SB_THUMBPOSITION:
break;
case SB_THUMBTRACK:
ScrollWindow((scrollinfo.nPos-nPos)*10,0);
scrollinfo.nPos = nPos;
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
break;
case SB_ENDSCROLL:
break;
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CtestdialogDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
SCROLLINFO scrollinfo;
GetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
scrollinfo.fMask= SIF_ALL | SIF_DISABLENOSCROLL;
//scrollinfo.nMax/=2;
switch (nSBCode)
{
case SB_LEFT:
ScrollWindow((scrollinfo.nPos-scrollinfo.nMin)*10,0);
scrollinfo.nPos = scrollinfo.nMin;
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
break;
case SB_RIGHT:
ScrollWindow((scrollinfo.nPos-scrollinfo.nMax)*10,0);
scrollinfo.nPos = scrollinfo.nMax;
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
break;
case SB_LINELEFT:
scrollinfo.nPos -= 1;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(10,0);
break;
case SB_LINERIGHT:
scrollinfo.nPos += 1;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(-10,0);
break;
case SB_PAGELEFT:
scrollinfo.nPos -= 5;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(10*5,0);
break;
case SB_PAGERIGHT:
scrollinfo.nPos += 5;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
ScrollWindow(-10*5,0);
break;
case SB_THUMBPOSITION:
break;
case SB_THUMBTRACK:
ScrollWindow((scrollinfo.nPos-nPos)*10,0);
scrollinfo.nPos = nPos;
SetScrollInfo(SB_HORZ,&scrollinfo,SIF_ALL);
break;
case SB_ENDSCROLL:
break;
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}