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. CDC::DrawText Not doing as it's told

CDC::DrawText Not doing as it's told

Scheduled Pinned Locked Moved C / C++ / MFC
help
4 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.
  • S Offline
    S Offline
    softwaremonkey
    wrote on last edited by
    #1

    Guys, I have a problem with CDC::DrawText() and I have been sitting here wasting away my Sunday trying to figure it out. The problem is that it does not draw the text in the position I expect it to be. The following code is supposed to draw a rectangle, then draw the text in the box, using the style (DT_LEFT, DT_CENTER or DT_RIGHT).

    CBrush brush(m\_background\[nIndex\]);
    CBrush\* pOldBrush = pDC->SelectObject(&brush);
    pDC->SetBkColor(m\_background\[nIndex\]);
    pDC->SetBkMode(OPAQUE);
    pDC->SelectStockObject(NULL\_PEN);
    pDC->Rectangle(m\_hitRect);
    pDC->SelectObject(pOldBrush);
    pDC->SetBkMode(TRANSPARENT);
    pDC->DrawText(txt,m\_hitRect, m\_nStyle | DT\_SINGLELINE | DT\_VCENTER);
    

    The text appears in the box (but not in the correct position) when I use DT_CENTER or DT_RIGHT but is not drwn at all with DT_LEFT (I think its outside the rect). Please help, I really should be cutting the grass! :-D Tony

    L M 2 Replies Last reply
    0
    • S softwaremonkey

      Guys, I have a problem with CDC::DrawText() and I have been sitting here wasting away my Sunday trying to figure it out. The problem is that it does not draw the text in the position I expect it to be. The following code is supposed to draw a rectangle, then draw the text in the box, using the style (DT_LEFT, DT_CENTER or DT_RIGHT).

      CBrush brush(m\_background\[nIndex\]);
      CBrush\* pOldBrush = pDC->SelectObject(&brush);
      pDC->SetBkColor(m\_background\[nIndex\]);
      pDC->SetBkMode(OPAQUE);
      pDC->SelectStockObject(NULL\_PEN);
      pDC->Rectangle(m\_hitRect);
      pDC->SelectObject(pOldBrush);
      pDC->SetBkMode(TRANSPARENT);
      pDC->DrawText(txt,m\_hitRect, m\_nStyle | DT\_SINGLELINE | DT\_VCENTER);
      

      The text appears in the box (but not in the correct position) when I use DT_CENTER or DT_RIGHT but is not drwn at all with DT_LEFT (I think its outside the rect). Please help, I really should be cutting the grass! :-D Tony

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Check the values in m_hitRect, I don't think we can guess what they are. [edit]It is also worth checking the return value from the DrawText() call to verify that it did what you expect.[/edit]

      The best things in life are not things.

      1 Reply Last reply
      0
      • S softwaremonkey

        Guys, I have a problem with CDC::DrawText() and I have been sitting here wasting away my Sunday trying to figure it out. The problem is that it does not draw the text in the position I expect it to be. The following code is supposed to draw a rectangle, then draw the text in the box, using the style (DT_LEFT, DT_CENTER or DT_RIGHT).

        CBrush brush(m\_background\[nIndex\]);
        CBrush\* pOldBrush = pDC->SelectObject(&brush);
        pDC->SetBkColor(m\_background\[nIndex\]);
        pDC->SetBkMode(OPAQUE);
        pDC->SelectStockObject(NULL\_PEN);
        pDC->Rectangle(m\_hitRect);
        pDC->SelectObject(pOldBrush);
        pDC->SetBkMode(TRANSPARENT);
        pDC->DrawText(txt,m\_hitRect, m\_nStyle | DT\_SINGLELINE | DT\_VCENTER);
        

        The text appears in the box (but not in the correct position) when I use DT_CENTER or DT_RIGHT but is not drwn at all with DT_LEFT (I think its outside the rect). Please help, I really should be cutting the grass! :-D Tony

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

        You don't show the values of variables so it's hard to guess. If you strip it down and hardcode values it works fine...

        CString txt = \_T("This is the text!");
        CRect m\_hitRect(10,10,200,40);
        //CBrush brush(RGB(0xFF,0x00,0x00));//(m\_background\[nIndex\]);
        //CBrush\* pOldBrush = dc.SelectObject(&brush);
        //dc.SetBkColor(RGB(0x80,0x80,0x80));
        //dc.SetBkMode(OPAQUE);
        //dc.SelectStockObject(NULL\_PEN);
        dc.Rectangle(m\_hitRect);
        //dc.SelectObject(pOldBrush);
        //dc.SetBkMode(TRANSPARENT);
        dc.DrawText(txt,m\_hitRect, DT\_LEFT | DT\_SINGLELINE | DT\_VCENTER);
        

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S 1 Reply Last reply
        0
        • M Mark Salsbery

          You don't show the values of variables so it's hard to guess. If you strip it down and hardcode values it works fine...

          CString txt = \_T("This is the text!");
          CRect m\_hitRect(10,10,200,40);
          //CBrush brush(RGB(0xFF,0x00,0x00));//(m\_background\[nIndex\]);
          //CBrush\* pOldBrush = dc.SelectObject(&brush);
          //dc.SetBkColor(RGB(0x80,0x80,0x80));
          //dc.SetBkMode(OPAQUE);
          //dc.SelectStockObject(NULL\_PEN);
          dc.Rectangle(m\_hitRect);
          //dc.SelectObject(pOldBrush);
          //dc.SetBkMode(TRANSPARENT);
          dc.DrawText(txt,m\_hitRect, DT\_LEFT | DT\_SINGLELINE | DT\_VCENTER);
          

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S Offline
          S Offline
          softwaremonkey
          wrote on last edited by
          #4

          Hi thanks for the reply. I figured out what it was, and as usual its something dumb and not in the area I was looking in. A long, long time ago I had padded out the right hand side of the string with spaces (not sure why I did this). Anyway, as you can imagine, this would totally screw up the justification. Oh well, that grass wont mow itself :laugh: 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