对话框自带滚动条的使用
摘自 http://wenku.baidu.com/link?url=aZe1zgBSBsf9xCYNpcz2fNGljmKxg372OVIGeJ7p6iRCWbbsertS7nA8sMUrv0JHak4T5hHpWqGHcUKRiefEcaU0GElq-1KexDvF1fwlJ-C
一,使用对话框窗口自带的滚动条,在属性页面中设置即可,如下
二,OnInitDialog()函数中,添加如下滚动条初始化语句
SCROLLINFO vinfo;
vinfo.cbSize =sizeof(vinfo);
vinfo.fMask =SIF_ALL;
vinfo.nPage =50;//滚动块自身的长短,通常有如下关系:其长度/滚动条长度(含两个箭头)=nPage/(nMax+2),
//另外nPage取值-1时,滚动条会不见了。
vinfo.nMax =600;//滚动条所能滚动的最大值
vinfo.nMin=0;//滚动条所能滚动的最小值
vinfo.nTrackPos =0;
SetScrollInfo(SB_VERT,&vinfo);//即使上述步骤一不做,使用此条语句也可以显示滚动条
三,添加相应的WM_VSCROLL函数
void CMy1d0Dlg::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);
}