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. How to fix Static contol width at run time?

How to fix Static contol width at run time?

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

    Hi all, on dialog box i have a static contol i want to fit its width according to its text when dialog box is open or text change . i use this but the control width is set less than text width..

    // get the text dimensions
    CString strText;
    m_label.GetWindowText(strText);
    CClientDC dc(this);
    CFont* pFont = GetFont();
    CFont* pOldFont = (CFont*)dc.SelectObject(pFont);
    CSize sizeText = dc.GetTextExtent(strText);
    dc.SelectObject(pOldFont);

    // get the text area (presume left-alligned text).
    CRect rcText;
    m\_label.GetWindowRect(rcText);
    
    CRect dlg\_stc=rcText;
    this->ScreenToClient(dlg\_stc);
    
    rcText = dlg\_stc;
    
    rcText.right = min(rcText.right, sizeText.cx);
    
    
    CRect new\_rect=rcText;
    m\_label.MoveWindow(new\_rect);
    

    can anybody help me to do this. thanks.

    L P 2 Replies Last reply
    0
    • L Le rner

      Hi all, on dialog box i have a static contol i want to fit its width according to its text when dialog box is open or text change . i use this but the control width is set less than text width..

      // get the text dimensions
      CString strText;
      m_label.GetWindowText(strText);
      CClientDC dc(this);
      CFont* pFont = GetFont();
      CFont* pOldFont = (CFont*)dc.SelectObject(pFont);
      CSize sizeText = dc.GetTextExtent(strText);
      dc.SelectObject(pOldFont);

      // get the text area (presume left-alligned text).
      CRect rcText;
      m\_label.GetWindowRect(rcText);
      
      CRect dlg\_stc=rcText;
      this->ScreenToClient(dlg\_stc);
      
      rcText = dlg\_stc;
      
      rcText.right = min(rcText.right, sizeText.cx);
      
      
      CRect new\_rect=rcText;
      m\_label.MoveWindow(new\_rect);
      

      can anybody help me to do this. thanks.

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

      I have had a similar issue in the past and used the GetTextExtentPoint32() function. I also found it necessary to add the size of a couple of extra characters as the calculation seems very precise.

      Use the best guess

      L 1 Reply Last reply
      0
      • L Lost User

        I have had a similar issue in the past and used the GetTextExtentPoint32() function. I also found it necessary to add the size of a couple of extra characters as the calculation seems very precise.

        Use the best guess

        L Offline
        L Offline
        Le rner
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        I have had a similar issue in the past and used the GetTextExtentPoint32() function. I also found it necessary to add the size of a couple of extra characters as the calculation seems very precise.

        this also gives the same width.

        1 Reply Last reply
        0
        • L Le rner

          Hi all, on dialog box i have a static contol i want to fit its width according to its text when dialog box is open or text change . i use this but the control width is set less than text width..

          // get the text dimensions
          CString strText;
          m_label.GetWindowText(strText);
          CClientDC dc(this);
          CFont* pFont = GetFont();
          CFont* pOldFont = (CFont*)dc.SelectObject(pFont);
          CSize sizeText = dc.GetTextExtent(strText);
          dc.SelectObject(pOldFont);

          // get the text area (presume left-alligned text).
          CRect rcText;
          m\_label.GetWindowRect(rcText);
          
          CRect dlg\_stc=rcText;
          this->ScreenToClient(dlg\_stc);
          
          rcText = dlg\_stc;
          
          rcText.right = min(rcText.right, sizeText.cx);
          
          
          CRect new\_rect=rcText;
          m\_label.MoveWindow(new\_rect);
          

          can anybody help me to do this. thanks.

          P Offline
          P Offline
          Parthi_Appu
          wrote on last edited by
          #4

          Le@rner wrote:

          rcText.right = min(rcText.right, sizeText.cx);

          try this for the above statement,

          rcText.right = rcText.left + sizeText.cx

          and try to avoid using unwanted temporary variables, you can simply have as below,

          CRect rcLabel;
          m_label.GetWindowRect(rcLabel); //get screen co-ordinate for static ctrl
          rcLabel.right = rcLabel.left + sizeText.cx; //increase/decrease the rect width
          ScreenToClient(rcLabel); // convert screen co-ordinate to client co-ordinate
          m_label.MoveWindow(rcLabel); //move the static ctrl to new position


          Do your Duty and Don't Worry about the Result

          L 1 Reply Last reply
          0
          • P Parthi_Appu

            Le@rner wrote:

            rcText.right = min(rcText.right, sizeText.cx);

            try this for the above statement,

            rcText.right = rcText.left + sizeText.cx

            and try to avoid using unwanted temporary variables, you can simply have as below,

            CRect rcLabel;
            m_label.GetWindowRect(rcLabel); //get screen co-ordinate for static ctrl
            rcLabel.right = rcLabel.left + sizeText.cx; //increase/decrease the rect width
            ScreenToClient(rcLabel); // convert screen co-ordinate to client co-ordinate
            m_label.MoveWindow(rcLabel); //move the static ctrl to new position


            Do your Duty and Don't Worry about the Result

            L Offline
            L Offline
            Le rner
            wrote on last edited by
            #5

            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