Click here to Skip to main content
11,479,486 members (47,333 online)
Rate this: bad
Please Sign up or sign in to vote.
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 !!!
Posted 9-Aug-12 9:06am
Edited 9-Aug-12 9:15am
Christian Amado at 9-Aug-12 14:10pm
Windows Forms, WPF, Silverlight?
Ank_ush at 9-Aug-12 14:13pm
Its Windows Application. Windows Forms.
Wes Aday at 9-Aug-12 14:12pm
How is "But still not perfect" a description of your problem?
Christian Amado at 9-Aug-12 14:13pm
Good point, Wes.
Ank_ush at 9-Aug-12 14:15pm
Title Bar text is not coming perfectly in center of title bar.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

What about this one: WinForm Extended[^]

Sergey Alexandrovich Kryukov at 9-Aug-12 14:29pm
Good links, my 5.
In my answer, I just explain the principle, and, importantly, motivation -- please see.
Christian Amado at 9-Aug-12 14:31pm
Hey! Nice Solution below! +5 too =)
Sergey Alexandrovich Kryukov at 9-Aug-12 14:45pm
Thank you, Cristian.
Christian Amado at 9-Aug-12 14:46pm
You're welcome =)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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 at 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.
Sergey Alexandrovich Kryukov at 9-Aug-12 14:44pm
Thank you, Cristian.
Christian Amado at 9-Aug-12 14:45pm
You're welcome.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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)

  Print Answers RSS
0 OriginalGriff 285
1 Sergey Alexandrovich Kryukov 215
2 F-ES Sitecore 175
3 Richard MacCutchan 125
4 Frankie-C 104
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,441
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100