在的WinForms,我有子類一GroupBox
更改此控件的邊框顏色。
問題
在設計模式(在視覺建設者的VisualStudio的),如果我進行任何更改控制我Groupbox
裏面可以說每個點擊控制變化在TEXTFONT然後我Groupbox
重繪控制是這樣的:
注意:使控件無效後,它會再次正確重繪。
問題
這是一個已知的問題時,業主繪製存儲控制集合像GroupBox
的容器?
我錯過了OnPaint
方法來解決這個繪畫問題嗎?
CODE
VB版本:
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'?這不是我期望看到的那樣。確保子控件沒有邊框。圖片中的黃色是什麼 - 它是轉載的標題嗎? – Plutonix 2014-12-01 21:22:58
@Plutonix感謝您的評論。 1)arg是一個ButtonBorderStyle:http://msdn.microsoft.com/en-us/library/system.windows.forms.controlpaint.drawborder%28v=vs.110%29.aspx 2)是的,似乎是黃色標題被重新繪製在內部控件的位置(並且組框邊框也被重新繪製),我不知道爲什麼會發生這種情況:( – ElektroStudios 2014-12-01 21:57:03
我不認爲它是顯示的代碼的結果 – Plutonix 2014-12-01 22:10:04