居中对齐表单标题栏的TExt. [英] Center Align the TExt of Form Title Bar.

查看:98
本文介绍了居中对齐表单标题栏的TExt.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望表单的文本显示在标题栏的中间.它是Windows应用程序.
我尝试了以下代码:

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 !!!

推荐答案

可能对您有所帮助: ^ ]

那一个呢: WinForm Extended [
May be helps you a little: Easy Customize Title Bar[^]

What about this one: WinForm Extended[^]

Regards.


根据定义,如果不能完美.您不应该这样做,因为这是由OS控制的.例如,当我在现在使用的Ubuntu版本上运行.NET应用程序时,它将居中显示表单标题,但取决于桌面管理器.如果我使用了类似的方法,系统将尝试使填充文本居中,并且看起来向右偏移.而且您不知道下一版Windows的外观以及可用的外观.因此,从不做这样的事情.

现在,如果使用直接相反的方法,您仍然可以完全自定义标题栏的外观.但是您不能使用组合方法.

首先,您可以使用原始(非托管)Windows API和非客户端Windows消息完全重绘表单的非客户端区域.我也不推荐使用它,因为这是Windows的过多要求. .NET尝试退出Windows,CLR旨在实现平台兼容性.如果您准确地编写Forms应用程序,则可以在不同的OS上运行它们,而无需重新编译.使用Windows特有的技巧,您将破坏这种兼容性,并且很可能破坏与未来Microsoft系统的兼容性.

第二种方法同时是最彻底和最安全的,它不需要任何互操作.您可以显示您的表单,而无需任何非客户区域.为此,请将表单属性 System.Windows.Forms.FormFormBorderStyle设置为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 [ ^ ].

完成此操作后,只需在常规客户区中模拟所有非客户控件(例如标题栏)即可.只需创建并添加一些自定义控件即可模拟需要非客户端表单元素的内容.

现在,有一个额外的建议:如果某些开发人员使用这种高度定制的表单,他们通常会进一步采取措施使这些表单不是矩形的,而是具有一些看起来很时髦的形状.使用属性System.Windows.Forms.Control.Region:
可以轻松完成此操作 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx [ ^ ].

—SA
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


我创建了此方法并在Form Load事件中调用.

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();
        }


这篇关于居中对齐表单标题栏的TExt.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆