闲来无事,把一些自认为比较能够用的上的做一个记录,以备后用,也能在淡忘之际能够重新记忆。
主要是针对winform中的一些基本控件做一个样式更改,原因是自带的控件在某些方面达不到要求,其实就是美观的问题。
如下:
1、绘制圆角Button
/// <summary>
/// 圆角Button
/// </summary>
/// <param name="sender">buttonName</param>
/// <param name="p_1">10</param>
/// <param name="p_2">0.1</param>
public static void DrawButton(Control sender, int p_1, double p_2)
{
GraphicsPath oPath = new GraphicsPath();
oPath.AddClosedCurve(new Point[]{
new Point(0,sender.Height/p_1),
new Point(sender.Width/p_1,0),
new Point(sender.Width - sender.Height/p_1,0),
new Point(sender.Width,sender.Height/p_1),
new Point(sender.Width,sender.Height - sender.Height / p_1),
new Point(sender.Width - sender.Width / p_1,sender.Height),
new Point(sender.Width / p_1,sender.Height),
new Point(0,sender.Height - sender.Height / p_1)
},
(float)p_2);
sender.Region = new Region(oPath);
}
调用: Button btn = (Button)sender;
ControlDraw.DrawButton(btn, 10, 0.1);
2、绘制圆角窗体
/// <summary>
/// 圆角窗体
/// </summary>
/// <param name="rect"></param>
/// <param name="radius"></param>
/// <returns></returns>
public static GraphicsPath GetRoundedRecPath(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;
}
调用:窗体InstrumentStateFrm的Resize事件
private void InstrumentStateFrm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
GraphicsPath formPath = new GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
formPath = ControlDraw.GetRoundedRecPath(rect, 50);
this.Region = new Region(formPath);
}
else
{
this.Region = null;
}
}
3、绘制圆角Panel
/// <summary>
/// 圆角panel
/// </summary>
/// <param name="rectangle">panel1_Paint事件e.ClipRectangle</param>
/// <param name="g">e.Graphics</param>
/// <param name="_radius">圆角弧度50</param>
/// <param name="cusp">true = 右边突出尖角,false = 圆角Panel</param>
/// <param name="begin_color">颜色</param>
/// <param name="end_color">颜色</param>
public static void DrawPanle(Rectangle rectangle,Graphics g,int _radius,bool cusp,Color begin_color,Color end_color)
{
int span = 2;
g.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush myLinearGradientBrush = new LinearGr