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. Drawing text issue

Drawing text issue

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpc++
5 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.
  • C Offline
    C Offline
    csrss
    wrote on last edited by
    #1

    Hi all. I got a huge problem while trying to draw a text with MFC. So, i got my custom class which extends CStatic. In OnPaint:

    CPaintDC dc(this);
    Gdiplus::Graphics * gfx = Gdiplus::Graphics::FromHDC(dc.GetSafeHdc());

    // then there a couple of things ... and then i am drawing a text like that (in another function which takes as IN parameter Gdiplus::Graphics, created in OnPaint):

    CDC * pDC = CDC::FromHandle(gfx->GetHDC());
    pDC->TextOut(0, 0, "my text");
    gfx->ReleaseHDC(pDC->GetSafeHdc());

    Simple. But every time OnPaint gets called - new text is drawn over old one, so it is getting bolder and bolder. And, it is not like the old one never erased, but it is erased only when there are about 5 same texts drawn over each one. Another thing, i've tried not to use Gdiplus at all - do everything only with CDC thing - same effect. Have used DrawText - same effect. Only with Gdiplus DrawString text is drawn correctly. But i need to draw it with Gdi32. Thanks.

    011011010110000101100011011010000110100101101110 0110010101110011

    K J 2 Replies Last reply
    0
    • C csrss

      Hi all. I got a huge problem while trying to draw a text with MFC. So, i got my custom class which extends CStatic. In OnPaint:

      CPaintDC dc(this);
      Gdiplus::Graphics * gfx = Gdiplus::Graphics::FromHDC(dc.GetSafeHdc());

      // then there a couple of things ... and then i am drawing a text like that (in another function which takes as IN parameter Gdiplus::Graphics, created in OnPaint):

      CDC * pDC = CDC::FromHandle(gfx->GetHDC());
      pDC->TextOut(0, 0, "my text");
      gfx->ReleaseHDC(pDC->GetSafeHdc());

      Simple. But every time OnPaint gets called - new text is drawn over old one, so it is getting bolder and bolder. And, it is not like the old one never erased, but it is erased only when there are about 5 same texts drawn over each one. Another thing, i've tried not to use Gdiplus at all - do everything only with CDC thing - same effect. Have used DrawText - same effect. Only with Gdiplus DrawString text is drawn correctly. But i need to draw it with Gdi32. Thanks.

      011011010110000101100011011010000110100101101110 0110010101110011

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      You can try using Graphics object gfx only for drawing. No need to mix GDI and GDI+. Instead of TextOut(), use Graphics::DrawString()

      C 1 Reply Last reply
      0
      • C csrss

        Hi all. I got a huge problem while trying to draw a text with MFC. So, i got my custom class which extends CStatic. In OnPaint:

        CPaintDC dc(this);
        Gdiplus::Graphics * gfx = Gdiplus::Graphics::FromHDC(dc.GetSafeHdc());

        // then there a couple of things ... and then i am drawing a text like that (in another function which takes as IN parameter Gdiplus::Graphics, created in OnPaint):

        CDC * pDC = CDC::FromHandle(gfx->GetHDC());
        pDC->TextOut(0, 0, "my text");
        gfx->ReleaseHDC(pDC->GetSafeHdc());

        Simple. But every time OnPaint gets called - new text is drawn over old one, so it is getting bolder and bolder. And, it is not like the old one never erased, but it is erased only when there are about 5 same texts drawn over each one. Another thing, i've tried not to use Gdiplus at all - do everything only with CDC thing - same effect. Have used DrawText - same effect. Only with Gdiplus DrawString text is drawn correctly. But i need to draw it with Gdi32. Thanks.

        011011010110000101100011011010000110100101101110 0110010101110011

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

        Use TextOut() or DrawText() function of CDC. Clear the earlier text with null string and then use drawing function.

        C 1 Reply Last reply
        0
        • K KingsGambit

          You can try using Graphics object gfx only for drawing. No need to mix GDI and GDI+. Instead of TextOut(), use Graphics::DrawString()

          C Offline
          C Offline
          csrss
          wrote on last edited by
          #4

          The problem is i cannot use Gdiplus for drawing a string because my requirement is to draw the string with exact same font which windows OS uses. Gdiplus produce different font even if you do:

          HFONT hFOnt = ::GetStockObject(DEFAULT_GUI_FONT);

          and then create Gdiplus::Font from hFont. In theory you should have default font and maybe you do have, but DrawString draws text with a different font, not system default.

          011011010110000101100011011010000110100101101110 0110010101110011

          1 Reply Last reply
          0
          • J john5632

            Use TextOut() or DrawText() function of CDC. Clear the earlier text with null string and then use drawing function.

            C Offline
            C Offline
            csrss
            wrote on last edited by
            #5

            Ahh, it was all about this MFC thing - sometimes you have to override some methods you got no idea about to make your control draw itself correctly. In this case it was:

            OnWindowPosChanged

            And just do nothing inside - static draws perfectly.

            011011010110000101100011011010000110100101101110 0110010101110011

            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