2017-04-19 60 views
2

我有一组框,我不喜欢在Visual Studio中可是没有边框颜色属性提供的groupbx所以我用这个代码来创建自己的组框。UI毛刺在我的计划写在自己的组框

public class MyGroupBox : GroupBox 
{ 
    private Color _borderColor = Color.Black; 

    public Color BorderColor 
    { 
     get { return this._borderColor; } 
     set { this._borderColor = value; } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     //get the text size in groupbox 
     Size tSize = TextRenderer.MeasureText(this.Text, this.Font); 

     Rectangle borderRect = e.ClipRectangle; 
     borderRect.Y = (borderRect.Y + (tSize.Height/2)); 
     borderRect.Height = (borderRect.Height - (tSize.Height/2)); 
     ControlPaint.DrawBorder(e.Graphics, borderRect, this._borderColor, ButtonBorderStyle.Solid); 

     Rectangle textRect = e.ClipRectangle; 
     textRect.X = (textRect.X + 6); 
     textRect.Width = tSize.Width; 
     textRect.Height = tSize.Height; 
     e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect); 
     e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect); 
    } 
} 

其作品“精”,我给自己买了一个黑色边框组框,而不是灰色的,当移动窗口中的组框毛刺出来,像这样除了,picture

是有这个修复或者我将不得不使用Visual Studio组框来防止此问题?我正在使用C#winforms

+0

我刚刚使用了你的groupbox,它没有这样做,你如何初始化你的(不需要控制内部),所以我可以重现它 – EpicKip

+0

不要使用'e.ClipRectangle'。改为使用'this.ClientRectangle'。 –

+0

@EpicKip它刚刚从工具箱中抓起来 – WhatsThePoint

回答

2

PaintEventArgs.ClipRectangle的文档有误导性 - 获取要绘制的矩形。。其实这个属性代表了窗口的无效的矩形,它并不总是完整的矩形。它可以用来跳过绘制矩形外的元素,但不能作为绘画的基础。

但对于所有的绘画基础矩形应该是被涂控制的ClientRectangle财产。因此,只需将e.ClipRectangle替换为this.ClientRectangle即可。