关闭

为对话框设置水平,垂直条及鼠标移动滚动

1506人阅读 评论(0) 收藏 举报

为对话框设置水平,垂直条及鼠标移动滚动

1.       创建一个对话框,设置属性如下:

2.       添加对话框的WM_HSCROLL消息,在触发水平滚动消息时,根据触发消息(如单击左右滚动箭头、拖到滚动块、单击滚动区域等)不同,设置相应的滚动位置。

 

void CImagePanel::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)

{

       int nCurpos,nMin,nMax,nThumbWidth;    

       SCROLLINFO siInfo;                                                     //定义滚动条信息

       GetScrollInfo(SB_HORZ,&siInfo);                                  //获取滚动条信息

       nCurpos = siInfo.nPos;                                            //获取当前位置

       nMin = siInfo.nMin;                                                       //获取最小值

       nMax = siInfo.nMax;                                                      //获取最大滚动位置

       nThumbWidth = siInfo.nPage;                                         //获取滚动快大小

       switch (nSBCode)

       {

       break;

       case SB_THUMBTRACK: //拖动滚动块

              ScrollWindow(-(nPos-nCurpos),0);                    //滚动窗口

              SetScrollPos(SB_HORZ,nPos);                                 //设置水平滚动条的滚动位置

       break;

       case SB_LINELEFT : //单击左箭头

              SetScrollPos(SB_HORZ,nCurpos-1);                  //设置水平滚动条的滚动位置

              if (nCurpos != 0)

                     ScrollWindow(1,0);                                          //向左滚动一个位置

       break;

       case SB_LINERIGHT: //单击右箭头

              SetScrollPos(SB_HORZ,nCurpos+1);                 //设置水平滚动条的滚动位置

              if (nCurpos+nThumbWidth < nMax)

                     ScrollWindow(-1,0);                                         //向右滚动一个位置

       break;

       case SB_PAGELEFT: //在滚动块的左方空白滚动区域单击,增量为6

              SetScrollPos(SB_HORZ,nCurpos-6);

              if (nCurpos+nThumbWidth >0)

                     ScrollWindow(6,0);                                          //向左滚动6个单位

       break;

       case SB_PAGERIGHT: //在滚动块的右方空白滚动区域单击,增量为6

              SetScrollPos(SB_HORZ,nCurpos+6);                 //设置水平滚动条的滚动位置

              if (nCurpos+nThumbWidth <nMax)

                     ScrollWindow(-6,0);                                         //向右滚动6个单位

       break;

       case SB_LEFT:                                                              //滚动到最左边

              SetScrollPos(SB_HORZ,0);                               //设置滚动位置为0

              ScrollWindow(nCurpos,0);                                //设置窗口滚动位置

       break;

       }    

       CDialog::OnHScroll(nSBCode, nPos, pScrollBar);    

 

}

3.       添加对话框的WM_VSCROLL消息

void CImagePanel::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)

{

       int nCurpos,nMin,nMax,nThumbWidth;

       SCROLLINFO siInfo;

       GetScrollInfo(SB_VERT,&siInfo);

       nCurpos = siInfo.nPos;

       nMin = siInfo.nMin;

       nMax = siInfo.nMax;

       nThumbWidth = siInfo.nPage;

       switch (nSBCode)

       {

       case SB_THUMBTRACK:

              nCurpos = GetScrollPos(SB_VERT);

              ScrollWindow(0,-(nPos-nCurpos));

              SetScrollPos(SB_VERT,nPos);    

       break;

 

       case SB_LINELEFT:

              nCurpos = GetScrollPos(SB_VERT);

              SetScrollPos(SB_VERT,nCurpos-1);

              if (nCurpos !=0)

                     ScrollWindow(0,1);

       break;

       case SB_LINERIGHT:

              nCurpos = GetScrollPos(SB_VERT);

              SetScrollPos(SB_VERT,nCurpos+1);

              if (nCurpos+nThumbWidth < nMax)

                     ScrollWindow(0,-1);     

       break;

       case SB_PAGELEFT: //在滚动块的上方空白滚动区域单击,增量为6

              SetScrollPos(SB_VERT,nCurpos-6);

              if (nCurpos+nThumbWidth >0)

                     ScrollWindow(0,6);

       break;

       case SB_PAGERIGHT: //在滚动块的下方空白滚动区域单击,增量为6

              SetScrollPos(SB_VERT,nCurpos+6);

              if (nCurpos+nThumbWidth < nMax)

                     ScrollWindow(0,-6);

       break;

       case SB_LEFT:     

              SetScrollPos(SB_VERT,0);

              ScrollWindow(0,nCurpos);

       break;

       }    

       CDialog::OnVScroll(nSBCode, nPos, pScrollBar);

}

4.       添加对话框的WM_MOUSEWHEEL消息

//数量鼠标滚动时的时间,适当地滚动窗口

BOOL CImagePanel::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)

{

       SCROLLINFO siInfo;                                       //定义滚动信息对象

       GetScrollInfo(SB_VERT,&siInfo);                     //获取滚动信息

       int nMin,nMax,nThumbWidth;                          

       nMin = siInfo.nMin;                                         //获取滚动最小值

       nMax = siInfo.nMax;

       nThumbWidth = siInfo.nPage;             //获取滚动块大小

       int nPos = GetScrollPos(SB_VERT);       //获取垂直滚动条的当前位置

       if (zDelta > 0)                                           //向上滚动

       {

              if (nPos == 0)

                     return TRUE;

              SetScrollPos(SB_VERT,nPos-6);        //设置滚动条位置

              ScrollWindow(0,6);                   //向上滚动窗口

       }

       else

       {

              if ((nPos+nThumbWidth >= nMax))

                     return TRUE;

              SetScrollPos(SB_VERT,nPos+6);

              ScrollWindow(0,-6);

       }

       return TRUE;//CDialog::OnMouseWheel(nFlags, zDelta, pt);

}

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3214次
    • 积分:61
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档