Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Center Align the Title Bar text In Windows Form?

Tags:

c#

winforms

I am developing a Windows Form Application. I want to Align the Text to Center or say to Right of Title Bar of Form. How can I do it ??

like image 461
Ankush Avatar asked Aug 14 '12 07:08

Ankush


1 Answers

It can be done with custom form - you will have to create your own title bar. See V4Vendettas comment;

Other approach (link) - is to create your own handler for form resize and insert there followong code. It will add appropriate amount of spaces from the left size of text. However you will have to add form.Refresh() and call that method in form.Load; also your window will have "..." as a text in task bar.

private void UpdateTextPosition()
{
    Graphics g = this.CreateGraphics();
    Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
    Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
    String tmp = " ";
    Double tmpWidth = 0;

    while ((tmpWidth + widthOfASpace) < startingPoint)
    {
       tmp += " ";
       tmpWidth += widthOfASpace;
    }

    this.Text = tmp + this.Text.Trim();
}
like image 122
JleruOHeP Avatar answered Sep 25 '22 01:09

JleruOHeP