前一段时间编了一个小程序,因要在对话框上设置滚动条,现就对话框上应用滚动条的一点经验给大家作个参考,如有不对之处望各位高手给予指证。

 

在对话框上要应用滚动条,首先当然是要将对话框的属性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);

}

以上代码完成后,滚动条已可以使用了。

 

但是应该如何将其它控件安排在对话框呢?很简单,你想用多大的对话框就将它拉多大,只需要在对话框所用的xxxx.cpp文件的void Cxxxx::DoDataExchange(CDataExchange* pDX)函数体中写入代码:

       SetWindowPos(NULL,0,0,800,600,SWP_NOMOVE | SWP_NOZORDER);

其中800和600两个数字分别为对话框创建时的宽度和高度。

这样就可以方便地使用滚动条了。


 

posted on 2011-06-03 19:41  maxweii  阅读(692)  评论(0编辑  收藏  举报