C# 如何在 Windows 窗体中居中对齐标题栏文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11947314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:48:27  来源:igfitidea点击:

How To Center Align the Title Bar text In Windows Form?

c#winforms

提问by Ankush

I am developing a Windows Form Application. I want to Align the Text to Center or say to Right of Title Bar of Form. How can I do it ??

我正在开发一个 Windows 窗体应用程序。我想将文本与中心对齐或说到表单标题栏的右侧。我该怎么做 ??

回答by l46kok

If it's a text that's inside a control (Like a label), you can edit the property named "TextAlign" to accomplish what you want inside Windows Form Designer.

如果它是控件内的文本(如标签),则可以编辑名为“TextAlign”的属性以在 Windows 窗体设计器中完成所需的操作。

Or programmatically,

或者以编程方式,

Label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

If you're talking about aligning the text of the title bar, there is no way to do this through Winforms. You will have to use something like this: http://www.codeproject.com/Articles/93959/WinForm-Extended

如果您正在谈论对齐标题栏的文本,则无法通过 Winforms 执行此操作。你将不得不使用这样的东西:http: //www.codeproject.com/Articles/93959/WinForm-Extended

回答by JleruOHeP

It can be done with custom form - you will have to create your own title bar. See V4Vendettas comment;

可以使用自定义表单来完成 - 您必须创建自己的标题栏。见 V4Vendettas 评论;

Other approach (link) - is to create your own handler for form resize and insert there followong code. It will add appropriate amount of spaces from the left size of text. However you will have to add form.Refresh() and call that method in form.Load; also your window will have "..." as a text in task bar.

其他方法(链接) - 是为表单调整大小创建自己的处理程序并插入后续代码。它将从文本的左侧大小添加适当数量的空格。但是,您必须添加 form.Refresh() 并在 form.Load 中调用该方法;您的窗口也会在任务栏中将“...”作为文本。

private void UpdateTextPosition()
{
    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();
}

回答by kishore

Inside Windows Form Designer. Make the changes like this.lable1.AutoSize = false;

在 Windows 窗体设计器中。进行如下更改this.lable1.AutoSize = false

Then:

然后:

this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

回答by Pharmacist Mohamed Ayyad

 Graphics g = this.CreateGraphics();
        double fw = this.Width; // form width
        double tw = g.MeasureString(this.Text.Trim(), this.Font).Width; \ text width
        double rp = (fw - tw) / 2;
        int tt = Convert.ToInt32(rp); 
        string st = " ";
        st = st.PadRight(tt / 3);
        this.Text = st + this.Text.Trim();