CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2010
    Posts
    3

    problem in line plotting IN VC++

    Hi everyone,
    I am trying to do simple line plotting, i have used a CStaticCtrl on which i am trying to plot a line on a button click event,this works fine for the first time,when the button is pressed for the second time,the value is not updated,i mean the previous line is not erased and the new line is drawn


    My Code
    void CMainWindow::OnPumpOffClicked()
    {
    CRect rect;
    CDC *dc=m_pic1.GetDC();
    CPen pen,*oldpen;
    m_pic1.GetClientRect(&rect);// m_pic1 is variable for the static control
    //dc->Rectangle(&rect);
    pen.CreatePen(PS_SOLID,10,RGB(255,0,0));
    oldpen=dc->SelectObject(&pen);
    dc->SetMapMode(MM_ISOTROPIC);
    dc->SetViewportOrg(0, 148);
    dc->MoveTo(x1,y1);
    dc->LineTo(x2,y2);
    }


    Any help is appreciated

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: problem in line plotting IN VC++

    You must paint only in the response of the WM_PAINT message sent by the system.
    Derive a new class from the CStatic for your m_pic1, handle the WM_PAINT message in this class and use the device context that Framework generates for drawing (just let the IDE implement this message handler for you)

    Besides, avoid using GetDC(): it always required to call the ReleaseDC() after you don't need the DC anymore but people like you and me very often forget to release and therefore get the resource leaks.
    Use CCLientDC class instead.
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2010
    Posts
    41

    Re: problem in line plotting IN VC++

    Someone once taught me to create a new object inheriting from a CStatic control instead of directly using one. This way it works great when you send an erase-bkgrnd message!

  4. #4
    Join Date
    Jan 2010
    Posts
    3

    Unhappy Re: problem in line plotting IN VC++

    Hey,
    Thanks for the reply,sir i would be very kind of u if u please explain it more detail, i mean if u can just edit my code and show me in short,i know i am asking for too much from u,thanks again for spending time in reading ma query

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: problem in line plotting IN VC++

    It is impossible just "to edit" your code because it is wrong (wrong by design and also has a resource leaks)
    Do what was suggested:
    1. Create a new class derived from CStatic.
    2. Implement the WM_PAINT message handler in this class.
    3. Put some code for drawing in the implemented OnPaint() handler.
    4. Declare your m_pic1 member to be of this new derived class type rather than CStatic
    5. In CMainWindow::OnPumpOffClicked() write:
      Code:
      m_pic1.Invalidate();
      m_pic1.UpdateWindow();
      (perhaps, it will work without UpdateWindow() call too)
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2010
    Posts
    3

    Re: problem in line plotting IN VC++

    Thanks a lot sir, i got well through what u had said,only thing is the values which i am receiving is in my main dialog, take for example variables x,y which are updated every 2 secs,now my derived class is CMystatic ,i created a variable m_pic1 of this class and created the control using Create Method(),i in On Paint() of these class i wrote move to(x1,y1); line to(x2,y2)

    now every time when i would click button would the On Paint() would be called? coz x,y of main window have different values the next time it is clicked.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: problem in line plotting IN VC++

    1. You don't need to use "Create Method()" to create your static control. You could add it in the resource editor and then add the control member variable for it of the CMystatic type.

    2. Add meber variables to your CMystatic class like:
    Code:
    CPoint p1;
    CPoint p2;
    Then update them in the OnPumpOffClicked() handler and use in the CMystatic::OnPaint() for painting.
    Victor Nijegorodov

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