Click here to Skip to main content
12,065,756 members (27,536 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: .NET2.0 .NET3.5 C# .NET , +
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;
            do
            {
                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
Ank_ush2.9K
Edited 9-Aug-12 9:15am
v3
Comments
Christian Amado 9-Aug-12 14:10pm
   
Windows Forms, WPF, Silverlight?
Ank_ush 9-Aug-12 14:13pm
   
Its Windows Application. Windows Forms.
Wes Aday 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.
Rate this: bad
 
good
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[^]

Regards.
  Permalink  
v2
Comments
   
Good links, my 5.
In my answer, I just explain the principle, and, importantly, motivation -- please see.
--SA
Christian Amado 9-Aug-12 14:31pm
   
Hey! Nice Solution below! +5 too =)
   
Thank you, Cristian.
--SA
Christian Amado 9-Aug-12 14:46pm
   
You're welcome =)
Rate this: bad
 
good
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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle.aspx[^],
http://msdn.microsoft.com/en-us/library/hw8kes41.aspx[^].

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx[^].

—SA
  Permalink  
v3
Comments
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.
--SA
Christian Amado 9-Aug-12 14:45pm
   
You're welcome.
Rate this: bad
 
good
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();
        }
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160207.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2016
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