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::GetTextExtent() and CWnd::GetClientRect()

CDC::GetTextExtent() and CWnd::GetClientRect()

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • M Offline
    M Offline
    Martyn Pearson
    wrote on last edited by
    #1

    Hi Quick question - am I comparing like for like if I do the following in a CEdit derived class? I am trying to determine whether or not the text will be completely visible when placed in the edit control. This code indicates that the text will not be completely visible, even though it quite clearly is! void CMyEdit::DoSomething(const CString & strText) { CRect rectText; GetClientRect(rectText); CDC * pDC = GetDC(); CSize sizeExtent = pDC->GetTextExtent(strText); if (sizeExtent.cx > rectText.Width()) // Is this valid? { // Text too large } ReleaseDC(pDC); } Thanks in advance!

    A R 2 Replies Last reply
    0
    • M Martyn Pearson

      Hi Quick question - am I comparing like for like if I do the following in a CEdit derived class? I am trying to determine whether or not the text will be completely visible when placed in the edit control. This code indicates that the text will not be completely visible, even though it quite clearly is! void CMyEdit::DoSomething(const CString & strText) { CRect rectText; GetClientRect(rectText); CDC * pDC = GetDC(); CSize sizeExtent = pDC->GetTextExtent(strText); if (sizeExtent.cx > rectText.Width()) // Is this valid? { // Text too large } ReleaseDC(pDC); } Thanks in advance!

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      I just did a quick lookup on the GetTextExtent function and noticed that there's another one called GetOutputTextExtent, which may be the one you need. Regards, Alvaro


      All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain

      1 Reply Last reply
      0
      • M Martyn Pearson

        Hi Quick question - am I comparing like for like if I do the following in a CEdit derived class? I am trying to determine whether or not the text will be completely visible when placed in the edit control. This code indicates that the text will not be completely visible, even though it quite clearly is! void CMyEdit::DoSomething(const CString & strText) { CRect rectText; GetClientRect(rectText); CDC * pDC = GetDC(); CSize sizeExtent = pDC->GetTextExtent(strText); if (sizeExtent.cx > rectText.Width()) // Is this valid? { // Text too large } ReleaseDC(pDC); } Thanks in advance!

        R Offline
        R Offline
        Roger Allen
        wrote on last edited by
        #3

        When you call GetDC() you get a DC with a default font in it. This will (most probably) not be the font being used by the edit control. You need to use that font when measuring your text. Check the result of a call to CEdit::GetFont(). Roger Allen Sonork 100.10016 WHats brown and sticky? A stick or some smelly stuff!

        M 1 Reply Last reply
        0
        • R Roger Allen

          When you call GetDC() you get a DC with a default font in it. This will (most probably) not be the font being used by the edit control. You need to use that font when measuring your text. Check the result of a call to CEdit::GetFont(). Roger Allen Sonork 100.10016 WHats brown and sticky? A stick or some smelly stuff!

          M Offline
          M Offline
          Martyn Pearson
          wrote on last edited by
          #4

          Thanks for that - calling CEdit::GetFont() and passing the result to CDC::SelectObject() before doing the comparison worked a treat!

          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