using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ButcherFactory.Controls
{
  public class RoundPanel : Panel
  {
    public RoundPanel()
    {
      this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
      this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
    }


    // 圆角
    // ===============================================================================================
    private int _Radius = 18;  // 圆角弧度

    /// <summary>圆角弧度(0为不要圆角)</summary>
    [DefaultValue(18)]
    [Browsable(true)]
    [Description("圆角弧度(0为不要圆角)")]
    public int _setRoundRadius
    {
      get
      {
        return _Radius;
      }
      set
      {
        if (value < 0) { _Radius = 0; }
        else { _Radius = value; }
        base.Refresh();
      }
    }


    // 圆角代码
    public void Round(System.Drawing.Region region)
    {
      System.Drawing.Drawing2D.GraphicsPath oPath = new System.Drawing.Drawing2D.GraphicsPath();
      int x = 0;
      int y = 0;
      int thisWidth = this.Width;
      int thisHeight = this.Height;
      int angle = _Radius;
      oPath.AddArc(x, y, angle, angle, 180, 90);                                 // 左上角
      oPath.AddArc(thisWidth - angle, y, angle, angle, 270, 90);                 // 右上角
      oPath.AddArc(thisWidth - angle, thisHeight - angle, angle, angle, 0, 90);  // 右下角
      oPath.AddArc(x, thisHeight - angle, angle, angle, 90, 90);                 // 左下角
      oPath.CloseAllFigures();
      Region = new System.Drawing.Region(oPath);
    }


    public RoundPanel(IContainer container)
    {
      container.Add(this);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
    {
      base.OnPaint(pe);
      pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      Round(this.Region);  // 圆角
    }

    protected override void OnResize(EventArgs eventargs)
    {
      base.OnResize(eventargs);
      base.Refresh();
    }
  }
}
