CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Erasing lines

  1. #1
    Join Date
    Aug 2003
    Posts
    162

    Erasing lines

    erm i have read through all the post about erasing line.. and this is what i did...

    After i draw the line, a dialog appear and after OnOk,
    i set SetROP2 to R2_NOP and draw the line in same position.. but the line is still there. I have tried a number of flags for the SetROP2 but it has no effect.

    I even tried to create a CPen with same background color and draw the line, it doesn works either.

    Anyone knows where do i go wrong?
    0 error(s), 0 warning(s)

  2. #2
    Join Date
    May 2002
    Posts
    12
    Have you invalidated the region or rect you want updated? Also, if you are creating an ActiveX control you should call FireViewChange( ) to update.

  3. #3
    Join Date
    Aug 2003
    Posts
    162
    i have tried but the line is still seen. ;(
    0 error(s), 0 warning(s)

  4. #4
    Join Date
    Nov 1999
    Location
    Beijing, P.R.China
    Posts
    115
    Post your code may be helpful.
    If it helps you, thanks for rating.

  5. #5
    Join Date
    Apr 2002
    Location
    Australia
    Posts
    54
    Hi,


    I have done a similar thing using either:

    SetROP2(R2_NOTXORPEN)
    or
    SetROP2(R2_XORPEN)

    You should call this before you draw the line both times. You will also need to set the pen color first.

    Does that work for you?
    If not, please post your code as suggested.

    thanks,
    Em
    Last edited by Emster; October 17th, 2003 at 02:17 AM.

  6. #6
    Join Date
    Aug 2003
    Posts
    162
    Code:
    void CtestView::OnMouseMove(UINT nFlags, CPoint point) 
    {
            if (move)
            {
                    CClientDC ClientDC (this);
                    ClientDC.SetROP2(R2_NOTXORPEN);
    
                    DrawLine(&ClientDC, start_point, point);
             }
    }
    start_point stores the point on mousedown

    Code:
    void DrawLine(CDC *pDC, CPoint m_pOne, CPoint m_pTwo)
    {
             pDC->MoveTo(m_pOne);
             pDC->LineTo(m_pTwo);
    }
    on mouseup, i call a dialog.DoModal()....

    Code:
    void CLineDialog::OnOK() 
    {
             CDialog::OnOK();
             CDC *pDC = GetDC();
             pDC->SetROP2(R2_NOTXORPEN);
    
             CPen Pen;
             Pen.CreatePen(PS_SOLID,1,RGB(255,255,255));
             pDC->SelectObject(&Pen);
    
             pDC->MoveTo(pt_x1, pt_y1);
             pDC->LineTo(pt_x2, pt_y2);
    }
    pt_x1 ... etc is global variable which i store the point.x and point.y on both mousedown and mouseup....

    where did i make mistake??

    Thanks a lot..
    0 error(s), 0 warning(s)

  7. #7
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Well, you are drawing to the dialog's DC using GetDC() - that' can't possibly work...

  8. #8
    Join Date
    Aug 2003
    Posts
    162
    !! how do i get the CView dc? or should i draw at OnDraw ;\
    0 error(s), 0 warning(s)

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by ayumi
    !! how do i get the CView dc? or should i draw at OnDraw ;\
    Yes. Always and only in OnDraw().

  10. #10
    Join Date
    Aug 2003
    Posts
    162
    but i have tried doing so in OnDraw and i do a Invalidate() after the CDialog::OnOK(); .. the line is still there ........

    Anyone has any better ideas??

    Thanks.
    0 error(s), 0 warning(s)

  11. #11
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Originally posted by ayumi
    but i have tried doing so in OnDraw and i do a Invalidate() after the CDialog::OnOK(); .. the line is still there ........
    You mean, you moved the entire code of drawing from OnOk to OnDraw and it still doesn't work ?

    BTW, I like your signature...
    0 errors, 0 warnings... so funny

  12. #12
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    There should be no need to redraw the line using R2_NOTXORPEN after the dialog is closed. Basically, you erase / draw the line in OnMouseMove(), giving a rubber banding effect while the mouse is moved. On MouseUp, you display the dialog. When the dialog is closed, you call Invalidate(). Now OnDraw() will be called, where you redraw your data - without the line.

  13. #13
    Join Date
    Aug 2003
    Posts
    162
    i'm not sure why.. even if i don't redraw the lines, after onok, i do a Invalidate(); , the line is still seen -_- it didn erase my background, or how should i erase the background? ;\
    0 error(s), 0 warning(s)

  14. #14
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Normally, you don't need to do extra background erasing (but you can by implementing a WM_ERASEBKGND handler). There seems to be something else wrong with your code. Can you post the code for
    • your view's class definition from the .h file,
    • your implementation of OnDraw(),
    • the place where you call Invalidate()?

  15. #15
    Join Date
    Aug 2003
    Posts
    162
    Code:
    class CtestView : public CView
    {
    	
    protected: // create from serialization only
    	
    	DECLARE_DYNCREATE(CtestView)
    
    // Attributes
    public:
    	CtestView();
    	CtestDoc* GetDocument();
    // Operations
    public:
    
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CtestView)
    	public:
    	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    	virtual void OnInitialUpdate();
    	protected:
    	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    	//}}AFX_VIRTUAL
    
    // Implementation
    public:
                   virtual ~CtestView();
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    
    protected:
    // Generated message map functions
    protected:
    	//{{AFX_MSG(CtestView)
                    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
                    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
    	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    Code:
    void CtestView::OnMouseMove(UINT nFlags, CPoint point) 
    {
            if (move)
            {
                    CClientDC ClientDC (this);
                    ClientDC.SetROP2(R2_NOTXORPEN);
    
                    DrawLine(&ClientDC, start_point, point);
             }
    
             CView::OnMouseMove(nFlags, point);
    }
    Code:
    void DrawLine(CDC *pDC, CPoint m_pOne, CPoint m_pTwo)
    {
             pDC->MoveTo(m_pOne);
             pDC->LineTo(m_pTwo);
    }
    Code:
    void CtestView::OnLButtonUp(UINT nFlags, CPoint point) 
    {
              CLineDialog dlg;
              dlg.DoModal();
    
              CView::OnLButtonUp(nFlags, point);
    }
    Code:
    void CLineDialog::OnOK() 
    {
             CDialog::OnOK();
             EraseLine = true;
             Invalidate();
    }
    Code:
    void CtestView::OnDraw(CDC* pDC)
    {
              CtestDoc* pDoc = GetDocument();
              ASSERT_VALID(pDoc);
              if(EraseLine) 
              {
                     CPen Pen;
                     Pen.CreatePen(PS_SOLID,1,RGB(0,0,100));
                     pDC->SelectObject(&Pen);
    
                     pDC->MoveTo(pt_x1, pt_y1);
                     pDC->LineTo(pt_x2, pt_y2);
                     EraseLine = false;
               }
              ......
              ......
              //Code to draw back image... too long to post.
    0 error(s), 0 warning(s)

Page 1 of 2 12 LastLast

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