Click here to Skip to main content
14,869,058 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I want the text of the form to appear in the middle of title bar. It's Windows Application.
I tried this code:
Graphics g = this.CreateGraphics();
            double start = (this.Width / 2) -(g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
            double widthspace = g.MeasureString(this.Text.Trim(), this.Font).Width / 2;
            string tmp = "         ";
            double tmpwidth = 0;
                tmp += "     ";
                tmpwidth += widthspace;
            while ((tmpwidth + widthspace) < start);
            this.Text = tmp + this.Text.Trim() + tmp;

But still not perfect.
Any other solution, plz help me.

Thanks in Advance !!!
Updated 9-Aug-12 8:15am
Christian Amado 9-Aug-12 14:10pm
Windows Forms, WPF, Silverlight?
Ank_ush 9-Aug-12 14:13pm
Its Windows Application. Windows Forms.
[no name] 9-Aug-12 14:12pm
How is "But still not perfect" a description of your problem?
Christian Amado 9-Aug-12 14:13pm
Good point, Wes.
Ank_ush 9-Aug-12 14:15pm
Title Bar text is not coming perfectly in center of title bar.

May be helps you a little: Easy Customize Title Bar[^]

What about this one: WinForm Extended[^]

Good links, my 5.
In my answer, I just explain the principle, and, importantly, motivation -- please see.
Christian Amado 9-Aug-12 14:31pm
Hey! Nice Solution below! +5 too =)
Thank you, Cristian.
Christian Amado 9-Aug-12 14:46pm
You're welcome =)
If cannot be perfect, by definition. You should not do it, because this is controlled by OS. For example, when I run my .NET applications on the version of Ubuntu I use right now, it centers the form titles, but it depends on desktop manager. If I used something like you do, the system would try to center a padded text, and it would look shifted right. And you don't know how next versions of Windows will look, and what skinning will be available. So, never do such things.

Now, you still can fully customize the look of the title bar if you use directly opposite approach. But you cannot use a combined approach.

First, you can totally re-draw non-client area of your form, using raw (unmanaged) Windows API and non-client Windows messages. I would not recommend it either, as this is too much Windows specific. .NET tries to go out of Windows, and CLR is designed for platform compatibility. If you write your Forms applications accurately, you can run them on different OS without recompilation. With Windows-specific tricks, you will break this compatibility, and pretty likely, compatibility with future Microsoft systems.

Second approach is the most radical and the safest at the same time, it does not require any interop. You can show your form without any non-client areas at all. To do it, set the form property System.Windows.Forms.FormFormBorderStyle to System.Windows.Forms.FormBorderStyle.None:[^],[^].

When this is done, simply simulate all non-client controls (such as title bar) in the regular client area. Just create and add some custom controls simulating requires non-client form elements.

Now, a bonus advice: if some developers use such highly customized forms, they often make a step further to make the forms non-rectangular, but of some funky-looking shapes. This is easily done using the property System.Windows.Forms.Control.Region:[^].

Christian Amado 9-Aug-12 14:32pm
I totally agree with you. It's not recommended to change the normal behavior of the predefined things in the OS.
Thank you, Cristian.
Christian Amado 9-Aug-12 14:45pm
You're welcome.
I created this method and called in Form Load event.

private void Center_Text()
            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();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900