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. static text box , GetClientRect, GetTextExtent [modified]

static text box , GetClientRect, GetTextExtent [modified]

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

    CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007

    S M H 3 Replies Last reply
    0
    • D daveyerwin

      CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007

      S Offline
      S Offline
      shivditya
      wrote on last edited by
      #2

      Sorry but I was unable to locate GetWindowTextW function ,and size is variable of which class ?

      || ART OF LIVING ||

      1 Reply Last reply
      0
      • D daveyerwin

        CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007

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

        CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); If the static control is from a dialog, it may use a different font. staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it before calculating the text extents. Mark

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

        D 1 Reply Last reply
        0
        • D daveyerwin

          CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007

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

          Whats problem ?


          WhiteSky


          1 Reply Last reply
          0
          • M Mark Salsbery

            CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); If the static control is from a dialog, it may use a different font. staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it before calculating the text extents. Mark

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

            D Offline
            D Offline
            daveyerwin
            wrote on last edited by
            #5

            >> staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it Greetings Mark I now have this ... void CmyAppDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //OnOK(); CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CFont * font = GetDC()->GetCurrentFont(); staticTextBox.SetFont(font,1); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } Now when the text fills the width of the static control textSize.cx equals stbRect.Width(). This is a good thing . I am setting the font of the control to the same font used by GetTextExtent . How may I instead , set the font used by GetTextExtent to the same font used by the static control ? Thanks for reading this .

            M 1 Reply Last reply
            0
            • D daveyerwin

              >> staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it Greetings Mark I now have this ... void CmyAppDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //OnOK(); CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CFont * font = GetDC()->GetCurrentFont(); staticTextBox.SetFont(font,1); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } Now when the text fills the width of the static control textSize.cx equals stbRect.Width(). This is a good thing . I am setting the font of the control to the same font used by GetTextExtent . How may I instead , set the font used by GetTextExtent to the same font used by the static control ? Thanks for reading this .

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

              Here's an example - using the dialog's font... void CmyAppDlg::OnBnClickedOk() { CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); // Get a DC for the static control //*edit* this could be "CClientDC ClientDC(this);" //*edit* (any window/screen-compatible DC will do) CClientDC StaticDC(&staticTextBox); // Select the dialog's font into it CFont *pOldFont = StaticDC.SelectObject(GetFont()); // Get the text extent based on the dialog font CSize textSize = StaticDC.GetTextExtent(cs2); // restore the DC StaticDC.SelectObject(pOldFont); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder -- modified at 14:29 Wednesday 16th May, 2007

              D 1 Reply Last reply
              0
              • M Mark Salsbery

                Here's an example - using the dialog's font... void CmyAppDlg::OnBnClickedOk() { CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); // Get a DC for the static control //*edit* this could be "CClientDC ClientDC(this);" //*edit* (any window/screen-compatible DC will do) CClientDC StaticDC(&staticTextBox); // Select the dialog's font into it CFont *pOldFont = StaticDC.SelectObject(GetFont()); // Get the text extent based on the dialog font CSize textSize = StaticDC.GetTextExtent(cs2); // restore the DC StaticDC.SelectObject(pOldFont); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder -- modified at 14:29 Wednesday 16th May, 2007

                D Offline
                D Offline
                daveyerwin
                wrote on last edited by
                #7

                Thank you Mark ! You have made many things very clear . I have this now :) CRect stbRect; CString stbStr; staticTextBox.GetClientRect (stbRect); staticTextBox.GetWindowTextW(stbStr); CClientDC stbCDC(&staticTextBox);; CFont *pOldFont = stbCDC.SelectObject(GetFont()); CSize textSize = stbCDC.GetTextExtent(stbStr); stbCDC.SelectObject(pOldFont); int stbLines = stbRect.bottom / textSize.cy ; stbStr = _T("this is on the top line"); for(int i=1 ; i

                M 1 Reply Last reply
                0
                • D daveyerwin

                  Thank you Mark ! You have made many things very clear . I have this now :) CRect stbRect; CString stbStr; staticTextBox.GetClientRect (stbRect); staticTextBox.GetWindowTextW(stbStr); CClientDC stbCDC(&staticTextBox);; CFont *pOldFont = stbCDC.SelectObject(GetFont()); CSize textSize = stbCDC.GetTextExtent(stbStr); stbCDC.SelectObject(pOldFont); int stbLines = stbRect.bottom / textSize.cy ; stbStr = _T("this is on the top line"); for(int i=1 ; i

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

                  Cool does it work like you need it to? I was never sure what you were up to :) Mark

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

                  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