最近在整理一个别人的MFC程序的时候,发现对话框太大了,屏幕显示不下,最后决定加一个对话框自带的滚动条,以垂直滑动条为例,具体操作如下:
1.添加垂直滑动条:右键对话框——>属性,将Vertical Scrollbar状态改为True。
2.添加响应函数:右键对话框——>添加类向导
3.在OnInitDialog()函数中添加
SCROLLINFO scrollinfo; GetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); scrollinfo.nPage=10; //设置滑块大小 scrollinfo.nMax=100; //设置滚动条的最大位置0–100 SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
4 .在OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)消息处理函数中添加:
void CChangeonDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) //对话框的滚动效果 { // TODO: 在此添加消息处理程序代码和/或调用默认值 SCROLLINFO scrollinfo; GetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); int unit=3; switch (nSBCode) { case SB_LINEUP: //Scroll one line up scrollinfo.nPos -= 1; if (scrollinfo.nPos<scrollinfo.nMin) { scrollinfo.nPos = scrollinfo.nMin; break; } SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); ScrollWindow(0,unit); break; case SB_LINEDOWN: //Scroll one line down scrollinfo.nPos += 1; if (scrollinfo.nPos+scrollinfo.nPage>scrollinfo.nMax) //此处一定要注意加上滑块的长度,再作判断 { scrollinfo.nPos = scrollinfo.nMax; break; } SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); ScrollWindow(0,-unit); break; case SB_PAGEUP: //Scroll one page up. scrollinfo.nPos -= 5; if (scrollinfo.nPos<=scrollinfo.nMin) { scrollinfo.nPos = scrollinfo.nMin; break; } SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); ScrollWindow(0,unit*5); break; case SB_PAGEDOWN: //Scroll one page down scrollinfo.nPos += 5; if (scrollinfo.nPos+scrollinfo.nPage>=scrollinfo.nMax) //此处一定要注意加上滑块的长度,再作判断 { scrollinfo.nPos = scrollinfo.nMax; break; } SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); ScrollWindow(0,-unit*5); break; case SB_ENDSCROLL: //End scroll break; case SB_THUMBPOSITION: //Scroll to the absolute position. The current position is provided in nPos break; case SB_THUMBTRACK: //Drag scroll box to specified position. The current position is provided in nPos ScrollWindow(0,(scrollinfo.nPos-nPos)*unit); scrollinfo.nPos = nPos; SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); break; } CDialog::OnVScroll(nSBCode, nPos, pScrollBar); }
5 .效果展示:
戳戳小手帮忙点个免费的赞和关注吧,嘿嘿。 |