Title bar text in the center

Asked

Viewed 905 times

-1

The Form title bar text in my Visual Studio 2015 is on the left by default. How do I make it centralized by default? Grateful.

  • This might help: https://www.codeproject.com/Articles/93959/WinForm-Extended

  • or maybe this https://www.codeproject.com/Questions/260240/Windows-Form-Title-Bar-Text-alignment

  • Here’s what you need: https://stackoverflow.com/questions/11947314/how-to-center-align-the-title-bar-text-in-windows-form

1 answer

0

Of course, it is not possible.

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

  1. Create a form personalized.

    This is somewhat laborious, but it brings a lot of freedom. On the other hand, with freedom comes the responsibility to do everything right. It’s not a lot to deal with, so I think it’s a viable option if you need more things that don’t naturally exist in Forms.

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

    That’s not the focus of this question, so if you’re interested, I think it’s interesting that you open up another question on this particular issue. I’m sure there’ll be some great answers.

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

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

    Note that you will have to calculate the space of the normal buttons of a window (minimize, restore and close) and that this still may vary depending on the operating system, since these Forms will be different in each version of Windows. In addition, the name of form 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();
    }
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.