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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. forcing repaint after CDC::TextOut function

forcing repaint after CDC::TextOut function

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelptutorialquestion
22 Posts 8 Posters 0 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.
  • G Offline
    G Offline
    goodoljosh1980
    wrote on last edited by
    #1

    Can someone explain to me how I get my dialog to repaint itself after I draw text on it via the CDC::TextOut command? Here is my code that I am using for drawing the text..... CFont Smaller_font; Smaller_font.CreateFont(40,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH | FF_DECORATIVE, "Arial"); def_font = dc.SelectObject(&Smaller_font); dc.SetTextColor(COLORREF RGB(0,0,0)); dc.TextOut(150, 130, "Thank you."); Smaller_font.DeleteObject(); This draws the "Thanks you" where I tell it, but if I have the program running and another window covers up the "Thank you." that I just drew, it erases it from my dialog. I am thinking that it is not forcing the repaint command somehow, but I am not sure how to force it. Please help....:(

    C M 2 Replies Last reply
    0
    • G goodoljosh1980

      Can someone explain to me how I get my dialog to repaint itself after I draw text on it via the CDC::TextOut command? Here is my code that I am using for drawing the text..... CFont Smaller_font; Smaller_font.CreateFont(40,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH | FF_DECORATIVE, "Arial"); def_font = dc.SelectObject(&Smaller_font); dc.SetTextColor(COLORREF RGB(0,0,0)); dc.TextOut(150, 130, "Thank you."); Smaller_font.DeleteObject(); This draws the "Thanks you" where I tell it, but if I have the program running and another window covers up the "Thank you." that I just drew, it erases it from my dialog. I am thinking that it is not forcing the repaint command somehow, but I am not sure how to force it. Please help....:(

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      do your painting in the dialog's OnPaint member function. Do the chickens have large talons?

      1 Reply Last reply
      0
      • G goodoljosh1980

        Can someone explain to me how I get my dialog to repaint itself after I draw text on it via the CDC::TextOut command? Here is my code that I am using for drawing the text..... CFont Smaller_font; Smaller_font.CreateFont(40,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH | FF_DECORATIVE, "Arial"); def_font = dc.SelectObject(&Smaller_font); dc.SetTextColor(COLORREF RGB(0,0,0)); dc.TextOut(150, 130, "Thank you."); Smaller_font.DeleteObject(); This draws the "Thanks you" where I tell it, but if I have the program running and another window covers up the "Thank you." that I just drew, it erases it from my dialog. I am thinking that it is not forcing the repaint command somehow, but I am not sure how to force it. Please help....:(

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Where is this code ? is in the OnPaint function of the dialog ?


        Maximilien Lincourt Your Head A Splode - Strong Bad

        G 2 Replies Last reply
        0
        • M Maximilien

          Where is this code ? is in the OnPaint function of the dialog ?


          Maximilien Lincourt Your Head A Splode - Strong Bad

          G Offline
          G Offline
          goodoljosh1980
          wrote on last edited by
          #4

          No, it's not in the OnPaint function. I am drawing text on the screen at different parts of the program, after the user clicks certain buttons.

          A S 2 Replies Last reply
          0
          • G goodoljosh1980

            No, it's not in the OnPaint function. I am drawing text on the screen at different parts of the program, after the user clicks certain buttons.

            A Offline
            A Offline
            Ali Rafiee
            wrote on last edited by
            #5

            Like Chris said you need to move that to the OnPaint message. void CDialog::OnPaint(...) { if (m_bDisplayThankYou) { DrawThankYou(pDC); } }

            1 Reply Last reply
            0
            • G goodoljosh1980

              No, it's not in the OnPaint function. I am drawing text on the screen at different parts of the program, after the user clicks certain buttons.

              S Offline
              S Offline
              Steve S
              wrote on last edited by
              #6

              Well, that's your problem. When the user clicks to trigger a draw, you need to modify some state data in your object. The OnPaint should then interpret what that data means, and provide an up-to-date set of paint operations. To trigger the repaint, invalidate your window. Steve S Developer for hire

              G 1 Reply Last reply
              0
              • S Steve S

                Well, that's your problem. When the user clicks to trigger a draw, you need to modify some state data in your object. The OnPaint should then interpret what that data means, and provide an up-to-date set of paint operations. To trigger the repaint, invalidate your window. Steve S Developer for hire

                G Offline
                G Offline
                goodoljosh1980
                wrote on last edited by
                #7

                I think I understand what you mean, but let me check. In the OnPaint() command of my dlg class, I need to have it check to see if, say a boolean value is true? If so, then do what? Also, what do you mean by... "To trigger the repaint, invalidate your window"

                H 1 Reply Last reply
                0
                • G goodoljosh1980

                  I think I understand what you mean, but let me check. In the OnPaint() command of my dlg class, I need to have it check to see if, say a boolean value is true? If so, then do what? Also, what do you mean by... "To trigger the repaint, invalidate your window"

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  why you dont use these function in wm_paint_**


                  **_

                  whitesky


                  G 1 Reply Last reply
                  0
                  • H Hamid Taebi

                    why you dont use these function in wm_paint_**


                    **_

                    whitesky


                    G Offline
                    G Offline
                    goodoljosh1980
                    wrote on last edited by
                    #9

                    what do you mean by this? I have tried... PostMessage(WMPAINT,0,0), but that didn't work either.

                    R H 2 Replies Last reply
                    0
                    • M Maximilien

                      Where is this code ? is in the OnPaint function of the dialog ?


                      Maximilien Lincourt Your Head A Splode - Strong Bad

                      G Offline
                      G Offline
                      goodoljosh1980
                      wrote on last edited by
                      #10

                      OK, Here is what I have tried now.... When the user clicks the button to trigger the drawing of text, I set a boolean value to true (RegKeyAvail). And I also implemented this... void CMartinPhDDlg::OnPaint() { CPaintDC dc(this); // device context for painting if(RegKeyAvail){ dc.SelectObject(&Welcome_font); dc.SetBkMode(TRANSPARENT); dc.SetTextColor(COLORREF RGB(255,0,0)); dc.TextOut(30,30, "WELCOME"); Welcome_font.DeleteObject(); } if (IsIconic()) {.... However, now the text isn't showing up at all. I know that the OnPaint is being called because I have set breakpoints in there and the functions walks through the dc.TextOut call, but does nothing......

                      C D 2 Replies Last reply
                      0
                      • G goodoljosh1980

                        OK, Here is what I have tried now.... When the user clicks the button to trigger the drawing of text, I set a boolean value to true (RegKeyAvail). And I also implemented this... void CMartinPhDDlg::OnPaint() { CPaintDC dc(this); // device context for painting if(RegKeyAvail){ dc.SelectObject(&Welcome_font); dc.SetBkMode(TRANSPARENT); dc.SetTextColor(COLORREF RGB(255,0,0)); dc.TextOut(30,30, "WELCOME"); Welcome_font.DeleteObject(); } if (IsIconic()) {.... However, now the text isn't showing up at all. I know that the OnPaint is being called because I have set breakpoints in there and the functions walks through the dc.TextOut call, but does nothing......

                        C Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #11

                        goodoljosh1980 wrote:

                        dc.SetTextColor(COLORREF RGB(255,0,0));

                        that compiles? and, where do you create Welcome_font ? Do the chickens have large talons?

                        G 1 Reply Last reply
                        0
                        • C Chris Losinger

                          goodoljosh1980 wrote:

                          dc.SetTextColor(COLORREF RGB(255,0,0));

                          that compiles? and, where do you create Welcome_font ? Do the chickens have large talons?

                          G Offline
                          G Offline
                          goodoljosh1980
                          wrote on last edited by
                          #12

                          Yeah, it compiles... That is how I set the color of the text I want. The Welcome_font is a member variable of the main dialog and is created in the BOOL CMartinPhDDlg::OnInitDialog() { CDialog::OnInitDialog(); GetWindowRect(m_rect); //Hide all diagrams to begin with top_diagram.ShowWindow(0); Intro_Stylus_Diagram.ShowWindow(0); Welcome_font.CreateFont(90, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DECORATIVE, "Times New Roman");

                          A 1 Reply Last reply
                          0
                          • G goodoljosh1980

                            Yeah, it compiles... That is how I set the color of the text I want. The Welcome_font is a member variable of the main dialog and is created in the BOOL CMartinPhDDlg::OnInitDialog() { CDialog::OnInitDialog(); GetWindowRect(m_rect); //Hide all diagrams to begin with top_diagram.ShowWindow(0); Intro_Stylus_Diagram.ShowWindow(0); Welcome_font.CreateFont(90, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DECORATIVE, "Times New Roman");

                            A Offline
                            A Offline
                            Ali Rafiee
                            wrote on last edited by
                            #13

                            are you calling Invalidate() UpdateWindow() after you set the boolean? By the way, don't delete the font object in the OnPaint method if it is not being recreated there. You are creating the font object once in your OnInitDialog but then deleting it everytime OnPaint runs.

                            G 2 Replies Last reply
                            0
                            • A Ali Rafiee

                              are you calling Invalidate() UpdateWindow() after you set the boolean? By the way, don't delete the font object in the OnPaint method if it is not being recreated there. You are creating the font object once in your OnInitDialog but then deleting it everytime OnPaint runs.

                              G Offline
                              G Offline
                              goodoljosh1980
                              wrote on last edited by
                              #14

                              I was calling Invalidate() in the OnPaint... Once I moved it...the text showed up and it doesn't get erased!!! THANKS TO EVERYONE!

                              S 1 Reply Last reply
                              0
                              • G goodoljosh1980

                                I was calling Invalidate() in the OnPaint... Once I moved it...the text showed up and it doesn't get erased!!! THANKS TO EVERYONE!

                                S Offline
                                S Offline
                                Steve S
                                wrote on last edited by
                                #15

                                That's because OnPaint is eventually called as a result of Invalidate(). By putting an invalidate in there, you said "I've drawn (it's valid)", swiftly followed by "it's invalid again". :) Steve S Developer for hire

                                G 1 Reply Last reply
                                0
                                • S Steve S

                                  That's because OnPaint is eventually called as a result of Invalidate(). By putting an invalidate in there, you said "I've drawn (it's valid)", swiftly followed by "it's invalid again". :) Steve S Developer for hire

                                  G Offline
                                  G Offline
                                  goodoljosh1980
                                  wrote on last edited by
                                  #16

                                  If that were true, couldn't I just put an Invalidate() in the program when I wanted it to repaint itself? That is, I call my TextOut functions...then tell it to Invalidate() (which eventually calls OnPaint())????

                                  A 1 Reply Last reply
                                  0
                                  • A Ali Rafiee

                                    are you calling Invalidate() UpdateWindow() after you set the boolean? By the way, don't delete the font object in the OnPaint method if it is not being recreated there. You are creating the font object once in your OnInitDialog but then deleting it everytime OnPaint runs.

                                    G Offline
                                    G Offline
                                    goodoljosh1980
                                    wrote on last edited by
                                    #17

                                    Thank you for the help. I do have an additional question, now though. I have a number of screens (100s of them) that I am going to be drawing line by line. Instead of having my OnPaint with all those boolean checks, can I call a function with CPaintDC as a member to draw them "outside" of the OnPaint()? Thanks again. Josh

                                    A 1 Reply Last reply
                                    0
                                    • G goodoljosh1980

                                      what do you mean by this? I have tried... PostMessage(WMPAINT,0,0), but that didn't work either.

                                      R Offline
                                      R Offline
                                      Ravi Bhavnani
                                      wrote on last edited by
                                      #18

                                      Move your drawing logic to the handler for the WM_PAINT message. Windows will send this message to your dialog when necessary. To force a repaint (uncommon), call Invalidate(). /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

                                      1 Reply Last reply
                                      0
                                      • G goodoljosh1980

                                        Thank you for the help. I do have an additional question, now though. I have a number of screens (100s of them) that I am going to be drawing line by line. Instead of having my OnPaint with all those boolean checks, can I call a function with CPaintDC as a member to draw them "outside" of the OnPaint()? Thanks again. Josh

                                        A Offline
                                        A Offline
                                        Ali Rafiee
                                        wrote on last edited by
                                        #19

                                        Yes you can. Pass the dc as a parameter to the method. AliR. Visual C++ MVP

                                        1 Reply Last reply
                                        0
                                        • G goodoljosh1980

                                          OK, Here is what I have tried now.... When the user clicks the button to trigger the drawing of text, I set a boolean value to true (RegKeyAvail). And I also implemented this... void CMartinPhDDlg::OnPaint() { CPaintDC dc(this); // device context for painting if(RegKeyAvail){ dc.SelectObject(&Welcome_font); dc.SetBkMode(TRANSPARENT); dc.SetTextColor(COLORREF RGB(255,0,0)); dc.TextOut(30,30, "WELCOME"); Welcome_font.DeleteObject(); } if (IsIconic()) {.... However, now the text isn't showing up at all. I know that the OnPaint is being called because I have set breakpoints in there and the functions walks through the dc.TextOut call, but does nothing......

                                          D Offline
                                          D Offline
                                          David Crow
                                          wrote on last edited by
                                          #20

                                          goodoljosh1980 wrote:

                                          if(RegKeyAvail){

                                          This check is actually not necessary. Much like the view renders whatever the document has in it, the OnPaint() handler should draw whatever value is in a member variable. If that value starts out as blank, so be it.

                                          void CMartinPhDDlg::OnPaint()
                                          {
                                          CPaintDC dc(this); // device context for painting
                                          dc.SelectObject(&Welcome_font);
                                          dc.SetBkMode(TRANSPARENT);
                                          dc.SetTextColor(COLORREF RGB(255,0,0));
                                          dc.TextOut(30,30, m_strText); // m_strText is set in various other methods

                                          // don't call DeleteObject() on a font that is currently selected into a DC 
                                          Welcome\_font.DeleteObject();
                                          

                                          }


                                          "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                                          "Judge not by the eye but by the heart." - Native American Proverb

                                          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