Windows窗体标题栏文本对齐 [英] Windows Form Title Bar Text alignment

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

问题描述

我有使用.net framework 3.5用c Sharp编码的Windows应用程序.
我需要一种动态的方式来在所有申请表格中将标题栏文本居中.

请注意,就我而言,我不想将标签停靠在顶部并将其文本居中放置,也不想使用自定义控件.
我需要的是将表格标题对准中心.
在此先感谢

I have windows application coded with c sharp using .net framework 3.5.
I need a dynamic way to center title bar text in all my application forms.

Please note that in my case I don''t want to dock label at top and center it''s text OR use a custom control.
All I need is to align the form caption at center.
thanks in advance

推荐答案

没有直接的方法可以尝试

http://www.codeproject.com/KB/dialog/FormEx.aspx ]
There is no direct way and try this

http://www.codeproject.com/KB/dialog/FormEx.aspx]


只需将这些代码放入表单的resize事件中:

Just drop this code in your form''s resize event:

Dim g as Graphics = Me.CreateGraphics()
Dim startingPoint as Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 2)
Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
Dim tmp As String = " "
Dim tmpWidth As Double = 0

Do  
  tmp += " "
  tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
        
Me.Text = tmp & Me.Text.Trim & tmp



这对我来说很好.

-Pete



This worked well for me.

- Pete


只需创建此方法,然后在要居中对齐的窗体的Form Load事件中调用,即可将标题栏的文本对齐.
Just Create this method and Call in Form Load event of the form in which you want to center align the text of title bar.
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();
        }


这篇关于Windows窗体标题栏文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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