Winform圆⾓按钮,冷家锋整理
⼀、先上效果图,Winform
⼆、声明
在下通过搜寻⽹络,补全了,并作了简单测试,原理我也说不清楚,在下典型的应⽤为王,能⽤就⾏,不到万不得已不去深究原理。
三、完整的控件代码,新建ButtonEx.cs的类,完全拷⼊即可。
//==代码开始=============================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace LJF
{
public enum ControlState
{
Normal,
Hover,
Pressed
}
public enum RoundStyle
{
None,
Top,
Bottom,
Left,
Right,
All
}
public class GraphicsPathHelper
{
#region 建⽴带有圆⾓样式的路径
/// <summary>
/// 建⽴带有圆⾓样式的路径
/// 建⽴带有圆⾓样式的路径
/// </summary>
/// <param name="rect">⽤来建⽴路径的矩形</param>
/// <param name="radius">圆⾓半径</param>
/// <param name="style"></param>
/
// <param name="correction"></param>
/// <returns></returns>
public static GraphicsPath CreatePath(
Rectangle rect, int radius, RoundStyle style, bool correction) {
GraphicsPath path = new GraphicsPath();
int radiusCorrection = correction ? 1 : 0;
switch (style)
{
case RoundStyle.None:
path.AddRectangle(rect);
break;
case RoundStyle.All:
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(
rect.Right - radius - radiusCorrection,
rect.Y,
radius,
radius,
270,
90);
path.AddArc(
rect.Right - radius - radiusCorrection,
rect.Bottom - radius - radiusCorrection,
radius,
radius, 0, 90);
path.AddArc(
rect.X,
rect.Bottom - radius - radiusCorrection,
radius,
radius,
90,
90);
break;
case RoundStyle.Left:
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddLine(
rect.Right - radiusCorrection, rect.Y,
rect.Right - radiusCorrection, rect.Bottom - radiusCorrection); path.AddArc(
rect.X,
rect.Bottom - radius - radiusCorrection,
radius,
radius,
90,
90);
break;
case RoundStyle.Right:
path.AddArc(
rect.Right - radius - radiusCorrection,
rect.Y,
rect.Y,
radius,
radius,
270,
90);
htmlborderpath.AddArc(
rect.Right - radius - radiusCorrection,
rect.Bottom - radius - radiusCorrection,
radius,
radius,
0,
90);
path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y); break;
case RoundStyle.Top:
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(
rect.Right - radius - radiusCorrection,
rect.Y,
radius,
radius,
270,
90);
path.AddLine(
rect.Right - radiusCorrection, rect.Bottom - radiusCorrection,
rect.X, rect.Bottom - radiusCorrection);
break;
case RoundStyle.Bottom:
path.AddArc(
rect.Right - radius - radiusCorrection,
rect.Bottom - radius - radiusCorrection,
radius,
radius,
0,
90);
path.AddArc(
rect.X,
rect.Bottom - radius - radiusCorrection,
radius,
radius,
90,
90);
path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y); break;
}
path.CloseFigure();
return path;
}
#endregion
}
public class ButtonEx : Button
{
private Color _baseColor = Color.FromArgb(51, 161, 224);//基颜⾊
private ControlState _controlState;//控件状态
private int _imageWidth = 18;
private RoundStyle _roundStyle = RoundStyle.All;//圆⾓
private int _radius = 8; //圆⾓半径
public ButtonEx(): base()
{
this.SetStyle(
ControlStyles.UserPaint | //控件⾃⾏绘制,⽽不使⽤操作系统的绘制
ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息,减少闪烁。
ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。ControlStyles.ResizeRedraw | //控件⼤⼩发⽣变化时,重绘。
ControlStyles.SupportsTransparentBackColor, true);//⽀持透明背景颜⾊
}
#region 属性
[DefaultValue(typeof(Color), "51, 161, 224")]
public Color BaseColor
{
get { return _baseColor; }
set
{
_baseColor = value;
base.Invalidate();
}
}
[DefaultValue(18)]//默认值为18px,最⼩12px
public int ImageWidth
{
get { return _imageWidth; }
set
{
if (value != _imageWidth)
{
_imageWidth = value < 12 ? 12 : value;
base.Invalidate();
}
}
}
[DefaultValue(typeof(RoundStyle), "1")]//默认全部都是圆⾓
public RoundStyle RoundStyle
{
get { return _roundStyle; }
set
{
if (_roundStyle != value)
{
_roundStyle = value;
base.Invalidate();
}
}
}
[DefaultValue(8)]//设置圆⾓半径,默认值为8,最⼩值为4px
[DefaultValue(8)]//设置圆⾓半径,默认值为8,最⼩值为4px public int Radius
{
get { return _radius; }
set
{
if (_radius != value)
{
_radius = value < 4 ? 4 : value;
base.Invalidate();
}
}
}
public ControlState ControlState //控件的状态
{
get { return _controlState; }
set
{
if (_controlState != value)
{
_controlState = value;
base.Invalidate();
}
}
}
#endregion

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。