CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 1999
    Posts
    505

    [RESOLVED] Drawing a rubber band rectangle

    Hi,
    I am trying to draw a rubber band rectangle. I have got a following code from a site but its giving an error:
    Code:
    class CSampleView: Public CView
    
    {
    
    ...
    
    public:
    
    CrectTracker m_tracker;//Changed to CRectTracker
    
    ...
    
    };
    
    Secondly, in the constructor to initialize the object CRectTracker document class:
    
    CSampleDoc :: CSampleDOC ()
    
    {
    
    // Initialize tracker position, size and style.
    
    m_tracker.m_rect.SetRect (0, 0, 10, 10);
    
    m_tracker.m_nStyle = CRectTracker :: resizeInside |
    
    CRectTracker :: dottedLine;
    
    }
    The error is:
    Code:
    error C2065: 'm_tracker' : undeclared identifier
    Can some body please guide me how to get rid of this error??

    Zulfi.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Drawing a rubber band rectangle

    m_tracker is a member of CSampleView. You're accessing it as if it were a member of CSampleDoc.

  3. #3
    Join Date
    Jun 1999
    Posts
    505

    Re: Drawing a rubber band rectangle

    Quote Originally Posted by GCDEF View Post
    m_tracker is a member of CSampleView. You're accessing it as if it were a member of CSampleDoc.
    Hi,
    Thanks for your reply. Actually its not my code. Should i be using view's constructor instead of document's constructor? Please guide me.

    Zulfi.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Drawing a rubber band rectangle

    That would probably make more sense. Typically you want your drawing in the view class.

  5. #5
    Join Date
    Jun 1999
    Posts
    505

    Re: Drawing a rubber band rectangle

    Quote Originally Posted by GCDEF View Post
    That would probably make more sense. Typically you want your drawing in the view class.
    Hi,
    Thanks for your advice. Its creating only one ellipse within a dotted rectangle.
    Code:
    Crubberband2View::Crubberband2View()
    {
    	// TODO: add construction code here
    	// Initialize tracker position, size and style.
    
    m_tracker.m_rect.SetRect (0, 0, 10, 10);
    
    m_tracker.m_nStyle = CRectTracker :: resizeInside | CRectTracker :: dottedLine;
    
    }
    
    Crubberband2View::~Crubberband2View()
    {
    }
    
    BOOL Crubberband2View::PreCreateWindow(CREATESTRUCT& cs)
    {
    	// TODO: Modify the Window class or styles here by modifying
    	//  the CREATESTRUCT cs
    
    	return CView::PreCreateWindow(cs);
    }
    
    // Crubberband2View drawing
    
    void Crubberband2View::OnDraw(CDC* pDC)
    {
    	Crubberband2Doc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    	CLine myLine;
    	CBrush brush (RGB (0, 0, 255));
    
    CBrush * pOldBrush = pDC-> SelectObject (&brush);
    
    // Draw ellipse in tracking rectangle.
    
    CRect rcEllipse;
    
     m_tracker.GetTrueRect (rcEllipse);
    
    pDC-> Ellipse (rcEllipse);
    
    // Draw tracking rectangle.
    
     m_tracker.Draw (pDC);
    
    // Select blue brush out of device context.
    
    pDC-> SelectObject (pOldBrush);
    	// TODO: add draw code for native data here
    }
    
    
    // Crubberband2View printing
    
    
    void Crubberband2View::OnFilePrintPreview()
    {
    #ifndef SHARED_HANDLERS
    	AFXPrintPreview(this);
    #endif
    }
    
    BOOL Crubberband2View::OnPreparePrinting(CPrintInfo* pInfo)
    {
    	// default preparation
    	return DoPreparePrinting(pInfo);
    }
    
    void Crubberband2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add extra initialization before printing
    }
    
    void Crubberband2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add cleanup after printing
    }
    
    void Crubberband2View::OnRButtonUp(UINT /* nFlags */, CPoint point)
    {
    	ClientToScreen(&point);
    	OnContextMenu(this, point);
    }
    
    void Crubberband2View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
    {
    #ifndef SHARED_HANDLERS
    	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
    #endif
    }
    
    
    // Crubberband2View diagnostics
    
    #ifdef _DEBUG
    void Crubberband2View::AssertValid() const
    {
    	CView::AssertValid();
    }
    
    void Crubberband2View::Dump(CDumpContext& dc) const
    {
    	CView::Dump(dc);
    }
    
    Crubberband2Doc* Crubberband2View::GetDocument() const // non-debug version is inline
    {
    	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Crubberband2Doc)));
    	return (Crubberband2Doc*)m_pDocument;
    }
    #endif //_DEBUG
    
    
    // Crubberband2View message handlers
    
    
    void Crubberband2View::OnLButtonDown(UINT nFlags, CPoint point)
    {
    	// TODO: Add your message handler code here and/or call default
    	// If clicked on ellipse, drag or resize it. Otherwise create a
    
    // Rubber-band rectangle nd create a new ellipse.
    
    BOOL bResult = m_tracker.HitTest (point)!= CRectTracker :: hitNothing;
    
    // Tracker rectangle changed so update views.
    
    if (bResult)
    
    {
    	Crubberband2Doc* pDoc = GetDocument();
     m_tracker.Track (this, point, TRUE);
    
     pDoc->SetModifiedFlag ();
    
     pDoc->UpdateAllViews (NULL);
    
    }
    
    else
    
     m_tracker.TrackRubberBand (this, point, TRUE);
    	CView::OnLButtonDown(nFlags, point);
    }
    i)Is it possible to create multiple rectangle with ellipses inside?
    ii) I want the dotted line to appear when i click inside the rectangle. But when i click outside the rectangle, the dotted line should disappear. How can i do this?

    Please guide me.

    Zulfi.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured