vb.net 如何在 Vb 网络中旋转标签?

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

How do I rotate a label in Vb net?

vb.net

提问by user6641274

I'm trying to rotate a label 90 degrees in Vb net and cannot get it working. My code is as follows. Any help would be appreciated.

我正在尝试将 Vb 网络中的标签旋转 90 度,但无法使其正常工作。我的代码如下。任何帮助,将不胜感激。



    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim sf As New StringFormat

    sf.Alignment = StringAlignment.Center

    sf.LineAlignment = StringAlignment.Center

    MetroLabel50.Text = ""

    e.Graphics.TranslateTransform(MetroLabel50.ClientSize.Width, MetroLabel50.ClientSize.Height)

    e.Graphics.RotateTransform(90)

    e.Graphics.DrawString("Label", MetroLabel50.Font, Brushes.Black, RectangleF.op_Implicit(MetroLabel50.ClientRectangle), sf)

    e.Graphics.ResetTransform()

End Sub

回答by Fredou

so after checking a little, this line

所以在检查了一点之后,这条线

e.Graphics.TranslateTransform(MetroLabel50.ClientSize.Width, MetroLabel50.ClientSize.Height)

should be

应该

e.Graphics.TranslateTransform(csng(MetroLabel50.ClientSize.Width/2), csng(MetroLabel50.ClientSize.Height/2))

you have to set it in the middle

你必须把它放在中间

also change Handles Me.Paintby Handles label1.Paint

也改变Handles Me.PaintHandles label1.Paint

sample code;

示例代码;

step 1, new project
step 2, drop a label in middle of the form
step 3, put that code

step 1,新建项目
step 2,在表格中间放一个标签
step 3,放那个代码

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.AutoSize = False
    Label1.Text = ""
    Label1.Width = 75
    Label1.Height = 75
    Label1.Refresh()
End Sub

Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
    e.Graphics.TranslateTransform(CSng(Label1.Width / 2), CSng(Label1.Height / 2))
    e.Graphics.RotateTransform(90)
    e.Graphics.DrawString("Hello", Label1.Font, Brushes.Black, New Point(0, 0))
    e.Graphics.ResetTransform()
End Sub

step 4, run the application

第 4 步,运行应用程序