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 resize CEdit dynamically?

How to resize CEdit dynamically?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondesignhelptutorial
5 Posts 5 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.
  • S Offline
    S Offline
    Soleil couchant
    wrote on last edited by
    #1

    Hi there, I have a CEdit Control which I have put on a form at design-time. The CEdit control does not have any text initially. Now on run-time, I am inserting text to the CEdit control and I want the CEdit Control size to fit the text that I entered. It has to fit EXACTLY the text (resizing the CEdit to have a width which will just be enough to show the text). How do I do this dynamically? Any help would be highly appreciated Best Regards, Soleil Couchant

    R D B M 4 Replies Last reply
    0
    • S Soleil couchant

      Hi there, I have a CEdit Control which I have put on a form at design-time. The CEdit control does not have any text initially. Now on run-time, I am inserting text to the CEdit control and I want the CEdit Control size to fit the text that I entered. It has to fit EXACTLY the text (resizing the CEdit to have a width which will just be enough to show the text). How do I do this dynamically? Any help would be highly appreciated Best Regards, Soleil Couchant

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      As far as I know, you have to count the pixels used by your text through the font size, and then resize the CEdit text accordingly.

      Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

      1 Reply Last reply
      0
      • S Soleil couchant

        Hi there, I have a CEdit Control which I have put on a form at design-time. The CEdit control does not have any text initially. Now on run-time, I am inserting text to the CEdit control and I want the CEdit Control size to fit the text that I entered. It has to fit EXACTLY the text (resizing the CEdit to have a width which will just be enough to show the text). How do I do this dynamically? Any help would be highly appreciated Best Regards, Soleil Couchant

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        For sizing a control, see SetWindowPos() or MoveWindow().


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        1 Reply Last reply
        0
        • S Soleil couchant

          Hi there, I have a CEdit Control which I have put on a form at design-time. The CEdit control does not have any text initially. Now on run-time, I am inserting text to the CEdit control and I want the CEdit Control size to fit the text that I entered. It has to fit EXACTLY the text (resizing the CEdit to have a width which will just be enough to show the text). How do I do this dynamically? Any help would be highly appreciated Best Regards, Soleil Couchant

          B Offline
          B Offline
          bob16972
          wrote on last edited by
          #4

          The focus of this article is italic fonts, but the author attempts to resize the controls to the text size so it may be up the same alley. here[^]

          1 Reply Last reply
          0
          • S Soleil couchant

            Hi there, I have a CEdit Control which I have put on a form at design-time. The CEdit control does not have any text initially. Now on run-time, I am inserting text to the CEdit control and I want the CEdit Control size to fit the text that I entered. It has to fit EXACTLY the text (resizing the CEdit to have a width which will just be enough to show the text). How do I do this dynamically? Any help would be highly appreciated Best Regards, Soleil Couchant

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

            Here's some code you can play with - it's the closest I could get on a single-line edit control. Multiline edit controls may work better since they allow you to change the formatting rect. Hopefully this will give you a general idea of a way to do it :)

            CString str = _T("Test Text");

            // Get text extent
            CFont *pFont = m_Edit1Edit.GetFont();
            CWindowDC dc(this);
            CFont *pOldFont = dc.SelectObject(pFont);
            CSize TextSize = dc.GetTextExtent(str);
            dc.SelectObject(pOldFont);

            // Calculate the new size and resize the control
            CRect WindowRect;
            CRect ClientRect;
            CRect FormatRect;
            m_Edit1Edit.GetWindowRect(&WindowRect);
            m_Edit1Edit.GetClientRect(&ClientRect);
            m_Edit1Edit.GetRect(&FormatRect);
            CSize BorderSize;
            BorderSize.cx = WindowRect.Width() - FormatRect.Width();
            BorderSize.cy = WindowRect.Height() - ClientRect.Height();
            m_Edit1Edit.SetWindowPos(0, 0, 0, TextSize.cx + BorderSize.cx, TextSize.cy + BorderSize.cy, SWP_NOZORDER | SWP_NOMOVE);

            // Set the control's text
            m_Edit1Edit.SetWindowText(str);

            "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