I'm wanting to dynamically resize a CButton to the width of the text within it. Is there either a built-in way to do this in MFC, or a way of calculating the pixel width of some specified text (so that I can use CWnd::SetWindowPos
)?
I'm wanting to dynamically resize a CButton to the width of the text within it. Is there either a built-in way to do this in MFC, or a way of calculating the pixel width of some specified text (so that I can use CWnd::SetWindowPos
)?
It's tedious. You need to use CWnd::GetFont()
on the button to get the font it's using, and then use the standard GetTextText
on a CDC
object where you will have selected that font.
It looks something like
CClientDC dc( &button );
CFont * pOldFont = dc.SelectObject( button.GetFont() );
... dc.GetTextExtent...
dc.SelectObject( pOldFont);