Is there any way to determine if the text of the form will fit?

Is there a way to determine if the text property of the form fits into the top panel using the width of the current form (or if it will be truncated with "...")?

enter image description here

+5
source share
1 answer

You can take a look at TextRenderer.MeasureText() .

To calculate the width of the subtitle text, use this snippet:

 var width = TextRenderer.MeasureText(caption, SystemFonts.CaptionFont).Width; 

You can use the size of your Form, subtract the fixed value for the icon (if available) and the buttons in the upper right corner (depending on the OS version and the visible state of the [Minimize] [Maximize] buttons) and check if all this is positive. This may not give you a completely accurate result, but it is probably the simplest approximation.

So far, this method has apparently calculated a fairly accurate approximation:

 /// <summary> /// Calculates an approximation of the available caption width /// Depends on OS and theme /// </summary> /// <returns>Width</returns> private int CalcAvaliableCaptionWidth() { return // Form width Width // Icon - (Icon == null ? 0 : Icon.Width) // Minimize button (26 on Win8) - (MinimizeBox ? SystemInformation.CaptionButtonSize.Width : 0) // Maximize button (26 on Win8) - (MaximizeBox ? SystemInformation.CaptionButtonSize.Width : 0) // Close button (45 on Win8) - SystemInformation.CaptionButtonSize.Width; } 

You can try my little WinForm app to check

verification WinForm application

Source code: https://gist.github.com/CodeZombieCH/b9def0b0d9c41a98593a

Thanks @Plutonix for the hint SystemInformation.CaptionButtonSize .

+6
source

All Articles