RaUI/Source/ryControls/Controls/PanelEx.cs
2020-11-28 15:03:57 +08:00

116 lines
3.2 KiB
C#

using ryControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ryControls
{
/// <summary>
///
/// </summary>
public partial class PanelEx : Panel
{
/// <summary>
///
/// </summary>
public PanelEx()
{
InitializeComponent();
this.BorderStyle = BorderStyle.None;
}
private int _radius = 10;
private RoundStyle _roundeStyle;
private Color _borderColor = SkinHelp.DefalutBorderColor;
/// <summary>
/// 控件边框颜色
/// </summary>
[DefaultValue(typeof(Color), "23, 169, 254"), Description("控件边框颜色")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
base.Invalidate();
}
}
/// <summary>
/// 控件背景颜色
/// </summary>
[DefaultValue(typeof(Color), "23, 169, 254"), Description("控件背景颜色")]
public Color TileBackColor
{
get; set;
} = Color.White;
/// <summary>
/// 圆角弧度大小
/// </summary>
[DefaultValue(typeof(int), "10"), Description("圆角弧度大小")]
public int Radius
{
get { return _radius; }
set
{
_radius = value;
base.Invalidate();
}
}
/// <summary>
/// 圆角风格
/// </summary>
public RoundStyle RoundeStyle
{
get { return _roundeStyle; }
set
{
_roundeStyle = value;
base.Invalidate();
}
}
private void PanelEx_Paint(object sender, PaintEventArgs e)
{
if (this.Radius > 0)
{
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
Rectangle r = new Rectangle
{
Width = this.Width,
Height = this.Height
};
DrawBorder(g, r, this.RoundeStyle, this.Radius);
}
}
else
e.Graphics.DrawRectangle(new Pen(this.BorderColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
}
private void DrawBorder(Graphics g, Rectangle rect, RoundStyle roundStyle, int radius)
{
rect.Width -= 1;
rect.Height -= 1;
using (GraphicsPath path = Drawing.CreatePath(rect, radius, roundStyle, false))
{
SolidBrush myBrush = new SolidBrush(TileBackColor);
using (Pen pen = new Pen(this.BorderColor))
{
g.FillPath(myBrush, path);
g.DrawPath(pen, path);
}
}
}
private void PanelEx_Resize(object sender, EventArgs e)
{
this.Refresh();
}
}
}