using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Customize.Controls
{
public class RoundRectangle
{
#region Initializes
///
/// (构造函数).Initializes a new instance of the class.
///
/// The roundRect.
/// The radius.
public RoundRectangle(Rectangle rect, int radius)
: this(rect, new CornerRadius(radius))
{
}
///
/// (构造函数).Initializes a new instance of the class.
///
/// The roundRect.
/// The corner radius.
public RoundRectangle(Rectangle rect, CornerRadius cornerRadius)
{
this.Rect = rect;
this.CornerRadius = cornerRadius;
}
#endregion
#region Properties
///
/// 获取或者设置矩形区域
///
/// The roundRect.
public Rectangle Rect { get; set; }
///
/// 获取或者设置圆角值
///
/// The corner radius.
public CornerRadius CornerRadius { get; set; }
#endregion
#region Methods
///
/// 获取该圆角矩形的GraphicsPath对象(圆角使用Bezier曲线实现)
///
///
/// Return a data(or instance) of GraphicsPath.
///
public GraphicsPath ToGraphicsBezierPath()
{
GraphicsPath path = new GraphicsPath();
int x = this.Rect.X;
int y = this.Rect.Y;
int w = this.Rect.Width;
int h = this.Rect.Height;
path.AddBezier(x, y + this.CornerRadius.TopLeft, x, y, x + this.CornerRadius.TopLeft, y, x + this.CornerRadius.TopLeft, y);
path.AddLine(x + this.CornerRadius.TopLeft, y, x + w - this.CornerRadius.TopRight, y);
path.AddBezier(x + w - this.CornerRadius.TopRight, y, x + w, y, x + w, y + this.CornerRadius.TopRight, x + w, y + this.CornerRadius.TopRight);
path.AddLine(x + w, y + this.CornerRadius.TopRight, x + w, y + h - this.CornerRadius.BottomRigth);
path.AddBezier(x + w, y + h - this.CornerRadius.BottomRigth, x + w, y + h, x + w - this.CornerRadius.BottomRigth, y + h, x + w - this.CornerRadius.BottomRigth, y + h);
path.AddLine(x + w - this.CornerRadius.BottomRigth, y + h, x + this.CornerRadius.BottomLeft, y + h);
path.AddBezier(x + this.CornerRadius.BottomLeft, y + h, x, y + h, x, y + h - this.CornerRadius.BottomLeft, x, y + h - this.CornerRadius.BottomLeft);
path.AddLine(x, y + h - this.CornerRadius.BottomLeft, x, y + this.CornerRadius.TopLeft);
path.CloseFigure();
return path;
}
///
/// 获取该圆角矩形的GraphicsPath对象(圆角使用矩形圆弧曲线曲线实现)
///
///
public GraphicsPath ToGraphicsArcPath()
{
GraphicsPath path = new GraphicsPath();
int x = this.Rect.X;
int y = this.Rect.Y;
int w = this.Rect.Width;
int h = this.Rect.Height;
path.AddArc(x, y, this.CornerRadius.TopLeft, this.CornerRadius.TopLeft, 180, 90);
path.AddArc(x + w - this.CornerRadius.TopRight, y, this.CornerRadius.TopRight, this.CornerRadius.TopRight, 270, 90);
path.AddArc(x + w - this.CornerRadius.BottomRigth, y + h - this.CornerRadius.BottomRigth,
this.CornerRadius.BottomRigth, this.CornerRadius.BottomRigth,
0, 90);
path.AddArc(x, y + h - this.CornerRadius.BottomLeft, this.CornerRadius.BottomLeft, this.CornerRadius.BottomLeft, 90, 90);
path.CloseFigure();
return path;
}
///
/// 获取该圆角矩形的GraphicsPath对象(天使之翼的区域样式,主要用于Tabcontrol的标签样式)
///
///
/// Return a data(or instance) of GraphicsPath.
///
public GraphicsPath ToGraphicsAnglesWingPath()
{
GraphicsPath path = new GraphicsPath();
int x = this.Rect.X;
int y = this.Rect.Y;
int w = this.Rect.Width;
int h = this.Rect.Height;
path.AddBezier(x, y + this.CornerRadius.TopLeft, x, y, x + this.CornerRadius.TopLeft, y, x + this.CornerRadius.TopLeft, y);
path.AddLine(x + this.CornerRadius.TopLeft, y, x + w - this.CornerRadius.TopRight, y);
path.AddBezier(x + w - this.CornerRadius.TopRight, y, x + w, y, x + w, y + this.CornerRadius.TopRight, x + w, y + this.CornerRadius.TopRight);
path.AddLine(x + w, y + this.CornerRadius.TopRight, x + w, y + h - this.CornerRadius.BottomRigth);
path.AddBezier(x + w, y + h - this.CornerRadius.BottomRigth, x + w, y + h, x + w + this.CornerRadius.BottomRigth, y + h, x + w + this.CornerRadius.BottomRigth, y + h);
path.AddLine(x + w + this.CornerRadius.BottomRigth, y + h, x - this.CornerRadius.BottomLeft, y + h);
path.AddBezier(x - this.CornerRadius.BottomLeft, y + h, x, y + h, x, y + h - this.CornerRadius.BottomLeft, x, y + h - this.CornerRadius.BottomLeft);
path.AddLine(x, y + h - this.CornerRadius.BottomLeft, x, y + this.CornerRadius.TopLeft);
path.CloseFigure();
return path;
}
#endregion
}
}