Search code examples
c#formssizetitlebarcaption

Adapt the size of a form to its title text in C#


Is there a way to adapt the size of a form to the size of its title / caption text?

For example, official C# Message Box forms will adjust to the size of its title text (Note the lorem ipsum):

Normal Message Box

Other forms will not adjust their size to that of their title text:

Other form

Instead, an ellipsis is added at the end to fit the size mentioned in the "Size" property of the designer.

Is there a way to make the form adjust to the size of the title instead of the size we mention? If not, is there a way to get the full length of the text so that we can assign it to the form?

I tried setting the width of the form using

int topTextWidth =  TextRenderer.MeasureText(this.Text, this.Font).Width;
this.Width = topTextWidth;

But this.Font apparently refers to another font size.


Solution

  • For those who wants a full answer, here it is.

    The actual line that resizes the form according to the caption text is the following:

    this.Width = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width + AllButtonsAndPadding;
    

    AllButtonsAndPadding contains the width of all the buttons (minimize, maximize and close), the window borders and the icon. Getting these information takes a bit of coding. Here's a full example of a form that resizes itself, no matter what buttons or icon you put.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;
    
    namespace WindowAutoAdapt
    {
    public partial class Form1 : Form
    {
        //A default value in case Application.RenderWithVisualStyles == false
        private int AllButtonsAndPadding = 0;
        private VisualStyleRenderer renderer = null;
    
        public Form1()
        {
            InitializeComponent();
            this.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; //A big text in the title
            GetElementsSize();
            ResizeForm();
        }
    
        //This gets the size of the X and the border of the form
        private void GetElementsSize()
        {
            var g = this.CreateGraphics();
    
            // Get the size of the close button.
            if (SetRenderer(VisualStyleElement.Window.CloseButton.Normal))
            {
                AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width;
            }
    
            // Get the size of the minimize button.
            if (this.MinimizeBox && SetRenderer(VisualStyleElement.Window.MinButton.Normal))
            {
                AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width;
            }
    
            // Get the size of the maximize button.
            if (this.MaximizeBox && SetRenderer(VisualStyleElement.Window.MaxButton.Normal))
            {
                AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width;
            }
    
            // Get the size of the icon.
            if (this.ShowIcon)
            {
                AllButtonsAndPadding += this.Icon.Width;
            }
    
            // Get the thickness of the left, bottom, 
            // and right window frame.
            if (SetRenderer(VisualStyleElement.Window.FrameLeft.Active))
            {
                AllButtonsAndPadding += (renderer.GetPartSize(g, ThemeSizeType.True).Width) * 2; //Borders on both side
            }
        }
    
        //This resizes the form
        private void ResizeForm()
        {
            this.Width = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width + AllButtonsAndPadding;
        }
    
        //This sets the renderer to the element we want
        private bool SetRenderer(VisualStyleElement element)
        {
            bool bReturn = VisualStyleRenderer.IsElementDefined(element);
    
            if (bReturn && renderer == null)
                renderer = new VisualStyleRenderer(element);
            else
                renderer.SetParameters(element);
    
            return bReturn;
        }
     }
     }