CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Drawing Line

  1. #1
    Join Date
    Jul 2005
    Posts
    62

    Drawing Line

    I have drawn a line using CClientDC in OnPaint() event..

    If I draw another line, the first line goes off...

    Can anybody give me solution for the above?

  2. #2
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: Drawing Line

    Where are you drawing the 2nd line? Do you have a code-example?

  3. #3
    Join Date
    Jul 2005
    Posts
    62

    Re: Drawing Line

    Thanks for your reply.....

    To draw a line, i am getting my first point in OnLButtonDown...
    When i move the mouse, I am drawing line from first point to the point where i move my mouse....

    But it is drawing wherever i move.. If I give Invalidate() function, it flickers.. How to solve the problem?

  4. #4
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: Drawing Line

    When you draw a line it's rendered to the Device Context (DC).
    When you invalidate it the entire contents are erased so your OnPaint function must repaint everything again including any lines you have previously drawn.
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

  5. #5
    Join Date
    Apr 2005
    Location
    Mumbai,India
    Posts
    185

    Re: Drawing Line

    I think u r selecting the co-ordinate when user press left mouse button and when user moves pointer, u r drawing & erasing line. You need not frequently call the Invalidate(), ( You may call InvalidateRect()).
    Supposing we have two CPoint object to denote the begin and end of line.
    CPoint begin,end;
    When user press left button, we initialize both (begin and end) with current value of co-oridantes.
    And, whenver user move mouse, we write the code in OnMouseMove() to
    1. Set the drawing mode R2_NOT using CDC::SetROP2.
    2. Draw the line with previous co-ordinate to erase the previous line
    3. Store the new co-ordinate in the end variable.
    4. Draw the line.

    And user release the mouse, we call InvalidateRect(). Rect area will determined by begin and end variables. and In OnDraw()/OnPaint() , we draw the line after setting the drawing mode R2_COPYPEN.

    In OnPaint(), don't use CClientDC, use instead CPaintDC to draw.
    Life is what u make it and u can make it more simple..........and woderful.
    Rate is what give and it give me pleasure.


  6. #6
    Join Date
    Jul 2005
    Posts
    62

    Re: Drawing Line

    I have written the below code in my project. (i am drawing this line in an image).

    It is getting flickered.

    ---------------------------------------------------------------------------------------------
    void MyClass::OnLButtonDown(UINT nFlags, CPoint point)
    {
    m_LMouseButton = TRUE;
    point1 = point;
    }

    void MyClass::OnMoveDown(UINT nFlags, CPoint point)
    {
    if( m_LMouseButton == TRUE)
    {
    CClientDC dc(this)
    dc.SetROP2(R2_NOT);
    dc.MoveTo(point1);
    dc.LineTo(point);

    CRect rect(point1,point);
    InvalidateRect(rect);

    }
    }

    void MyClass::OnLButtonUp(UINT nFlags, CPoint point)
    {
    if(m_LMouseButton == TRUE)
    {
    m_LMouseButton=FALSE;
    }
    }

  7. #7
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: Drawing Line

    You need to use double bufferring

    http://www.codeguru.com/Cpp/misc/mis...ticle.php/c389
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

  8. #8
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: Drawing Line

    Hello,

    For whatever you wanted to see displayed after invalidation, you should include it in OnPaint function. You are doing the draw in a function OnMoveDown, is it not so? Is that function being called from OnPaint? Where the end points of lines are stored? I feel that you have to store the line information as an array in document or view class and draw the lines accordingly in OnPaint. Whenever a new line is created using mouse events, that line information should be appended to the array.

    The tutorial example Scribble coming with VC++ explains the step-by-step method of creating an application on this line.

    Best luck.
    Pravin.
    21-07-2005.

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