What is the correct way to automatically configure a Static control?

I want to adjust the size of the static control to its content size, so I first need to calculate the size of its text content. I found a way to use GetTextExtentPoint32 to calculate the size, but I need to set the DC font in the same way as the control font. Is there a better way to do this? I installed the Static control font once, I think maybe I don’t need to install the DC font the second time.

What is the best way to calculate the size of text content Static? And is there a better way to automate static control?

+6
source share
3 answers

It seems to me that you already understood the correct way to do this. Call GetTextExtentPoint32 to determine the ideal size for the control, given the text it contains, and then resize the control to a computed size.

This is a lot of work, but what happens when you work with the raw Win32 API. You do not have a convenient wrapper library that abstracts all this for you in the Control.AutoSize() function. You can easily write your own function and reuse it, but standard Win32 controls do not provide auto-size APIs.

As for the font, you definitely need to make sure that the device context uses the same font as the control, otherwise you will calculate the wrong size. But you do not need to create a new device context, request a static control font descriptor and select it in your new DC. Instead, you can use constant DC control using the GetDC function and pass it to the static control window. Make sure that if you call GetDC , you will always answer the ReleaseDC call when you are done!

However, pay attention to some caveats of the GetTextExtentPoint32 function that may interfere with the accuracy of the size you are calculating:

  • He ignores clipping.
  • When calculating the height, new lines ( \n ) or carriage returns ( \r\n ) are not taken into account.
  • It does not take into account the prefix characters (preceding the line with the ampersand) and is used to indicate keyboard mnemonics if your static control has SS_NOPREFIX style .
  • It cannot return an exact result in the light of kerning, which can be automatically implemented by some devices.

(All of this is mentioned in the related documentation, but does anyone really read this?)

Perhaps a simpler alternative is to draw text in the same way as static control. If you don't have the SS_SIMPLE style style (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, taking into account the other control styles that are set ( link ).

You can do the same and add the DT_CALCRECT flag to your DrawText function DrawText , which forces it to determine the width and height of the rectangle needed to draw the specified text, without actually drawing the text.

+3
source

Most windows using static text controls are dialogs in which the size of the static control is expressed in dialog units (DLUs), the size of which is approximately the same as the font size. Thus, dialog controls are reasonably sized.

If you are not using dialogs, you can try to fake dialog behavior using MapDialogRect .

Otherwise, yes, you should use GetTextExtentPoint32 .

0
source

As I know, there is no autosave for static monitoring. You are doing it right.

  • Use GetWinDowText to get the text of a static window.
  • Use GetDC to get dc for window
  • Use WM_GETFONT to get the font for the window and select the font in dc
  • Use one of the text size calculation functions to calculate the text size.
  • Restore original dc font
  • Release dc

You always need to choose the right font in DC to get the exact result. I also personally prefer DrawText with DT_CALCRECT for calculating text size. See http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498%28v=vs.85%29.aspx

With DrawText, you do not need to specify the number of characters if the NULL text is complete. In addition, you can combine various formatting options to customize the calculation. For example, the Ampersand (&) character in a static control text underlines the following character. With Drawtext, you can calculate the size correctly, but there is no provision in GetTextExtentPoint32 to indicate this.

0
source

Source: https://habr.com/ru/post/921066/


All Articles