C# UserControl中的自定义控件无法正确呈现
在这张照片中。。。 ... 您可以看到每个“线条颜色”标签旁边都有一个彩色圆圈 在我的项目中,彩色圆圈是一个样例。以下是样例的完整代码文件:C# UserControl中的自定义控件无法正确呈现,c#,winforms,C#,Winforms,在这张照片中。。。 ... 您可以看到每个“线条颜色”标签旁边都有一个彩色圆圈 在我的项目中,彩色圆圈是一个样例。以下是样例的完整代码文件: public class Swatch : System.Windows.Forms.Panel { /*private int _Radius = 20; [System.ComponentModel.Category("Layout")] public int Radius { get { return
public class Swatch : System.Windows.Forms.Panel
{
/*private int _Radius = 20;
[System.ComponentModel.Category("Layout")]
public int Radius
{
get { return _Radius; }
set { _Radius = value; }
} */
private System.Drawing.Color _BorderColor = System.Drawing.Color.Transparent;
[System.ComponentModel.Category("Appearance")]
public System.Drawing.Color BorderColor
{
get { return _BorderColor; }
set { _BorderColor = value; }
}
private System.Drawing.Color _FillColor = System.Drawing.Color.Blue;
[System.ComponentModel.Category("Appearance")]
public System.Drawing.Color FillColor
{
get { return _FillColor; }
set { _FillColor = value; }
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
System.Drawing.Rectangle RealRect = new System.Drawing.Rectangle(e.ClipRectangle.Location, e.ClipRectangle.Size);
RealRect.Inflate(-1, -1);
int Radius = Math.Min(RealRect.Size.Height, RealRect.Size.Width);
System.Drawing.Rectangle SqRect = new System.Drawing.Rectangle();
SqRect.Location = RealRect.Location;
SqRect.Size = new System.Drawing.Size(Radius, Radius);
System.Drawing.Drawing2D.CompositingQuality PrevQual = e.Graphics.CompositingQuality;
using (System.Drawing.SolidBrush Back = new System.Drawing.SolidBrush(this.FillColor))
{
using (System.Drawing.Pen Pen = new System.Drawing.Pen(new System.Drawing.SolidBrush(this.BorderColor)))
{
//e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.FillEllipse(Back, SqRect);
e.Graphics.DrawEllipse(Pen, SqRect);
}
}
e.Graphics.CompositingQuality = PrevQual;
}
public Swatch()
{
this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, true);
this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
this.DoubleBuffered = true;
}
}
每一行都是一个UserControl,它由一个TableLayoutPanel、labels、一个样例控件和一个NumericUpDown框组成
大约有10行,它们被放置在TableLayoutPanel中,该面板位于选项卡控件的选项卡页中。选项卡页面的AutoScroll
设置为true
,因此溢出会导致选项卡页面滚动
问题在于,每当我运行应用程序并上下滚动时,样本(彩色圆圈)就会撕裂并显示各种瑕疵,如上图所示。我希望有一个干净的滚动没有渲染工件
我尝试过使用SetStyle
(如这里所建议的),但没有效果
UserControl(每行)已将双缓冲
设置为true
,这也没有任何效果
我担心我遗漏了一些非常明显的东西。问题是,您根据剪切矩形计算圆的半径。因此,当该行仅部分可见时,会产生错误的值
您应该根据基类提供的实际矩形计算它,并让它正常剪裁。问题是,您需要根据剪裁矩形计算圆的半径。因此,当该行仅部分可见时,会产生错误的值 您应该根据基类提供的实际矩形来计算它,并让它被正常剪裁