Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need help on drawling line in OnPaint function

Need help on drawling line in OnPaint function

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John502
    wrote on last edited by
    #1

    Hi, I am able to draw vertical line on dialog, line will move from left to right for the period of time. while moving i am erasing the previous line with previous pixels and drawing new line with new pixels... but while the line is moving, if i have opened any other application on dialog box, the prevois line is not erasing and it is continue with the new line.. can any one help me why it is happening like this...? Thanks in advance.. Any advice will be appriciated..

    C I 2 Replies Last reply
    0
    • J John502

      Hi, I am able to draw vertical line on dialog, line will move from left to right for the period of time. while moving i am erasing the previous line with previous pixels and drawing new line with new pixels... but while the line is moving, if i have opened any other application on dialog box, the prevois line is not erasing and it is continue with the new line.. can any one help me why it is happening like this...? Thanks in advance.. Any advice will be appriciated..

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      What are you doing exactly in the OnPaint handler ? What you have to think is that windows doesn't remember the drawings for your window. So it means that whenever it needs to repaint the window (for instance because you have another window on top), then it will ask your window to repaint itself (by sending a WM_PAINT message). If you don't redraw your window properly, then you will loose some data. Also, the ebest way to erase a previous line your example is to simply request a new repainting of your window and draw the line at the new position (as everything is cleared, your previous line won't be visible anymore).

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      J 1 Reply Last reply
      0
      • C Cedric Moonen

        What are you doing exactly in the OnPaint handler ? What you have to think is that windows doesn't remember the drawings for your window. So it means that whenever it needs to repaint the window (for instance because you have another window on top), then it will ask your window to repaint itself (by sending a WM_PAINT message). If you don't redraw your window properly, then you will loose some data. Also, the ebest way to erase a previous line your example is to simply request a new repainting of your window and draw the line at the new position (as everything is cleared, your previous line won't be visible anymore).

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        J Offline
        J Offline
        John502
        wrote on last edited by
        #3

        Hi cedric, Thanks for your reply.. actually i am drawing line along with the slider positions on dialog, i mean slider will move from left to right, that time along with slider positions the line will also move. I have written the below code in one thread.. m_pDlg->GetClientRect( &deflatedClientRect ); deflatedClientRect.DeflateRect( TB_WIDTH, TB_WIDTH ); m_pDlg->m_slider_bar1.SetPos(slide_pos); m_pDlg->m_slider_bar2.SetPos(slide_pos); m_pDlg->m_slider_bar1.GetThumbRect( &thumbRect ); m_pDlg->m_slider_bar1.ClientToScreen( &thumbRect ); m_pDlg->ScreenToClient( &thumbRect ); ptStart.x = thumbRect.CenterPoint().x; ptStart.y = TB_WIDTH; //ptEnd.x = ptStart.x; //ptEnd.y = deflatedClientRect.bottom; m_pDlg->x1 = ptStart.x; m_pDlg->x2 = ptStart.x; //m_pDlg->x2 = ptEnd.x; //m_pDlg->y2 = 280; m_pDlg->InvalidateRect(deflatedClientRect,TRUE); using this code i will get sliders pixels information, and i am calling InavalidateRect from here. In Onpaint i am using below code snippet.. int nOldmode=dc.SetROP2(R2_NOTXORPEN); dc.MoveTo(old_x1,old_y1); dc.LineTo(old_x2,old_y2); dc.MoveTo(x1,y1); dc.LineTo(x2,y2); old_x1 = x1; old_y1 = y1; old_x2 = x2; old_y2 = y2; plase check the code and let me know that is the problem..> thnaks in advance..

        C 1 Reply Last reply
        0
        • J John502

          Hi cedric, Thanks for your reply.. actually i am drawing line along with the slider positions on dialog, i mean slider will move from left to right, that time along with slider positions the line will also move. I have written the below code in one thread.. m_pDlg->GetClientRect( &deflatedClientRect ); deflatedClientRect.DeflateRect( TB_WIDTH, TB_WIDTH ); m_pDlg->m_slider_bar1.SetPos(slide_pos); m_pDlg->m_slider_bar2.SetPos(slide_pos); m_pDlg->m_slider_bar1.GetThumbRect( &thumbRect ); m_pDlg->m_slider_bar1.ClientToScreen( &thumbRect ); m_pDlg->ScreenToClient( &thumbRect ); ptStart.x = thumbRect.CenterPoint().x; ptStart.y = TB_WIDTH; //ptEnd.x = ptStart.x; //ptEnd.y = deflatedClientRect.bottom; m_pDlg->x1 = ptStart.x; m_pDlg->x2 = ptStart.x; //m_pDlg->x2 = ptEnd.x; //m_pDlg->y2 = 280; m_pDlg->InvalidateRect(deflatedClientRect,TRUE); using this code i will get sliders pixels information, and i am calling InavalidateRect from here. In Onpaint i am using below code snippet.. int nOldmode=dc.SetROP2(R2_NOTXORPEN); dc.MoveTo(old_x1,old_y1); dc.LineTo(old_x2,old_y2); dc.MoveTo(x1,y1); dc.LineTo(x2,y2); old_x1 = x1; old_y1 = y1; old_x2 = x2; old_y2 = y2; plase check the code and let me know that is the problem..> thnaks in advance..

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Did you read my previous message ? Why are you repainting the old line ?

          venki502 wrote:

          I have written the below code in one thread..

          You are doing that in a separate thread ? :wtf: I wouldn't do that: instead simply handle the slider moved event and update the position of your line at that point. Why do you need a thread for that ?

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          J 1 Reply Last reply
          0
          • C Cedric Moonen

            Did you read my previous message ? Why are you repainting the old line ?

            venki502 wrote:

            I have written the below code in one thread..

            You are doing that in a separate thread ? :wtf: I wouldn't do that: instead simply handle the slider moved event and update the position of your line at that point. Why do you need a thread for that ?

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            J Offline
            J Offline
            John502
            wrote on last edited by
            #5

            Hi, Actualli i am setting the slider position in OnTimer function, Okay can you please tell me the event fired at silder movement, so that i will try with that.. Thanks.

            1 Reply Last reply
            0
            • J John502

              Hi, I am able to draw vertical line on dialog, line will move from left to right for the period of time. while moving i am erasing the previous line with previous pixels and drawing new line with new pixels... but while the line is moving, if i have opened any other application on dialog box, the prevois line is not erasing and it is continue with the new line.. can any one help me why it is happening like this...? Thanks in advance.. Any advice will be appriciated..

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              This is getting painful to read. 1/ In your thumbtrack handler, just call InvalidateRect () to cause the window to redraw. 2/ In the OnPaint routine, get the thumb positions, calculate your two end points for the line. Draw the line. Go home and sleep. It's not a complex problem. I have no idea why you're bringing in threads, etc. If you want, you could be clever and use R2_NOT to undraw a line, and draw it in the new position, but that seems a little over complex at the moment. Good luck, Iain.

              Codeproject MVP for C++, I can't believe it's for my lounge posts...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups