【发布时间】:2014-12-01 19:45:07
【问题描述】:
场景
在 WinForms 中,我将 GroupBox 子类化以更改此控件的边框颜色。
问题
在 设计模式(在 VisualStudio 的 visual builder 中),如果我对 Groupbox 中的控件进行任何类型的更改,可以说单击每个控件要更改文本字体,然后我的 Groupbox 会像这样重绘控件:
注意:在使控件无效后,它会重新正确地重新绘制。
问题
这是一个已知问题,当所有者绘制一个存储控件集合(如GroupBox)的容器时?
我在 OnPaint 方法中缺少什么来解决这个绘画问题?
代码
VB版本:
''' <summary>
''' Handles the <see cref="E:Paint"/> event.
''' </summary>
''' <param name="e">A <see cref="T:PaintEventArgs"/> that contains the event data.</param>
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' MyBase.OnPaint(e)
Me.DrawBorder(e)
End Sub
''' <summary>
''' Draws a border on the control surface.
''' </summary>
Private Sub DrawBorder(ByVal e As PaintEventArgs)
' The groupbox header text size.
Dim textSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
' The width of the blankspace drawn at the right side of the text.
Dim blankWidthSpace As Integer = 3
' The thex horizontal offset.
Dim textOffset As Integer = 7
' The rectangle where to draw the border.
Dim borderRect As Rectangle = e.ClipRectangle
With borderRect
.Y = .Y + (textSize.Height \ 2)
.Height = .Height - (textSize.Height \ 2)
End With
' The rectangle where to draw the header text.
Dim textRect As Rectangle = e.ClipRectangle
With textRect
.X = .X + textOffset
.Width = (textSize.Width - blankWidthSpace)
.Height = textSize.Height
End With
' Draw the border.
ControlPaint.DrawBorder(e.Graphics, borderRect, Me.borderColor1, Me.borderStyle1)
' Fill the text rectangle.
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
' Draw the text on the text rectangle.
textRect.Width = textSize.Width + (blankWidthSpace * 2) ' Fix the right side space.
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect)
End Sub
C#版本:
/// <summary>
/// Handles the <see cref="E:Paint"/> event.
/// </summary>
/// <param name="e">A <see cref="T:PaintEventArgs"/> that contains the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
// MyBase.OnPaint(e)
this.DrawBorder(e);
/// <summary>
/// Draws a border on the control surface.
/// </summary>
private void DrawBorder(PaintEventArgs e)
{
// The groupbox header text size.
Size textSize = TextRenderer.MeasureText(this.Text, this.Font);
// The width of the blankspace drawn at the right side of the text.
int blankWidthSpace = 3;
// The thex horizontal offset.
int textOffset = 7;
// The rectangle where to draw the border.
Rectangle borderRect = e.ClipRectangle;
var _with1 = borderRect;
_with1.Y = _with1.Y + (textSize.Height / 2);
_with1.Height = _with1.Height - (textSize.Height / 2);
// The rectangle where to draw the header text.
Rectangle textRect = e.ClipRectangle;
var _with2 = textRect;
_with2.X = _with2.X + textOffset;
_with2.Width = (textSize.Width - blankWidthSpace);
_with2.Height = textSize.Height;
// Draw the border.
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor1, this.borderStyle1);
// Fill the text rectangle.
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
// Draw the text on the text rectangle.
textRect.Width = textSize.Width + (blankWidthSpace * 2);
// Fix the right side space.
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
【问题讨论】:
-
什么是
borderStyle1?这不是我期望在那里看到的 arg。确保子控件也没有边框。图片中的黄色是什么 - 是转载的标题吗? -
@Plutonix 感谢您的评论。 1) arg 是 ButtonBorderStyle: msdn.microsoft.com/en-us/library/… 2) 是的,似乎黄色标题在内部控件的位置被重新绘制(并且组框边框也被重新绘制),我不知道为什么会这样:(
-
我不认为这是显示代码的结果。
-
@Plutonix 应该是因为这是我试图管理的唯一功能(bordercolor)没有其他重要的东西包含完整的源代码,无论如何这里是完整的源代码:stackoverflow.com/questions/27230168/…(见我的回答,有)
标签: c# .net vb.net user-controls gdi+