【问题标题】:Custom Control Text Missing自定义控件文本丢失
【发布时间】:2016-07-28 12:48:36
【问题描述】:

我有一个自定义控件,它应该代表一个标签,但带有圆角。我已经创建了控件,因此它可以从设计器工具箱中拖放,圆角顶角,但文本似乎消失了。

我知道我可以为 Text 添加另一个自定义属性,该属性将显示在底部,但 Text 属性已经存在,理想情况下我想使用它。我以为我可以使用覆盖属性来做到这一点,但现在它仍然没有显示。

任何建议,我已经在下面复制了我的代码...

Imports System.Windows.Forms.Design
Imports System.Runtime.InteropServices
Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class CustomControl
    Inherits System.Windows.Forms.UserControl
    Private m_Radius As Integer
    Private m_BorderWidth As Integer
    Private m_FillColor As Color
    Private m_Text As String = Me.Text
    Private m_Label As Label

    Private components As System.ComponentModel.Container = Nothing

    Public Sub New()

        MyBase.BorderStyle = Windows.Forms.BorderStyle.None
    End Sub


    ''' <summary>
    ''' Indicates a Radius of the control's corners
    ''' </summary>
    ''' <returns>The corner Radius.</returns>
    Public Property Radius As Integer
        Get
            Return m_Radius
        End Get
        Set(value As Integer)
            m_Radius = value
        End Set
    End Property

    ''' <summary>
    ''' Indicates the width to draw the outer border of the control.
    ''' </summary>
    ''' <returns>The border width.</returns>
    Public Property BorderWidth As Integer
        Get
            Return m_BorderWidth
        End Get
        Set(value As Integer)
            m_BorderWidth = value
        End Set
    End Property

    ''' <summary>
    ''' Indicates the outline colour of the control.
    ''' </summary>
    ''' <returns>The outline colour.</returns>
    Public Property FillColor As Color
        Get
            Return m_FillColor
        End Get
        Set(value As Color)
            m_FillColor = value
        End Set
    End Property

    <Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Overrides Property Text() As String
        Get
            Return m_Text
        End Get
        Set(ByVal value As String)
            m_Text = value
            'This line just for update
            'the UI when I design to check
            'if the values are saved.
            MyBase.Text = value
        End Set
    End Property


    Protected Overrides Sub onPaint(e As PaintEventArgs)
        Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
        rect.X = rect.X + 1
        rect.Y = rect.Y + 1
        rect.Width -= 2
        rect.Height -= 2

        Using bb As GraphicsPath = GetPath(rect, Radius)
            'Draw the background
            Using br As Brush = New SolidBrush(FillColor)
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality
                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear
                e.Graphics.FillPath(br, bb)
            End Using
            'Draw the border
            Using br As Brush = New SolidBrush(Color.Black)
                rect.Inflate(-1, -1)
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality
                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear
                e.Graphics.DrawPath(New Pen(br, BorderWidth), bb)
            End Using
        End Using
    End Sub

    Protected Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
        Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width - 1, h As Int32 = rc.Height - 1
        r = r << 1
        Dim path As GraphicsPath = New GraphicsPath()
        If r > 0 Then
            If (r > h) Then r = h
            If (r > w) Then r = w

            ' Top Left
            path.AddArc(x, y, r, r, 180, 90)

            ' Top Right
            path.AddArc(x + w - r, y, r, r, 270, 90)

            'Bottom Right
            'path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
            path.AddLine(x + w, y + h, x + w, y + h)

            ' Bottom Left
            ' path.AddArc(x, y + h - r, r, r, 90, 90)
            path.AddLine(x, y + h, x, y + h)

            path.CloseFigure()
        Else
            path.AddRectangle(rc)
        End If
        Return path
    End Function

End Class

谢谢

【问题讨论】:

  • 看起来不像是在绘制文本。
  • @Plutonix,我就知道是你回复了!总是在案件:)。我之前确实有过文字绘制,但在绘制的圆角矩形下方。在玩了一会儿之后,我似乎把所有的文字都弄丢了:/

标签: .net vb.net user-controls


【解决方案1】:

您仅在 OnPaint 覆盖中绘制边框,而不是文本。在底部添加:

TextRenderer.DrawText(e.Graphics, m_Text, Me.Font, 
                     New Point(3, 3), Me.ForeColor)

这将绘制到固定点 3,3,但您可能需要添加代码以基于 TextAlign 属性 (ToDo) 或至少基于 Padding 值来计算位置。

如果您希望它在更改文本时在设计时重绘文本,您还必须将 Me.Invalidate() 添加到 Text 属性设置器。

【讨论】:

  • 谢谢!这很有魅力,但如果你碰巧有一些用于文本对齐的备用代码,那就太棒了。 :)
  • 如果回答了问题,请点击对勾并点赞。 “链接”问题在这里不受欢迎,因为它隐藏了内容。寻找 TextAlign 答案的其他人不会考虑搜索圆形用户控件的答案。再问一个问题。
  • 好点。谢谢,问题得到了很好的回答,而且我已经设法解决它并为其编写代码:o)
  • 根据复杂程度,Padding 就足够了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多