在CRectView中加入如下代码:void CRectView::OnLButtonDown(UINT nFlags, CPoint point)
{
  CRectTracker temp;
  temp.TrackRubberBand(this,point,TRUE);
  temp.m_rect.NormalizeRect();
  CView::OnLButtonDown(nFlags, point);
}//上面这段代码可以实现这样的功能:按下鼠标,随着鼠标的移动画虚线框矩形。但是,当鼠标左键弹起时,虚线框就没了,我想要鼠标弹起时,虚线框仍然在。于是,我在void CRectView::OnLButtonUp(UINT nFlags, CPoint point)中添加如下代码:void CRectView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CRectTracker rect;
  rect.m_rect.SetRect(PtBegin.x,PtBegin.y,point.x,point.y);
  rect.Draw(pDC);
}为什么按上面做了后,鼠标弹起时仍然没有虚线框???

解决方案 »

  1.   

    你将temp.m_rect区域保存起来,然后Invalidate/InvalidateRect();在你的CRectView的OnDraw函数中去绘制图形
      

  2.   

    可以这样写:CRect m_rect; // 成员变量void CRectView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default CRectTracker temp;
    temp.TrackRubberBand(this,point,TRUE);
    temp.m_rect.NormalizeRect();
    m_rect.SetRect(temp.m_rect.TopLeft(), temp.m_rect.BottomRight());

    InvalidateRect(m_rect, TRUE);
    CView::OnLButtonDown(nFlags, point);
    }
    void CRectView::OnDraw(CDC* pDC)
    {
    CCRectDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here CPen pen(PS_DOT, 0, RGB(128, 0, 0));
    HPEN hOldPen = (HPEN)pDC->SelectObject(pen);
    pDC->Rectangle(m_rect); pDC->SelectObject(hOldPen);
    pen.DeleteObject();
    }
      

  3.   

    我解决了,跟你一样的问题。
    其实如果你在LButtonDown、LButtonUp这两个消息响应函数中分别插入一个AfxMessageBox,你就会发现,当你按下鼠标的时候LButtonDown响应了,可是如果你松掉鼠标时,却没有响应LButtonUp(其实MouseMove也没有响应),这是因为在你拖动“橡皮带”的时候,函数仍处于LButtonDown函数中,一直到你松掉鼠标,LButtonDown中的TrackRubberBand才结束,所以在你在按住鼠标左键的时候,不管你怎么拖动,你都还在TrackRubberBand里,也就是LButtonDown里,MouseMove一直都没有响应,LButtonUp也一直都得不到响应~~~~
    我的代码是这样的,话说自己研究这个2天了,才发现解决方法~~~唉~~CRectTracker  RectTracker(CRect(0,0,0,0),CRectTracker::resizeOutside|CRectTracker::dottedLine);
    RectTracker.TrackRubberBand(this,CurPoint,1);
    ::GetCursorPos(&CurPoint);
    ::ScreenToClient(m_hWnd,&CurPoint);
    TrackerRect = CRect(OldPoint.x,OldPoint.y,CurPoint.x,CurPoint.y);
    RectTracker.m_rect = CRect(OldPoint.x,OldPoint.y,CurPoint.x,CurPoint.y);
    RectTracker.Draw(pdc);
      

  4.   

    额,漏了~~~
    void Ctest5View::OnLButtonDown(UINT nFlags, CPoint point)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    //Ctest5Doc* pDC = (Ctest5Doc*)AfxGetMainWnd();
    //AfxMessageBox(TEXT("Donw"));         OldPoint = point;

            RectTracker(CRect(0,0,0,0),CRectTracker::resizeOutside|CRectTracker::dottedLine);
    RectTracker.TrackRubberBand(this,CurPoint,1);
    ::GetCursorPos(&CurPoint);
    ::ScreenToClient(m_hWnd,&CurPoint);
    TrackerRect = CRect(OldPoint.x,OldPoint.y,CurPoint.x,CurPoint.y);
    RectTracker.m_rect = CRect(OldPoint.x,OldPoint.y,CurPoint.x,CurPoint.y);
    RectTracker.Draw(pdc);
    pdc = GetDC();
                    CView::OnLButtonDown(nFlags, point);
    }