2014-02-17 35 views
1

我用下面的代碼來創建一個組框用彩色邊框:的WinForms組框

Public Class BorderGroupBox 
    Inherits GroupBox 

    Private _borderColor As Color 
    Private _borderWidth As Integer 
    Private _borderStyle As ButtonBorderStyle 

    ... 

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
     Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font) 
     Dim borderRect As Rectangle = e.ClipRectangle 
     borderRect.Y = CInt((borderRect.Y + (tSize.Height/2))) 
     borderRect.Height = CInt((borderRect.Height - (tSize.Height/2))) 
     ControlPaint.DrawBorder(e.Graphics, borderRect, _borderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle) 
     Dim textRect As Rectangle = e.ClipRectangle 
     textRect.X = (textRect.X + 6) 
     textRect.Width = tSize.Width + 6 
     textRect.Height = tSize.Height 
     e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect) 
     e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect) 
    End Sub 
End Class 

的問題是,它被放在一個滾動的容器內,並且如果是滾動邊界ISN 「T重繪正確:

Redraw

+0

在哪種類型的可滾動容器中Panel,SplitContainer? – Coder

+0

這是一個flowlayoutpanel – Matthias

+0

如何使這個groupbox背景顏色內部的控制器透明? –

回答

5

你可以得到它胡作非爲比差了很多:

enter image description here

這是因爲你的代碼使用e.ClipRectangle而出錯。請注意,它會在您的代碼段中出現兩次。該變量確實不是給你的邊框矩形。它告訴你需要重新繪製多少客戶區域。這是一個優化的機會,您可以通過省略不需要刷新的客戶區域的部分來縮小繪圖。

它是通常是大小與顯示矩形相同,這就是爲什麼它看起來像它工作得很好。但不是當你把它放在一個可滾動的容器中時,Windows通過將可移動的客戶區的各部分進行blitting來優化滾動。然後爲由滾動顯示的部分生成一個繪畫。用一個小的e.ClipRectangle。你可以在截圖中看到,注意小矩形。

用Me.DisplayRectangle替換e.ClipRectangle。

+0

我用'Me.DisplayRectangle'替換了兩行,但現在它根本沒有繪製任何邊框。 – Matthias

+0

我在發佈之前測試過它,使用DisplayRectangle工作正常。正如它應該。 –

+0

你有沒有改變代碼中的其他內容?你用什麼容器? – Matthias

1

此類允許爲所有框設置邊框,或者通過將邊框顏色控件添加到組框的屬性選項卡來單獨設置邊框。

Public Class GroupBoxA 
    Inherits GroupBox 

    Private _borderColor As Color 

    Public Sub New() 
     MyBase.New() 
     Me._borderColor = Color.OrangeRed 
    End Sub 

    Public Property BorderColor() As Color 
     Get 
      Return Me._borderColor 
     End Get 
     Set(ByVal value As Color) 
      Me._borderColor = value 
     End Set 
    End Property 

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
     Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font) 
     Dim borderRect As Rectangle = Me.DisplayRectangle 

     borderRect.Y = (borderRect.Y + (tSize.Height/2)) 
     borderRect.Height = (borderRect.Height - (tSize.Height/2)) 

     ControlPaint.DrawBorder(e.Graphics, borderRect, Me._borderColor, 
           ButtonBorderStyle.Solid) 

     Dim textRect As Rectangle = Me.DisplayRectangle 
     textRect.X = (textRect.X + 6) 
     textRect.Width = tSize.Width 
     textRect.Height = tSize.Height 

     e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect) 
     e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect) 
    End Sub 
End Class 
+0

對不起愚蠢的問題,但我如何使用這個新的類在我的窗體上顯示爲一個新的組框控件? –

+0

其實我只是想通了,謝謝 –

+0

Dim grpNew As New GroupBoxA Controls.Add(grpNew) –

-1

您必須使用Me.ClientRectangle而不是Me.DisplayRectangle作爲boder和文本。如果你使用舊的方式,然後閃爍問題沒有解決和組框的文本將不會顯示。