MFC对话框水平和垂直滚动条功能
作者:互联网
转载来自:https://blog.csdn.net/zhangchunbo135246/article/details/25234369
1.Resource View - Dialog - 主程序对话框 - 右键 - 属性 - Horizontal Scroll - true 和VerticalScroll - true;
2.Message - WM_HSCROLL :OnHScroll回车,跳到.cpp文件添加代码,同理WM_HSCROLL;
3.添加代码
void CMyBSplineDlg::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); 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; } Invalidate(); CDialog::OnHScroll(nSBCode, nPos, pScrollBar); } void CMyBSplineDlg::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; } Invalidate(); CDialog::OnVScroll(nSBCode, nPos, pScrollBar); }
标签:case,break,MFC,对话框,滚动条,nPos,SIF,SB,scrollinfo 来源: https://www.cnblogs.com/Galesaur-wcy/p/14184103.html