View类中LButtonDown和LButtonUP重写的注意事项

在LButtonDown函数中最好添加 this->SetCapture();

 同时在LButtonUP函数中添加

if (GetCapture() == this)
 {
  ReleaseCapture();
 }

一旦在这两个函数里面添加了这两个功能,则MouseMove函数中的鼠标点坐标就可以是视口以外的坐标点。

在选中滑动视口中尤其重要。

选中 滑动代码:

//多选框
  if(m_bCreateNewDevice == false && m_SysAction == DRAW_RANGE_SELECT)
  {
     CPoint pt = cpoint;
     ClientToDoc(pt);

        CRect rectTotal(0,0, wholeRect.Width(), wholeRect.Height());
     if (!rectTotal.PtInRect(pt))
     {
        return;
     }
     CRect rectWnd(0, 0, 0, 0);
     GetClientRect(&rectWnd);
     ClientToDoc(rectWnd);

      CPoint ptScroll = GetScrollPosition();   

     if (rectTotal.Width() > rectWnd.Width())
     {
        CSize szGrid;
        szGrid.cx = m_iGridWidth;
        szGrid.cy = m_iGridHeight;
        DocToClient(szGrid);
        int iMoveCap = szGrid.cx;


      if ((rectWnd.right < pt.x  ))
      {
         /*此时,鼠标向右移动,并已移出了视图*/
         int iPos = GetScrollPos(SB_HORZ);
         if (iPos == wholeRect.Width())
         {
            return;
         }
         ptScroll.x += iMoveCap;
         ScrollToPosition(ptScroll);
         Invalidate(FALSE);

         ClientToDoc(szGrid);
         iMoveCap = szGrid.cx;
         pt.x += iMoveCap;
      }
      else if ( (rectWnd.left > pt.x))
      {
         int iPos = GetScrollPos(SB_HORZ);

         /*此时,鼠标向左移动,并已移出了视图*/
         ptScroll.x -= iMoveCap;
         ScrollToPosition(ptScroll);
         Invalidate(FALSE);

         ClientToDoc(szGrid);
         iMoveCap = szGrid.cx;
         pt.x -= iMoveCap;
      }
   }

   if (rectTotal.Height() > rectWnd.Height())
   {
      CSize szGrid;
      szGrid.cx = m_iGridWidth;
      szGrid.cy = m_iGridHeight;
      DocToClient(szGrid);

      int iMoveCap = szGrid.cy;  
      if ((rectWnd.bottom < pt.y))
      {
         /*此时,鼠标向下移动,并已移出了视图*/
         ptScroll.y += iMoveCap;
         ScrollToPosition(ptScroll);
         Invalidate(FALSE);

         ClientToDoc(szGrid);
         iMoveCap = szGrid.cy;
         pt.y += iMoveCap;
      }
      else if ((rectWnd.top > pt.y))
      {
         /*此时,鼠标向上移动,并已移出了视图*/
         ptScroll.y -= iMoveCap;
         ScrollToPosition(ptScroll);
         Invalidate(FALSE);

         ClientToDoc(szGrid);
         iMoveCap = szGrid.cy;
         pt.y -= iMoveCap;
      }
   }
   m_SelectRangeCurPoint = point;

 

鼠标捕获(setCapture)作用是将鼠标事件捕获到当前文档的指定的对象。这个对象会为当前应用程序或整个系统接收所有鼠标事件。
setCapture捕获以下鼠标事件:onmousedown、onmouseup、onclick、ondblclick、onmouseover和onmouseout。
程序中主要是要捕获onmousemove和onmouseup事件。
msdn的介绍中还说到setCapture有一个bool参数,用来设置在容器内的鼠标事件是否都被容器捕获。
容器就是指调用setCapture的对象,大概意思就是:
参数为true时(默认)容器会捕获容器内所有对象的鼠标事件,即容器内的对象不会触发鼠标事件(跟容器外的对象一样);
参数为false时容器不会捕获容器内对象的鼠标事件,即容器内的对象可以正常地触发事件和取消冒泡。
 
 
posted @ 2013-03-08 16:16  zhaogh990  阅读(276)  评论(0编辑  收藏  举报