在对话框上要应用滚动条,首先当然是要将对话框的属性vertical scroll选中(在这里只介绍纵向滚动条,横向滚动条与之原理相同)。然后是按Ctlr+w进入classwizard选中对话消息WM_OnVScroll,并添加它的响应函数,接着进入代码编辑区,写入代码:
void CRq::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
switch (nSBCode)
{
case SB_BOTTOM:
ScrollWindow(0,(scrollinfo.nPos-scrollinfo.nMax)*10);
scrollinfo.nPos = scrollinfo.nMax;
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
case SB_TOP:
ScrollWindow(0,(scrollinfo.nPos-scrollinfo.nMin)*10);
scrollinfo.nPos = scrollinfo.nMin;
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
case SB_LINEUP:
scrollinfo.nPos -= 1;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,10);
break;
case SB_LINEDOWN:
scrollinfo.nPos += 1;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,-10);
break;
case SB_PAGEUP:
scrollinfo.nPos -= 5;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,10*5);
break;
case SB_PAGEDOWN:
scrollinfo.nPos += 5;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,-10*5);
break;
case SB_ENDSCROLL:
// MessageBox("SB_ENDSCROLL");
break;
case SB_THUMBPOSITION:
// ScrollWindow(0,(scrollinfo.nPos-nPos)*10);
// scrollinfo.nPos = nPos;
// SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
case SB_THUMBTRACK:
ScrollWindow(0,(scrollinfo.nPos-nPos)*10);
scrollinfo.nPos = nPos;
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
}
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
按照网友的方法添加后并不能使用滑动条。之后我重新创建了基于对话框的工程[发现是有用的。img=http://img.bbs.csdn.net/upload/201507/31/1438339092_77145.jpg][/img]
------解决思路----------------------
滚动条和多文档或是单文档,没有关系
和是否模态窗口没有关系
操作滚动条一般需要2个方面,1,根据窗接收的滚动条消息设置滚动快;2,根据滚动大小设置窗口参数并重绘窗口
使用ScrollWindow(0,-10*5); 用于滚动当前窗口的内容,但是滚动后会留出的空白区一般必须处理
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);应该可以不用