public partial class CSkinPanel : Panel
{
private Color _BaseBackColor = Color.Transparent;
/// <summary>
/// 背景初始色
/// </summary>
[Browsable(true), Category("Background"), Description("背景初始色")]
public Color BaseBackColor
{
get { return _BaseBackColor; }
set { _BaseBackColor = value; this.Refresh(); }
}
private Color _GradualColor = Color.White;
/// <summary>
/// 渐变颜色
/// </summary>
[Browsable(true), Category("Background"), Description("渐变颜色")]
public Color GradualColor
{
get { return _GradualColor; }
set { _GradualColor = value; this.Refresh(); }
}
private bool _ShowGradualBack = false;
/// <summary>
/// 是否应用渐变背景
/// </summary>
[Browsable(true), Category("Background"), Description("是否应用渐变背景")]
public bool ShowGradualBack
{
get { return _ShowGradualBack; }
set { _ShowGradualBack = value; this.Refresh(); }
}
private GradualType _GradualType = GradualType.LinearGradient;
/// <summary>
/// 渐变背景类型
/// </summary>
[Browsable(true), Category("Background"), Description("渐变背景类型")]
public GradualType GradualType
{
get { return _GradualType; }
set { _GradualType = value; this.Refresh(); }
}
private LinearGradientMode _Mode = LinearGradientMode.Vertical;
/// <summary>
/// 渐变模式
/// </summary>
[Browsable(true), Category("Background"), Description("渐变模式")]
public LinearGradientMode Mode
{
get { return _Mode; }
set { _Mode = value; this.Refresh(); }
}
private int _Radius = 8;
/// <summary>
/// 圆角大小
/// </summary>
[Browsable(true), Category("Shape"), Description("圆角大小")]
public int Radius
{
get { return _Radius; }
set { _Radius = value; this.Refresh(); }
}
private RadiusStyle _RadiusStyle = RadiusStyle.None;
/// <summary>
/// 圆角样式
/// </summary>
[Browsable(true), Category("Shape"), Description("圆角样式")]
public RadiusStyle Style
{
get { return _RadiusStyle; }
set { _RadiusStyle = value; ReRegion(); }
}
private int _OffSet = 0;
/// <summary>
/// 偏移量
/// </summary>
[Browsable(true), Category("Shape"), Description("偏移量")]
public int OffSet
{
get { return _OffSet; }
set { _OffSet = value; this.Refresh(); }
}
private bool _ShowBorder = false;
/// <summary>
/// 显示边框
/// </summary>
[Browsable(true), Category("Border"), Description("显示边框")]
public bool ShowBorder
{
get { return _ShowBorder; }
set { _ShowBorder = value; this.Refresh(); }
}
private Color _BorderColor = Color.Black;
/// <summary>
/// 边框颜色
/// </summary>
[Browsable(true), Category("Border"), Description("边框颜色")]
public Color BorderColor
{
get { return _BorderColor; }
set { _BorderColor = value; this.Refresh(); }
}
private int _BorderWidth = 1;
/// <summary>
/// 边框宽度
/// </summary>
[Browsable(true), Category("Border"), Description("边框宽度")]
public int BorderWidth
{
get { return _BorderWidth; }
set { _BorderWidth = value; this.Refresh(); }
}
private Color _BorderGradualColor = Color.White;
/// <summary>
/// 边框渐变颜色
/// </summary>
[Browsable(true), Category("Border"), Description("边框渐变颜色")]
public Color BorderGradualColor
{
get { return _BorderGradualColor; }
set { _BorderGradualColor = value; this.Refresh(); }
}
private bool _ShowBorderGradual = false;
/// <summary>
/// 显示边框渐变
/// </summary>
[Browsable(true), Category("Border"), Description("显示边框渐变")]
public bool ShowBorderGradual
{
get { return _ShowBorderGradual; }
set { _ShowBorderGradual = value; this.Refresh(); }
}
private int LoadCount
{ get; set; }
IntPtr hHandle = IntPtr.Zero;//GetWindowDC(m.HWnd);
public CSkinPanel()
: base()
{
hHandle = GetWindowDC(this.Handle);
LoadCount = 0;
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲
//this.SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
//this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲
//this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0047)
ReRegion();
if (m.Msg == 0x000F || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回
{
return;
}
//建立Graphics对像
Graphics g = Graphics.FromHdc(hDC);
//画边框
RePaint(g);
//释放DC
ReleaseDC(m.HWnd, hDC);
}
}
private void ReRegion()
{
Rectangle Rectangle = new Rectangle(OffSet, OffSet, this.Width - OffSet * 2, this.Height - OffSet * 2);
GraphicsPath path = CreatePath(Rectangle, this.Radius, this.Style);
if (path == null)
return;
this.Region = new Region(path);
//this.Refresh();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
//ReRegion();
//Refresh();
}
private void RePaint(Graphics g)
{
if (g == null)
return;
try
{
g.SmoothingMode = SmoothingMode.HighQuality; //图片柔顺模式选择
g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
g.CompositingQuality = CompositingQuality.HighQuality;//再加一点
Rectangle Rectangle = new Rectangle(OffSet, OffSet, this.Width - OffSet * 2, this.Height - OffSet * 2);
GraphicsPath path = CreatePath(Rectangle, this.Radius, this.Style);
if (path == null)
return;
if (ShowGradualBack)
{
if (GradualType == Forms.GradualType.LinearGradient)
{
LinearGradientBrush brush = new LinearGradientBrush(Rectangle, BaseBackColor, GradualColor, Mode);
g.FillRectangle(brush, Rectangle);
}
else
{
Color[] surroundColor = new Color[] { BaseBackColor };
PathGradientBrush pb = new PathGradientBrush(path);
pb.CenterColor = GradualColor;
pb.SurroundColors = surroundColor;
pb.CenterPoint = new PointF(this.Width / 2, this.Height / 2);
g.FillPath(pb, path);
}
}
//this.Region = new Region(path);
if (ShowBorder)
{
Pen pen = null;
if (!ShowBorderGradual)
pen = new Pen(BorderColor);
else
{
LinearGradientBrush brush = new LinearGradientBrush(Rectangle, BorderColor, BorderGradualColor, LinearGradientMode.Vertical);
pen = new Pen(brush);
}
pen.Width = BorderWidth;
if (this.Style == RadiusStyle.None)
{
g.DrawRectangle(pen, Rectangle);
}
else
{
g.DrawPath(pen, path);
}
}
}
catch { }
}
private GraphicsPath CreatePath(Rectangle rectangle, int radius, RadiusStyle style)
{
try
{
int lr = 2 * radius;
GraphicsPath path = new GraphicsPath();
if (style == RadiusStyle.None)
{
path.AddLine(rectangle.X, rectangle.Y, rectangle.Right, rectangle.Y);
path.AddLine(rectangle.Right, rectangle.Y, rectangle.Right, rectangle.Bottom);
path.AddLine(rectangle.Right, rectangle.Bottom, rectangle.X, rectangle.Bottom);
path.AddLine(rectangle.X, rectangle.Bottom, rectangle.X, rectangle.Y);
}
else if (style == RadiusStyle.All)
{
//上
path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom - radius));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
//下
path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y + radius));
path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
}
else if (style == RadiusStyle.Left)
{
//上
path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom));
//下
path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y + radius));
path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
}
else if (style == RadiusStyle.Top)
{
//上
path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom));
//下
path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y + radius));
path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
}
else if (style == RadiusStyle.Right)
{
//上
path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom - radius));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
//下
path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y));
}
else if (style == RadiusStyle.Bottom)
{
//上
path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom - radius));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
//下
path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y));
}
else if (style == RadiusStyle.LeftTop)
{
//上
path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom));
//下
path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y + radius));
path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
}
else if (style == RadiusStyle.LeftBottom)
{
//上
path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom));
//下
path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y));
}
else if (style == RadiusStyle.RightTop)
{
//上
path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom));
//下
path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y));
}
else
{
//上
path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
//右
path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom - radius));
path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
//下
path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
//左
path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y));
}
return path;
}
catch
{
return null;
}
}
//导入API函数
[System.Runtime.InteropServices.DllImport("user32.dll ")]
static extern IntPtr GetWindowDC(IntPtr hWnd);//返回hWnd参数所指定的窗口的设备环境。
[System.Runtime.InteropServices.DllImport("user32.dll ")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); //函数释放设备上下文环境(DC
}