Windows窗体中的垂直文本使用onPaint

时间:2014-02-21 03:07:42

标签: vb.net button

我发现这个Vertical Text on Button Control我觉得这看起来很简单,但我无法让它发挥作用。

Public Class VerticalButton3

    Inherits System.Windows.Forms.Button

    Private _VerticalText As String
    Public Property VerticalText() As String
        Get
            Return _VerticalText
        End Get
        Set(ByVal value As String)
            _VerticalText = value
        End Set
    End Property

    Private Fmt As New StringFormat

    Public Sub New()
        Fmt.Alignment = StringAlignment.Center
        Fmt.LineAlignment = StringAlignment.Center
    End Sub

    Protected Overrides Sub OnPaint(ByVal PaintEvt As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint (PaintEvt)
        PaintEvt.Graphics.TranslateTransform(Width, 0)
        PaintEvt.Graphics.RotateTransform (90)
        PaintEvt.Graphics.DrawString(_VerticalText, Font, Brushes.Black, New Rectangle(0, 0, Height, Width), Fmt)
    End Sub

End Class

Button 但是我得到了垂直和水平文本。

我尝试使用与Vertical Label Control in VB.NET类似的Public Overrides Property Text(),但这不起作用

如何仅获取垂直文本?

1 个答案:

答案 0 :(得分:1)

我测试了你的代码。如果您将Text属性设置为“”

,它可以按您的要求工作

这是我试过的代码

Private WithEvents vbtn As New VerticalButton3
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    vbtn.Top = 0
    vbtn.Left = 0
    vbtn.Text = "" ' Note Text property is set to ""
    vbtn.VerticalText = "Vertical"
    vbtn.Height = 100
    Controls.Add(vbtn)

End Sub

或者,您可以在班级的构造函数中使用Text=""

Public Sub New()
    Fmt.Alignment = StringAlignment.Center
    Fmt.LineAlignment = StringAlignment.Center
    Text = ""
End Sub

编辑:我认为最好覆盖Text属性本身,因为如果错误地设置了Text属性,则两者都可能出现。以下是覆盖Text属性的方法(您可能不再需要VerticalText属性)。

Public Shadows Property Text
    Get
        Return _VerticalText
    End Get
    Set(ByVal value)
        _VerticalText = value
    End Set
End Property