none
winfrom中自定义圆角Panel控件的圆角外的区域能否去掉! RRS feed

  • 问题

  • 就是看到上面左边有两块红色的区域能否去掉,这样就是真正意义上的圆角Panel了。不知道该如何处理,恳请大神赐教!

    源码如下:

    public partial class FrmCPanel : Form
        {
            public FrmCPanel()
            {
                InitializeComponent();

            }

            private void Draw(Rectangle rectangle, Graphics g, int _radius, bool cusp, Color begin_color, Color end_color)
            {
                int span = 2;
                //抗锯齿
                g.SmoothingMode = SmoothingMode.AntiAlias;
                //渐变填充
                LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
                //画尖角
                if (cusp)
                {
                    span = 10;
                    PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
                    PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
                    PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
                    PointF[] ptsArray = { p1, p2, p3 };
                    g.FillPolygon(myLinearGradientBrush, ptsArray);
                }
                //填充
                //g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, _radius));
                g.DrawPath(Pens.Red, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, _radius));
                g.FillPath(Brushes.Green, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, _radius));
                //g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle, _radius));
            }


            public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
            {
                //左边圆角
                GraphicsPath gp = new GraphicsPath();
                gp.AddArc(x, y, radius, radius, 180, 90);
                gp.AddLine(new Point(x + width, y), new Point(x + width, y + height));
                gp.AddArc(x, height - radius, radius, radius, 90, 90);
                gp.CloseAllFigures();
                return gp;
            }

            private void panel2_Paint(object sender, PaintEventArgs e)
            {
                Draw(e.ClipRectangle, e.Graphics, 30, false, Color.FromArgb(113, 113, 113), Color.FromArgb(0, 0, 0));
                base.OnPaint(e);
                Graphics g = e.Graphics;
                g.DrawString("其实我是个Panel", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));
            }

    2017年12月22日 8:22

答案

  • Hi,

    你可以参考下面的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                GraphicsPath formPath = new GraphicsPath();
                Rectangle rect = new Rectangle(0, 0, panel1.Width, panel1.Height);
    
                formPath = GetRectPath(rect, 60);
    
                panel1.Region = new Region(formPath);
            }
    
            private GraphicsPath GetRectPath(Rectangle rect, int radius)
            {
    
                int diameter = radius;
                Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
                GraphicsPath path = new GraphicsPath();
    
                // 左上角  
                path.AddArc(arcRect, 180, 90);
    
                // 右上角  
                arcRect.X = rect.Right - diameter;
                path.AddArc(arcRect, 270, 90);
    
                // 右下角  
                arcRect.Y = rect.Bottom - diameter;
                path.AddArc(arcRect, 0, 90);
    
                // 左下角  
                arcRect.X = rect.Left;
                path.AddArc(arcRect, 90, 90);
                path.CloseFigure();//闭合曲线  
                return path;
    
            }
        }
    }

    上面这段代码是可以把panel的四角变成圆弧。下面是我测试结果。

    这个方法的设计,你可以参考作者的原帖,尊重原版。

    https://social.msdn.microsoft.com/Forums/zh-CN/211912ee-f851-4e99-8ce6-02ac5c97d018/pictureboxregion-?forum=visualcshartzhchs#83863fb3-7c4b-49f4-85d2-ba5764965aaf

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年12月26日 9:30
    版主

全部回复

  • Hi,

    欢迎在MSDN论坛发帖。

    你有没有尝试把背景颜色,设置成默认,我感觉应该是你代码某处,把它设置成红色。

    你可以尝试按照上图修改 backcolor.

    你可以看这边的解决方案,已经成功解决你遇到的问题。

    https://www.cnblogs.com/qiaoke/p/6124693.html

    Best Regards,

    Hart

    注意:此回复包含对第三方万维网站点的引用。 Microsoft提供此信息为您提供方便。 Microsoft不控制这些网站,并且没有测试在这些网站上发现的任何软件或信息; 因此,Microsoft不能对任何软件或信息的质量,安全性或适用性做任何声明。 使用互联网上发现的任何软件都存在固有的危险,Microsoft警告您,在从互联网检索任何软件之前,请确保您完全理解风险。


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年12月25日 7:42
    版主
  • @Hart Wang,我知道是背景色引起的,但是我就是想写个Panel的扩展控件,并且让他的轮廓或者说边框本身就是圆弧形的,这样怎么实现呢?!
    2017年12月25日 9:14
  • Hi,

    你可以参考下面的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                GraphicsPath formPath = new GraphicsPath();
                Rectangle rect = new Rectangle(0, 0, panel1.Width, panel1.Height);
    
                formPath = GetRectPath(rect, 60);
    
                panel1.Region = new Region(formPath);
            }
    
            private GraphicsPath GetRectPath(Rectangle rect, int radius)
            {
    
                int diameter = radius;
                Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
                GraphicsPath path = new GraphicsPath();
    
                // 左上角  
                path.AddArc(arcRect, 180, 90);
    
                // 右上角  
                arcRect.X = rect.Right - diameter;
                path.AddArc(arcRect, 270, 90);
    
                // 右下角  
                arcRect.Y = rect.Bottom - diameter;
                path.AddArc(arcRect, 0, 90);
    
                // 左下角  
                arcRect.X = rect.Left;
                path.AddArc(arcRect, 90, 90);
                path.CloseFigure();//闭合曲线  
                return path;
    
            }
        }
    }

    上面这段代码是可以把panel的四角变成圆弧。下面是我测试结果。

    这个方法的设计,你可以参考作者的原帖,尊重原版。

    https://social.msdn.microsoft.com/Forums/zh-CN/211912ee-f851-4e99-8ce6-02ac5c97d018/pictureboxregion-?forum=visualcshartzhchs#83863fb3-7c4b-49f4-85d2-ba5764965aaf

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年12月26日 9:30
    版主