2016-07-28 58 views
1

我有一個自定義控件應該代表一個標籤,但帶有圓角。我已經創建了控件,以便從設計器工具箱中拖放,將頂部圓角四捨五入,但文本似乎消失了。自定義控件文本缺失

我知道我可以爲文本添加另一個自定義屬性,它將顯示在底部,但文本屬性已經存在,理想情況下我想使用它。我以爲我可以用Override Property來做,但是現在還不能顯示。

任何建議,我已經複製下面我的代碼...

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 

感謝

+0

它看起來不像你正在繪製文本。 – Plutonix

+0

@Plutonix,我知道你會回答!總是在案件:)。我確實有過之前的文字繪畫,但在帶圓角的彩繪矩形下方。玩了一下之後,我好像丟掉了所有的文字:/ –

回答

1

你只畫在OnPaint覆蓋的邊界,而不是文字。在底部添加:

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

這即將固定點3,3,但你可能要添加代碼來計算根據TextAlign屬性(待辦事項),或者至少基於該Padding值的位置。

如果您希望在設計時重新繪製文本,但您還需要將Me.Invalidate()添加到Text屬性設置器中。

+0

謝謝!這很有魅力,但如果你碰巧有一些備用的代碼可以滿足文本對齊的需求,那將會很棒。 :) –

+0

如果它回答了這個問題,請點擊對號並上傳它。 「鏈接」問題在這裏沒有得到很好的迴應,因爲它隱藏了內容。其他人正在尋找TextAlign答案不會想到搜索答案的四捨五入的用戶控件。只是問另一個問題。 – Plutonix

+0

好點。謝謝,問題回答很大,我已經設法解決它併爲它編寫代碼:o) –