How to resize CEdit dynamically?
-
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
-
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
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[^]
-
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
For sizing a control, see
SetWindowPos()
orMoveWindow()
.
"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
-
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
-
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
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