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