【问题标题】:c# How to make smooth arc region using graphics pathc#如何使用图形路径制作平滑的弧形区域
【发布时间】:2015-11-23 18:37:51
【问题描述】:

我正在尝试制作一个带有圆形边的标签控件。 这是我在继承自 Label 的 myclass 中的 OnPaint() 重写方法中的代码。

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
        e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
        e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, GradientColor1, GradientColor2, 90);
        e.Graphics.FillRectangle(brush, new Rectangle(new Point(0, 0), new Size(this.Width, this.Height)));

        using (GraphicsPath gp = new GraphicsPath())
        {
            gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180);
            gp.AddLine(new Point(this.Height / 2, 0), new Point(this.Width - (this.Height / 2), 0));
            gp.AddArc(new Rectangle(new Point(this.Width - this.Height, 0), new Size(this.Height, this.Height)), -90, 180);
            gp.CloseFigure();

            this.Region = new Region(gp);
        }

        base.OnPaint(e);
    }

问题在于它没有平滑两侧的曲线并显示故障。 这是控件的截图:

【问题讨论】:

  • 您不能对区域进行反锯齿处理。
  • 您真的需要更改区域吗?对于圆形标签,填充圆形背景似乎就足够了,无需更改区域。这样您就可以平滑地填充形状。
  • 为了说明 Reza 的观点:Label 对透明度的支持相当不错。只是不要试图让它与不同的控件重叠!
  • @Reza 我需要更改区域以使其变圆。如果您有任何交叉,请分享。

标签: c# winforms graphics controls


【解决方案1】:

如 cmets 中所述,您不能对该区域进行抗锯齿处理,因此您必须使用图形的 FillPath 方法才能对绘图进行抗锯齿处理

protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Aqua, Color.Blue, 90);

            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180);
                gp.AddLine(new Point(this.Height / 2, 0), new Point(this.Width - (this.Height / 2), 0));
                gp.AddArc(new Rectangle(new Point(this.Width - this.Height, 0), new Size(this.Height, this.Height)), -90, 180);
                gp.CloseFigure();

                e.Graphics.FillPath(brush, gp);
            }

            base.OnPaint(e);
        }

如果您真的需要更改区域,我想您必须使用比用于绘图的区域稍大的区域:

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
        e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
        e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Aqua, Color.Blue, 90);

        using (GraphicsPath gp = new GraphicsPath())
        {
            AddRoundedRectangle(gp, new Point(1, 1), new Size(this.Size.Width - 2, Size.Height - 2));
            e.Graphics.FillPath(brush, gp);
        }

        using (GraphicsPath gp = new GraphicsPath())
        {
            AddRoundedRectangle(gp, new Point(0, 0), this.Size);
            this.Region = new Region(gp);
        }

        base.OnPaint(e);
    }

    private void AddRoundedRectangle(GraphicsPath gp, Point upperLeft, Size size)
    {
        gp.AddArc(new Rectangle(upperLeft, new Size(size.Height, size.Height)), 90, 180);
        gp.AddLine(new Point(size.Height / 2 , upperLeft.Y), new Point(size.Width - (size.Height / 2), upperLeft.Y));
        gp.AddArc(new Rectangle(new Point(size.Width - size.Height, upperLeft.Y), new Size(size.Height, size.Height)), -90, 180);
        gp.CloseFigure();
    }

第二个选项的优点是会影响诸如 MouseHover 之类的事件,但可能会遇到您看到该区域的“边界”的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2019-02-09
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多