标签:des style class blog code tar
闲来无事,从网上找了不少自定义控件,然后整理了一下,做了一个水晶按钮
///
/// 表示 Windows 的按钮控
///
[Description("表示 Windows 的按钮控件"), DefaultEvent("Click"), ToolboxBitmap(typeof (System.Windows.Forms.Button))]
public class Button : Control
{
public Button()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.Selectable, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.UserPaint, true);
_fadeIn.Interval = 30;
_fadeOut.Interval = 30;
_fadeIn.Tick += FadeIn_Tick;
_fadeOut.Tick += FadeOut_Tick;
}
///
/// 淡出
///
private void FadeOut_Tick(object sender, EventArgs e)
{
if (ButtonStyle == Style.Flat)
{
_glowAlpha = 0;
}
if (_glowAlpha - 30 0)
{
_glowAlpha = 0;
_fadeOut.Stop();
}
else
{
_glowAlpha -= 30;
}
Invalidate();
}
///
/// 淡入
///
private void FadeIn_Tick(object sender, EventArgs e)
{
if (ButtonStyle == Style.Flat)
{
_glowAlpha = 0;
}
if (_glowAlpha + 30 >= 255)
{
_glowAlpha = 255;
_fadeIn.Stop();
}
else
{
_glowAlpha += 30;
}
Invalidate();
}
private readonly Color[] _colorArray =
{
Color.White, Color.FromArgb(172, 168, 153), Color.White,
Color.FromArgb(236, 233, 216)
};
private float _radius = 3;
private Style _mButtonStyle = Style.Default;
private State _mButtonState = State.None;
private readonly Timer _fadeIn = new Timer();
private readonly Timer _fadeOut = new Timer();
private int _glowAlpha;
private ContentAlignment _mTextAlign = ContentAlignment.MiddleCenter;
private ContentAlignment _mImageAlign = ContentAlignment.BottomCenter;
private Size _mImageSize = new Size(24, 24);
private Image _mImage;
private Image _backImage;
///
/// 高亮颜色
///
[Category("Appearance"), Description("高亮颜色"), DefaultValue(typeof(Color), "White"), Browsable(true)]
public Color LightColor
{
set
{
_colorArray[0] = value;
Invalidate();
}
get { return _colorArray[0]; }
}
///
/// 底色
///
[Category("Appearance"), Description("底色"), DefaultValue(typeof(Color), "172, 168, 153"), Browsable(true)]
public Color PrimaryColor
{
set
{
_colorArray[1] = value;
Invalidate();
}
get { return _colorArray[1]; }
}
///
/// 亮点颜色
///
[Category("Appearance"), Description("亮点颜色"), DefaultValue(typeof(Color), "White"), Browsable(true)]
public Color GlowColor
{
set
{
_colorArray[2] = value;
Invalidate();
}
get { return _colorArray[2]; }
}
///
/// 基本色
///
[Category("Appearance"), Description("基本色"), DefaultValue(typeof(Color), "236, 233, 216"), Browsable(true)]
public Color BaseColor
{
set
{
_colorArray[3] = value;
Invalidate();
}
get { return _colorArray[3]; }
}
///
/// 角的度数
///
[Category("Appearance"), Description("角的度数"), DefaultValue(typeof (float), "8"), Browsable(true)]
public float CornerRadius
{
set
{
_radius = value;
Invalidate();
}
get { return _radius; }
}
///
/// 角的度数
///
[Category("Appearance"), Description("角的度数"), DefaultValue(typeof (Style), "Default"), Browsable(true)]
public Style ButtonStyle
{
get { return _mButtonStyle; }
set
{
_mButtonStyle = value;
Invalidate();
}
}
///
/// 按钮文本的对其方式
///
[Category("Text"), Description("按钮文本的对其方式"), DefaultValue(typeof (Style), "MiddleCenter"), Browsable(true)]
public ContentAlignment TextAlign
{
get { return _mTextAlign; }
set
{
_mTextAlign = value;
Invalidate();
}
}
///
/// 图片的对齐方式
///
[Category("图像"), Description("图片的对齐方式"), DefaultValue(typeof (ContentAlignment), "MiddleLeft"),
Browsable(true)]
public ContentAlignment ImageAlign
{
set
{
_mImageAlign = value;
Invalidate();
}
get { return _mImageAlign; }
}
///
/// 图片的高度和宽度
///
[Category("图像"), Description("图片的高度和宽度"), DefaultValue(typeof(ContentAlignment), "24,24"), Browsable(true)]
public Size ImageSize
{
set
{
_mImageSize = value;
Invalidate();
}
get { return _mImageSize; }
}
///
/// 图片
///
[Category("图像"), Description("图片"), DefaultValue(null), Browsable(true)]
public Image Image
{
set
{
_mImage = value;
Invalidate();
}
get { return _mImage; }
}
[Category("Appearance"), Description("背景图"), DefaultValue(null), Browsable(true)]
public Image BackImage
{
set
{
_backImage = value;
Invalidate();
}
get { return _backImage; }
}
///
/// 获取绘制区域路径
///
/// 区域
/// 角1度数
/// 角2度数
/// 角3度数
/// 角4度数
///
private GraphicsPath RoundRect(RectangleF r, float r1, float r2, float r3, float r4)
{
float x = r.X, y = r.Y, w = r.Width, h = r.Height;
var gp = new GraphicsPath();
gp.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y);
gp.AddLine(x + r1, y, x + w - r2, y);
gp.AddBezier(x + w - r2, y, x + w, y, x + w, y + r2, x + w, y + r2);
gp.AddLine(x + w, y + r2, x + w, y + h - r3);
gp.AddBezier(x + w, y + h - r3, x + w, y + h, x + w - r3, y + h, x + w - r3, y + h);
gp.AddLine(x + w - r3, y + h, x + r4, y + h);
gp.AddBezier(x + r4, y + h, x, y + h, x, y + h - r4, x, y + h - r4);
gp.AddLine(x, y + h - r4, x, y + r1);
return gp;
}
///
/// 获取绘制文本的方式
///
///
private StringFormat StringFormatAlignment()
{
var sf = new StringFormat();
switch (TextAlign)
{
case ContentAlignment.TopLeft:
case ContentAlignment.TopCenter:
case ContentAlignment.TopRight:
sf.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.MiddleLeft:
case ContentAlignment.MiddleCenter:
case ContentAlignment.MiddleRight:
sf.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.BottomLeft:
case ContentAlignment.BottomCenter:
case ContentAlignment.BottomRight:
sf.LineAlignment = StringAlignment.Far;
break;
}
switch (TextAlign)
{
case ContentAlignment.TopLeft:
case ContentAlignment.MiddleLeft:
case ContentAlignment.BottomLeft:
sf.Alignment = StringAlignment.Near;
break;
case ContentAlignment.TopCenter:
case ContentAlignment.MiddleCenter:
case ContentAlignment.BottomCenter:
sf.Alignment = StringAlignment.Center;
break;
case ContentAlignment.TopRight:
case ContentAlignment.MiddleRight:
case ContentAlignment.BottomRight:
sf.Alignment = StringAlignment.Far;
break;
}
return sf;
}
///
/// 绘制外部
///
///
private void DrawOuterStroke(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
Rectangle r = ClientRectangle;
r.Width -= 1;
r.Height -= 1;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
using (var p = new Pen(PrimaryColor))
{
g.DrawPath(p, rr);
}
}
}
///
/// 绘制内部
///
///
private void DrawInnerStroke(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
Rectangle r = ClientRectangle;
r.X++;
r.Y++;
r.Width -= 3;
r.Height -= 3;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
using (var p = new Pen(LightColor))
{
g.DrawPath(p, rr);
}
}
}
///
/// 绘制背景
///
///
private void DrawBackground(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
int alpha = (_mButtonState == State.Pressed) ? 204 : 127;
Rectangle r = ClientRectangle;
r.Width--;
r.Height--;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
using (var sb = new SolidBrush(BaseColor))
{
g.FillPath(sb, rr);
}
SetClip(g);
if (BackImage != null)
{
g.DrawImage(BackImage, ClientRectangle);
}
g.ResetClip();
using (var sb = new SolidBrush(Color.FromArgb(alpha, PrimaryColor)))
{
g.FillPath(sb, rr);
}
}
}
///
/// 绘制高亮
///
///
private void DrawHighlight(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
int alpha = (_mButtonState == State.Pressed) ? 60 : 150;
var rect = new Rectangle(0, 0, Width, Height/2);
using (GraphicsPath r = RoundRect(rect, CornerRadius, CornerRadius, 0, 0))
{
using (var lg = new LinearGradientBrush(r.GetBounds(),
Color.FromArgb(alpha, LightColor),
Color.FromArgb(alpha/3, LightColor),
LinearGradientMode.Vertical))
{
g.FillPath(lg, r);
}
}
}
///
/// 绘制亮点
///
///
private void DrawGlow(Graphics g)
{
if (_mButtonState == State.Pressed)
{
return;
}
SetClip(g);
using (var glow = new GraphicsPath())
{
glow.AddEllipse(-5, Height/2 - 10, Width + 11, Height + 11);
using (var gl = new PathGradientBrush(glow))
{
gl.CenterColor = Color.FromArgb(_glowAlpha, GlowColor);
gl.SurroundColors = new[] {Color.FromArgb(0, GlowColor)};
g.FillPath(gl, glow);
}
}
g.ResetClip();
}
///
/// 绘制剪辑区域
///
///
private void SetClip(Graphics g)
{
Rectangle r = ClientRectangle;
r.X++;
r.Y++;
r.Width -= 3;
r.Height -= 3;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
g.SetClip(rr);
}
}
///
/// 绘制文本
///
///
private void DrawText(Graphics g)
{
StringFormat sf = StringFormatAlignment();
var s = g.MeasureString(Text, Font);
var x = (int) ((Width - s.Width - 1)/2);
var y = (int) ((Height - s.Height - 1)/3*2);
var r = new Rectangle(x, y, (int) s.Width + 1, (int) s.Height + 1);
g.DrawString(Text, Font, new SolidBrush(ForeColor), r, sf);
}
///
/// 绘制图片
///
///
private void DrawImage(Graphics g)
{
if (Image == null)
{
return;
}
var r = new Rectangle(8, 8, ImageSize.Width, ImageSize.Height);
switch (ImageAlign)
{
case ContentAlignment.TopCenter:
r = new Rectangle(Width/2 - ImageSize.Width/2, 8, ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.TopRight:
r = new Rectangle(Width - 8 - ImageSize.Width, 8, ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.MiddleLeft:
r = new Rectangle(8, Height/2 - ImageSize.Height/2, ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.MiddleCenter:
r = new Rectangle(Width/2 - ImageSize.Width/2, Height/2 - ImageSize.Height/2, ImageSize.Width,
ImageSize.Height);
break;
case ContentAlignment.MiddleRight:
r = new Rectangle(Width - 8 - ImageSize.Width, Height/2 - ImageSize.Height/2, ImageSize.Width,
ImageSize.Height);
break;
case ContentAlignment.BottomLeft:
r = new Rectangle(8, Height - 8 - ImageSize.Height, ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.BottomCenter:
r = new Rectangle(Width/2 - ImageSize.Width/2, Height - 8 - ImageSize.Height, ImageSize.Width,
ImageSize.Height);
break;
case ContentAlignment.BottomRight:
r = new Rectangle(Width - 8 - ImageSize.Width, Height - 8 - ImageSize.Height, ImageSize.Width,
ImageSize.Height);
break;
}
g.DrawImage(Image, r);
}
///
/// 重绘按钮
///
///
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
pevent.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
DrawBackground(pevent.Graphics);
DrawHighlight(pevent.Graphics);
DrawImage(pevent.Graphics);
DrawText(pevent.Graphics);
DrawGlow(pevent.Graphics);
DrawOuterStroke(pevent.Graphics);
DrawInnerStroke(pevent.Graphics);
}
///
/// 鼠标移入
///
///
protected override void OnMouseEnter(EventArgs e)
{
_mButtonState = State.Hover;
_fadeOut.Stop();
_fadeIn.Start();
base.OnMouseEnter(e);
}
///
/// 鼠标移出
///
///
protected override void OnMouseLeave(EventArgs e)
{
_mButtonState = State.None;
if (_mButtonStyle == Style.Flat)
{
_glowAlpha = 0;
}
_fadeIn.Stop();
_fadeOut.Start();
base.OnMouseLeave(e);
}
///
/// 鼠标按下
///
///
protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_mButtonState = State.Pressed;
if (_mButtonStyle != Style.Flat)
{
_glowAlpha = 255;
}
_fadeIn.Stop();
_fadeOut.Stop();
Invalidate();
}
base.OnMouseDown(mevent);
}
///
/// 鼠标弹起
///
///
protected override void OnMouseUp(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_mButtonState = State.Hover;
_fadeIn.Stop();
_fadeOut.Stop();
Invalidate();
}
base.OnMouseUp(mevent);
}
///
/// 按钮变化
///
///
protected override void OnResize(EventArgs e)
{
Rectangle r = ClientRectangle;
r.X -= 1;
r.Y -= 1;
r.Width += 2;
r.Height += 2;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
Region = new Region(rr);
}
base.OnResize(e);
}
///
/// 鼠标活动类别
///
public enum State
{
///
/// 正常状态
///
None,
///
/// 鼠标悬浮状态
///
Hover,
///
/// 鼠标按下
///
Pressed
}
///
/// 绘制样式
///
public enum Style
{
///
/// 只画背景的鼠标
///
Default,
///
/// 绘制按钮作为正常
///
Flat
};
}
winfrom 水晶按钮,搜素材,soscw.com
winfrom 水晶按钮
标签:des style class blog code tar
原文地址:http://www.cnblogs.com/rogation/p/3796875.html