在这张照片中......
...你可以看到每个“线条颜色”标签旁边都有一个彩色圆圈。
在我的项目中,彩色圆圈是斯沃琪。以下是Swatch的完整代码文件:
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,标签,Swatch控件和NumericUpDown框组成。
大约有10行,它们放在TableLayoutPanel中,它位于选项卡控件上的TabPage内。标签页的AutoScroll
设置为true
,以便溢出导致标签页滚动。
问题是每当我运行应用程序并向上和向下滚动时,色板(彩色圆圈)会撕裂并显示各种伪影,如上图所示。我想要干净的滚动,没有渲染工件。
我已尝试使用SetStyle
(如此处Painting problem in windows form所示),但它没有效果。
UserControl(每一行)的DoubleBuffered
设置为true
,这也没有效果。
我担心我错过了一些相当明显的东西。
答案 0 :(得分:4)
问题是你根据剪裁矩形计算圆的半径。因此,当线条仅部分可见时,会产生不良值。
您应该根据真实矩形(基类提供的矩形)进行计算,然后正常剪裁。