I want to adjust a Static control's size to its content size, so I need to calculate the size of its text content first. I found a way to use GetTextExtentPoint32
to calculate the size, but I need to set the DC's font to the same as the control's font first. Is there a better way to do this? I've set the Static control's font once, I think maybe I don't need to set the DC's font the second time.
What is the best way to calculate the size of a Static control's text content? And is there a better way to autosize the Static control?
It sounds to me like you've already figured out the correct way to do it. Call GetTextExtentPoint32
to figure out the ideal size of the control given the text that it contains, and then resizing the control to the calculated size.
It's a lot of work, but that's what happens when you're working with the raw Win32 API. You don't have a handy wrapper library that abstracts all this for you in a Control.AutoSize()
function. You could easily write your own function and re-use it, but the Win32 standard controls do not expose an "auto-size" API.
As far as the font, you will definitely need to make sure that the device context is using the same font as the control, otherwise you'll calculate the wrong size. But you don't have to create a new device context, request a handle the static control's font, and select that into your new DC. Instead, you can just use the static control's DC using the GetDC
function and passing in the handle to your static control window. Make sure that if you call GetDC
, you always follow up with a call to ReleaseDC
when you're finished!
However, do note some caveats of the GetTextExtentPoint32
function that may interfere with the accuracy of the size you calculate:
- It ignores clipping.
- It does not take into account new lines (
\n
) or carriage returns (\r\n
) when computing the height.
- It does not take into account prefix characters (those preceded in the string with ampersand) and used to denote keyboard mnemonics if your static control does not have the
SS_NOPREFIX
style.
- It may not return an accurate result in light of the kerning that may be implemented automatically by some devices.
(This is all mentioned in the linked documentation, but does anyone actually read that?)
Perhaps an easier alternative is to draw the text the same way that the static control is already doing. Unless you have the SS_SIMPLE
style set (which uses TextOut
or ExtTextOut
to draw text as an optimization), static controls draw their text by calling the DrawText
function with the appropriate parameters, given the other control styles that are set (reference).
You can do exactly the same thing, and add the DT_CALCRECT
flag in your call to the DrawText
function, which causes it to determine the width and height of the rectangle required to draw the specified text without actually drawing the text.
There is no autosize for static control as far as I know. You are doing it correct.
- Use GetWinDowText to get the text of static window
- Use GetDC to get the dc for the window
- Use WM_GETFONT to get the font for the window and select the font into the dc
- Use one of the text size calculation function to calculate the text size
- Restore the the original dc font
- Release dc
You will always have to select the proper font into the dc to get accurate result. Also I personally prefer DrawText with DT_CALCRECT to calculate the size of a text. Refer http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498%28v=vs.85%29.aspx
With DrawText, you dont have to supply the character count if the text is NULL terminated. Plus you can combine various formatting option to adjust the calculation. For example, an Ampersand(&) character in a static control text underlines the next character. With Drawtext you will be able to calculate the size properly but in GetTextExtentPoint32 there is no provision to specify this.
Most windows using static text controls are dialogs, where the static control's size is expressed in dialog units (DLU), which account roughly for the size of the font. In this way, dialog controls tend to have sensible sizes.
If you are not using dialogs, you can attempt to fake dialog behavior using MapDialogRect
.
Otherwise yes you must use GetTextExtentPoint32
.