vb.net 表单中的主表单文本对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16943253/
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-09-17 13:51:06  来源:igfitidea点击:

Main form text aligning in vb.net form

.netvb.netformstextalignment

提问by Kraxed

I want to align my main form text to the centre of the title bar. I have no idea how I would start this. I have also "googled" this problem and have found no solution.

我想将我的主表单文本与标题栏的中心对齐。我不知道我将如何开始。我也“谷歌”了这个问题,但没有找到解决方案。

回答by maxedev

This works for me with the caveats mentioned in my comment link:

这适用于我的评论链接中提到的警告:

Public Class Form1
 Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    CenterMe()
 End Sub
 Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
    CenterMe()
 End Sub
 Private Sub CenterMe()
    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

    Me.Refresh()
 End Sub
End Class