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. CDC DrawText

CDC DrawText

Scheduled Pinned Locked Moved C / C++ / MFC
questionannouncement
8 Posts 3 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
    gurucplusplus
    wrote on last edited by
    #1

    Hi, I use CDC* pDC->DrawText(" number ") in OnDraw() to update text to a frame. I then call InvalidateRect(NULL,FALSE) to display the text. What is the better way of display text without calling InvalidateRect() because OnDraw() with InvalidateRect() slow down application quite a lot. Thanks.

    M M 2 Replies Last reply
    0
    • G gurucplusplus

      Hi, I use CDC* pDC->DrawText(" number ") in OnDraw() to update text to a frame. I then call InvalidateRect(NULL,FALSE) to display the text. What is the better way of display text without calling InvalidateRect() because OnDraw() with InvalidateRect() slow down application quite a lot. Thanks.

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      If you're doing this right you should not have to call InvalidateRect(). OnDraw only gets called when some Rect is alreay invalid, that's why windows generated the WM_PAINT to get you to call OnDraw in the first place. If your text doesn't appear without this then something else is wrong. I'm not sure what you mean by

      gurucplusplus wrote:

      update text to a frame

      . With most windows you can call SetWindowText to set the text that's displayed. If this is a custom window class or an owner draw control then DrawText might be right:~

      Nothing is exactly what it seems but everything with seems can be unpicked.

      1 Reply Last reply
      0
      • G gurucplusplus

        Hi, I use CDC* pDC->DrawText(" number ") in OnDraw() to update text to a frame. I then call InvalidateRect(NULL,FALSE) to display the text. What is the better way of display text without calling InvalidateRect() because OnDraw() with InvalidateRect() slow down application quite a lot. Thanks.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        You're causing recursive repeated calls to OnDraw() by calling InvalidateRect() from within OnDraw(). That'll certainly slow things down :)

        gurucplusplus wrote:

        What is the better way of display text without calling InvalidateRect()

        Don't call InvalidateRect()! Mark -- modified at 14:29 Thursday 7th June, 2007

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        G 1 Reply Last reply
        0
        • M Mark Salsbery

          You're causing recursive repeated calls to OnDraw() by calling InvalidateRect() from within OnDraw(). That'll certainly slow things down :)

          gurucplusplus wrote:

          What is the better way of display text without calling InvalidateRect()

          Don't call InvalidateRect()! Mark -- modified at 14:29 Thursday 7th June, 2007

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

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

          How can I update this text to graphic without calling invalidateRect()?

          M 1 Reply Last reply
          0
          • G gurucplusplus

            How can I update this text to graphic without calling invalidateRect()?

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            When you call DrawText, you ARE updating the text. You use Invalidate methods to force a repaint from outside of OnPaint/OnDraw. For example, if the text you are drawing changes as the result of some process in the application and you want the window updated to reflect the change, then you'd call Invalidate. This will mark the given area of the window as invalid. On the next WM_PAINT message (which will call OnDraw() for CViews) you will call DrawText() with the updated text. I hope that made sense :) Mark

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            G 1 Reply Last reply
            0
            • M Mark Salsbery

              When you call DrawText, you ARE updating the text. You use Invalidate methods to force a repaint from outside of OnPaint/OnDraw. For example, if the text you are drawing changes as the result of some process in the application and you want the window updated to reflect the change, then you'd call Invalidate. This will mark the given area of the window as invalid. On the next WM_PAINT message (which will call OnDraw() for CViews) you will call DrawText() with the updated text. I hope that made sense :) Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              G Offline
              G Offline
              gurucplusplus
              wrote on last edited by
              #6

              So, If my text drawn changes I have to call invalidateRect to mark invalid for the next WM_PAINT message to update the text. Where should I call invalidaterect then? Not in ondraw()? thanks

              M 1 Reply Last reply
              0
              • G gurucplusplus

                So, If my text drawn changes I have to call invalidateRect to mark invalid for the next WM_PAINT message to update the text. Where should I call invalidaterect then? Not in ondraw()? thanks

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                gurucplusplus wrote:

                If my text drawn changes I have to call invalidateRect to mark invalid for the next WM_PAINT message to update the text. Where should I call invalidaterect then?

                Right. If the text characters never change (like pDC->DrawText("Text") then there's no reason to ever call InvalidateRect() because every time the window needs repainting the text will just be redrawn. Now suppose you are drawing the text of a string that changes (pDC->DrawText(str)). The user does something that causes the value of str to change and you want to reflect that change in the window. Any time after you change str, call InvalidateRect() to mark the area as invalid. When the resulting WM_PAINT is received by the window, CView::OnPaint() will call OnDraw(). In your OnDraw() override, you call pDC->DrawText(str), drawing the new text on the window. Also remember, WM_PAINT messages are LOW priority (they can get bumped to the back of the message queue). If you need an instant update, like if the text string is changing as the result of a timer (like in a clock implementation), then you can add a call to UpdateWindow() right after the call to InvalidateRect(). This will cause a WM_PAINT to be sent immediately. Remember that, when responding to WM_PAINT, it is your responsibility to validate the region of the window that is invalid to prevent repeated WM_PAINT messages. Most of the time this is done for us by MFC or the default window message handling. So if you invalidate regions of a window while it's trying to validate it, the WM_PAINT messages keep coming. If you want to see the recursive action/result of doing this in your OnDraw(), try this in your OnDraw(). This consumes all the CPU time of one of my processors and renders dialog controls useless: Invalidate(); UpdateWindow(); return; I'm actually surprised it doesn't crash :) Mark

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                G 1 Reply Last reply
                0
                • M Mark Salsbery

                  gurucplusplus wrote:

                  If my text drawn changes I have to call invalidateRect to mark invalid for the next WM_PAINT message to update the text. Where should I call invalidaterect then?

                  Right. If the text characters never change (like pDC->DrawText("Text") then there's no reason to ever call InvalidateRect() because every time the window needs repainting the text will just be redrawn. Now suppose you are drawing the text of a string that changes (pDC->DrawText(str)). The user does something that causes the value of str to change and you want to reflect that change in the window. Any time after you change str, call InvalidateRect() to mark the area as invalid. When the resulting WM_PAINT is received by the window, CView::OnPaint() will call OnDraw(). In your OnDraw() override, you call pDC->DrawText(str), drawing the new text on the window. Also remember, WM_PAINT messages are LOW priority (they can get bumped to the back of the message queue). If you need an instant update, like if the text string is changing as the result of a timer (like in a clock implementation), then you can add a call to UpdateWindow() right after the call to InvalidateRect(). This will cause a WM_PAINT to be sent immediately. Remember that, when responding to WM_PAINT, it is your responsibility to validate the region of the window that is invalid to prevent repeated WM_PAINT messages. Most of the time this is done for us by MFC or the default window message handling. So if you invalidate regions of a window while it's trying to validate it, the WM_PAINT messages keep coming. If you want to see the recursive action/result of doing this in your OnDraw(), try this in your OnDraw(). This consumes all the CPU time of one of my processors and renders dialog controls useless: Invalidate(); UpdateWindow(); return; I'm actually surprised it doesn't crash :) Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  G Offline
                  G Offline
                  gurucplusplus
                  wrote on last edited by
                  #8

                  Thanks.

                  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