【问题标题】:Custom control in a UserControl does not render correctlyUserControl 中的自定义控件无法正确呈现
【发布时间】:2013-01-03 23:19:40
【问题描述】:

在这张照片中... ...您可以看到每个“线条颜色”标签旁边都有一个彩色圆圈。

在我的项目中,彩色圆圈是 Swatch。以下是 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,这也没有任何效果。

我担心我遗漏了一些相当明显的东西。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    问题是您根据剪切矩形计算圆的半径。因此,当线条仅部分可见时,会产生错误的值。

    你应该根据基类提供的真实矩形计算它,并让它被正常裁剪。

    【讨论】:

      猜你喜欢
      • 2011-05-12
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      相关资源
      最近更新 更多