Title bar text in the center

0

The title bar text of the Form in my Visual Studio 2015 is on the left by default. How do I get it centered by default? Thankful.

    
asked by anonymous 11.05.2017 / 14:36

1 answer

0

Of course, it is not possible.

There are two ways to deal with this, neither is easy and perhaps none is good for the specific case.

  • Create a custom form .

    This is somewhat laborious, but it brings a lot of freedom. On the other hand, with freedom comes the responsibility to do everything right. There are not many things to deal with so I think it's a cogitable option if you need more things that do not exist naturally in the forms .

    This option is fine if you need to modify more things, otherwise I think the best idea is to settle for the title on the left.

    This is not the focus of this question, so if you are interested, I find it interesting to open another question about this specific question. I'm sure there will be great answers.

  • Create a method that edits the title by the size of form added spaces left and right to center it. This is the famous "gambiarra mode".

    Basically, what needs to be done is to call the method that does this calculation when loading the form, using the event Load " and whenever you change the size of it, using the ResizeEnd .

    Note that you will have to compute the normal button space of a window (minimize, restore, and close) and that may vary depending on the operating system, since these forms will be different in each version of Windows. In addition, the form name will not appear on the taskbar because it will be too long.

    The basis of this code is this.

    private void UpdateTextPosition()
    {
        var grap = CreateGraphics();
    
        var pontoInicio = (Width / 2) - (grap.MeasureString(Text.Trim(), Font).Width / 2);
        var larguraEspaco = grap.MeasureString(" ", Font).Width;
    
        var tituloTemp = " ";
        double larguraTemp = 0;
    
        while ((larguraTemp + larguraEspaco) < pontoInicio)
        {
            tituloTemp += " ";
            larguraTemp += larguraEspaco;
        }
    
        Text = tituloTemp + Text.Trim();
    }
    
  • 11.05.2017 / 15:00