您现在的位置: 天下网吧 >> 网吧天地 >> 天下码农 >> 微信小程序 >> 正文

积累的VC编程小技巧之滚动条

2011-1-5vczx佚名

1.设置滚动条的滚动大小

创建一个基于CScrollviewSDI Project(在第6步中选CScrollview
若你已创建了,这步可以省略。
然后:
改为如
void CTestView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();

CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = 1024;  //改这两个
sizeTotal.cy = 768;   //
SetScrollSizes(MM_TEXT, sizeTotal);
}

 

2.滚动条的控制

BOOL CDiagramShowView::PreTranslateMessage(MSG* pMsg)

{

       CFileTreeDoc* pDoc = (CFileTreeDoc*)GetDocument();

       CPoint point = GetScrollPosition();

      

       if(pMsg->message == WM_KEYDOWN)

       {

              switch(pMsg->wParam)

              {

              case VK_LEFT:

                     if( point.x > 10)

                     {

                           EndPoint.x = EndPoint.x - 10;

                           EndPoint.y = EndPoint.y;

                     }

                     else

                     {

                           EndPoint.x = 0;

                           EndPoint.y = EndPoint.y;

                     }

                     ScrollToPosition(EndPoint);

                     InvalidateRect(NULL,TRUE);

                     break;

              case VK_RIGHT:

                     if( point.x < pDoc->intDiagramColumnCount * pDoc->intColumnWidth - 10 )

                     {

                           EndPoint.x = EndPoint.x + 10;

                           EndPoint.y = EndPoint.y;

                     }

                     else

                     {

                           EndPoint.y = pDoc->intDiagramColumnCount * pDoc->intColumnWidth;

                           EndPoint.x = EndPoint.x;

                     }

                     ScrollToPosition(EndPoint);

                     InvalidateRect(NULL,TRUE);

                     break;

              case VK_UP:

                     if( point.y > 10)

                     {

                           EndPoint.y = EndPoint.y - 10;

                           EndPoint.x = EndPoint.x;

                     }

                     else

                     {

                           EndPoint.y = 0;

                           EndPoint.x = EndPoint.x;

                     }

                     ScrollToPosition(EndPoint);

                     InvalidateRect(NULL,TRUE);

                     break;

              case VK_DOWN:

                     if( point.y < pDoc->intDiagramRowCount * pDoc->intRowHeight - 10 )

                     {

                           EndPoint.y = EndPoint.y + 10;

                           EndPoint.x = EndPoint.x;

                     }

                     else

                     {

                           EndPoint.y = pDoc->intDiagramRowCount * pDoc->intRowHeight;

                           EndPoint.x = EndPoint.x;

                     }

                     ScrollToPosition(EndPoint);

                     InvalidateRect(NULL,TRUE);

                     break;

              default:

                     break;

              }

       }

       return FALSE;

}

 

 

// 通过正负号判断是向上还是向下滚动

if(zDelta==120) 

向上滚动
if(zDelta==-120)
向下滚动

 

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

{

       CFileTreeDoc* pDoc = (CFileTreeDoc*)GetDocument();

       CPoint point = GetScrollPosition();

      

       if(zDelta==120)

       {

              if( point.y >= 20 )

              {

                     EndPoint.x = point.x;

                     EndPoint.y = point.y;

                    

                     EndPoint.x = EndPoint.x;

                     EndPoint.y = EndPoint.y - 20;

              }

              else

              {

                     EndPoint.x = EndPoint.x;

                     EndPoint.y = 0;

              }

       }

      

       if(zDelta==-120)

       {

              if( point.y <= pDoc->intDiagramRowCount * pDoc->intRowHeight - 20 )

              {

                     EndPoint.x = point.x;

                     EndPoint.y = point.y;

                    

                     EndPoint.x = EndPoint.x;

                     EndPoint.y = EndPoint.y + 20;

              }

              else

              {

                     EndPoint.x = EndPoint.x;

                     EndPoint.y = EndPoint.y;

              }

       }

      

       ScrollToPosition(EndPoint);

       InvalidateRect(NULL,TRUE);

       return CScrollView::OnMouseWheel(nFlags, zDelta, pt);

}

 

3.给从CWnd派生的窗口添加滚动条

ModifyStyle(0,WS_VSCROLL);

 

4.如何用键盘滚动分割的视口

我的问题是当我用鼠标滚动分割窗口时,视口滚动都很正常,但用键盘时,却什么也没有发生.

在你的视图继承类中加入如下两个函数,假定该类为CScrollerView:

void CScrollerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
        BOOL processed;
        for (unsigned int i=0;i< nRepCnt&&processed;i++)
                processed=KeyScroll(nChar);
        if (!processed)
           CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CScrollerView::KeyScroll(UINT nChar)
{
        switch (nChar)
                {
                case VK_UP:
                        OnVScroll(SB_LINEUP,0,NULL);
                        break;
                case VK_DOWN:
                        OnVScroll(SB_LINEDOWN,0,NULL);
                        break;
                case VK_LEFT:
                        OnHScroll(SB_LINELEFT,0,NULL);
                        break;
                case VK_RIGHT:
                        OnHScroll(SB_LINERIGHT,0,NULL);
                        break;
                case VK_HOME:
                        OnHScroll(SB_LEFT,0,NULL);
                        break;
                case VK_END:
                        OnHScroll(SB_RIGHT,0,NULL);
                        break;
                case VK_PRIOR:
                        OnVScroll(SB_PAGEUP,0,NULL);
                        break;
                case VK_NEXT:
                        OnVScroll(SB_PAGEDOWN,0,NULL);
                        break;
                default:
                        return FALSE; // not for us
                             // and let the default class
                             // process it.
                }
   return TRUE;
}

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛 https://bbs.txwb.com

关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩

本文来源:vczx 作者:佚名

声明
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系邮箱:support@txwb.com,系统开号,技术支持,服务联系QQ:1175525021本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧 网吧天下