闲来无事,从网上找了不少自定义控件,然后整理了一下,做了一个水晶按钮

    /// <summary>
/// 表示 Windows 的按钮控
/// </summary>
[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 = ;
_fadeOut.Interval = ;
_fadeIn.Tick += FadeIn_Tick;
_fadeOut.Tick += FadeOut_Tick;
} /// <summary>
/// 淡出
/// </summary>
private void FadeOut_Tick(object sender, EventArgs e)
{
if (ButtonStyle == Style.Flat)
{
_glowAlpha = ;
}
if (_glowAlpha - <= )
{
_glowAlpha = ;
_fadeOut.Stop();
}
else
{
_glowAlpha -= ;
}
Invalidate();
} /// <summary>
/// 淡入
/// </summary>
private void FadeIn_Tick(object sender, EventArgs e)
{
if (ButtonStyle == Style.Flat)
{
_glowAlpha = ;
}
if (_glowAlpha + >= )
{
_glowAlpha = ;
_fadeIn.Stop();
}
else
{
_glowAlpha += ;
}
Invalidate();
} private readonly Color[] _colorArray =
{
Color.White, Color.FromArgb(, , ), Color.White,
Color.FromArgb(, , )
}; private float _radius = ;
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(, );
private Image _mImage;
private Image _backImage; /// <summary>
/// 高亮颜色
/// </summary>
[Category("Appearance"), Description("高亮颜色"), DefaultValue(typeof(Color), "White"), Browsable(true)]
public Color LightColor
{
set
{
_colorArray[] = value;
Invalidate();
}
get { return _colorArray[]; }
} /// <summary>
/// 底色
/// </summary>
[Category("Appearance"), Description("底色"), DefaultValue(typeof(Color), "172, 168, 153"), Browsable(true)]
public Color PrimaryColor
{
set
{
_colorArray[] = value;
Invalidate();
}
get { return _colorArray[]; }
} /// <summary>
/// 亮点颜色
/// </summary>
[Category("Appearance"), Description("亮点颜色"), DefaultValue(typeof(Color), "White"), Browsable(true)]
public Color GlowColor
{
set
{
_colorArray[] = value;
Invalidate();
}
get { return _colorArray[]; }
} /// <summary>
/// 基本色
/// </summary>
[Category("Appearance"), Description("基本色"), DefaultValue(typeof(Color), "236, 233, 216"), Browsable(true)]
public Color BaseColor
{
set
{
_colorArray[] = value;
Invalidate();
}
get { return _colorArray[]; }
} /// <summary>
/// 角的度数
/// </summary>
[Category("Appearance"), Description("角的度数"), DefaultValue(typeof (float), ""), Browsable(true)]
public float CornerRadius
{
set
{
_radius = value;
Invalidate();
}
get { return _radius; }
} /// <summary>
/// 角的度数
/// </summary>
[Category("Appearance"), Description("角的度数"), DefaultValue(typeof (Style), "Default"), Browsable(true)]
public Style ButtonStyle
{
get { return _mButtonStyle; }
set
{
_mButtonStyle = value;
Invalidate();
}
} /// <summary>
/// 按钮文本的对其方式
/// </summary>
[Category("Text"), Description("按钮文本的对其方式"), DefaultValue(typeof (Style), "MiddleCenter"), Browsable(true)]
public ContentAlignment TextAlign
{
get { return _mTextAlign; }
set
{
_mTextAlign = value;
Invalidate();
}
} /// <summary>
/// 图片的对齐方式
/// </summary>
[Category("图像"), Description("图片的对齐方式"), DefaultValue(typeof (ContentAlignment), "MiddleLeft"),
Browsable(true)]
public ContentAlignment ImageAlign
{
set
{
_mImageAlign = value;
Invalidate();
}
get { return _mImageAlign; }
} /// <summary>
/// 图片的高度和宽度
/// </summary>
[Category("图像"), Description("图片的高度和宽度"), DefaultValue(typeof(ContentAlignment), "24,24"), Browsable(true)]
public Size ImageSize
{
set
{
_mImageSize = value;
Invalidate();
}
get { return _mImageSize; }
} /// <summary>
/// 图片
/// </summary>
[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; }
} /// <summary>
/// 获取绘制区域路径
/// </summary>
/// <param name="r">区域</param>
/// <param name="r1">角1度数</param>
/// <param name="r2">角2度数</param>
/// <param name="r3">角3度数</param>
/// <param name="r4">角4度数</param>
/// <returns></returns>
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;
} /// <summary>
/// 获取绘制文本的方式
/// </summary>
/// <returns></returns>
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;
} /// <summary>
/// 绘制外部
/// </summary>
/// <param name="g"></param>
private void DrawOuterStroke(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
Rectangle r = ClientRectangle;
r.Width -= ;
r.Height -= ;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
using (var p = new Pen(PrimaryColor))
{
g.DrawPath(p, rr);
}
}
} /// <summary>
/// 绘制内部
/// </summary>
/// <param name="g"></param>
private void DrawInnerStroke(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
Rectangle r = ClientRectangle;
r.X++;
r.Y++;
r.Width -= ;
r.Height -= ;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
using (var p = new Pen(LightColor))
{
g.DrawPath(p, rr);
}
}
} /// <summary>
/// 绘制背景
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
int alpha = (_mButtonState == State.Pressed) ? : ;
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);
}
}
} /// <summary>
/// 绘制高亮
/// </summary>
/// <param name="g"></param>
private void DrawHighlight(Graphics g)
{
if (ButtonStyle == Style.Flat && _mButtonState == State.None)
{
return;
}
int alpha = (_mButtonState == State.Pressed) ? : ;
var rect = new Rectangle(, , Width, Height/);
using (GraphicsPath r = RoundRect(rect, CornerRadius, CornerRadius, , ))
{
using (var lg = new LinearGradientBrush(r.GetBounds(),
Color.FromArgb(alpha, LightColor),
Color.FromArgb(alpha/, LightColor),
LinearGradientMode.Vertical))
{
g.FillPath(lg, r);
}
}
} /// <summary>
/// 绘制亮点
/// </summary>
/// <param name="g"></param>
private void DrawGlow(Graphics g)
{
if (_mButtonState == State.Pressed)
{
return;
}
SetClip(g);
using (var glow = new GraphicsPath())
{
glow.AddEllipse(-, Height/ - , Width + , Height + );
using (var gl = new PathGradientBrush(glow))
{
gl.CenterColor = Color.FromArgb(_glowAlpha, GlowColor);
gl.SurroundColors = new[] {Color.FromArgb(, GlowColor)};
g.FillPath(gl, glow);
}
}
g.ResetClip();
} /// <summary>
/// 绘制剪辑区域
/// </summary>
/// <param name="g"></param>
private void SetClip(Graphics g)
{
Rectangle r = ClientRectangle;
r.X++;
r.Y++;
r.Width -= ;
r.Height -= ;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
g.SetClip(rr);
}
} /// <summary>
/// 绘制文本
/// </summary>
/// <param name="g"></param>
private void DrawText(Graphics g)
{
StringFormat sf = StringFormatAlignment();
var s = g.MeasureString(Text, Font);
var x = (int) ((Width - s.Width - )/);
var y = (int) ((Height - s.Height - )/*);
var r = new Rectangle(x, y, (int) s.Width + , (int) s.Height + );
g.DrawString(Text, Font, new SolidBrush(ForeColor), r, sf);
} /// <summary>
/// 绘制图片
/// </summary>
/// <param name="g"></param>
private void DrawImage(Graphics g)
{
if (Image == null)
{
return;
}
var r = new Rectangle(, , ImageSize.Width, ImageSize.Height);
switch (ImageAlign)
{
case ContentAlignment.TopCenter:
r = new Rectangle(Width/ - ImageSize.Width/, , ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.TopRight:
r = new Rectangle(Width - - ImageSize.Width, , ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.MiddleLeft:
r = new Rectangle(, Height/ - ImageSize.Height/, ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.MiddleCenter:
r = new Rectangle(Width/ - ImageSize.Width/, Height/ - ImageSize.Height/, ImageSize.Width,
ImageSize.Height);
break;
case ContentAlignment.MiddleRight:
r = new Rectangle(Width - - ImageSize.Width, Height/ - ImageSize.Height/, ImageSize.Width,
ImageSize.Height);
break;
case ContentAlignment.BottomLeft:
r = new Rectangle(, Height - - ImageSize.Height, ImageSize.Width, ImageSize.Height);
break;
case ContentAlignment.BottomCenter:
r = new Rectangle(Width/ - ImageSize.Width/, Height - - ImageSize.Height, ImageSize.Width,
ImageSize.Height);
break;
case ContentAlignment.BottomRight:
r = new Rectangle(Width - - ImageSize.Width, Height - - ImageSize.Height, ImageSize.Width,
ImageSize.Height);
break;
}
g.DrawImage(Image, r);
} /// <summary>
/// 重绘按钮
/// </summary>
/// <param name="pevent"></param>
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);
} /// <summary>
/// 鼠标移入
/// </summary>
/// <param name="e"></param>
protected override void OnMouseEnter(EventArgs e)
{
_mButtonState = State.Hover;
_fadeOut.Stop();
_fadeIn.Start();
base.OnMouseEnter(e);
} /// <summary>
/// 鼠标移出
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
_mButtonState = State.None;
if (_mButtonStyle == Style.Flat)
{
_glowAlpha = ;
}
_fadeIn.Stop();
_fadeOut.Start();
base.OnMouseLeave(e);
} /// <summary>
/// 鼠标按下
/// </summary>
/// <param name="mevent"></param>
protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_mButtonState = State.Pressed;
if (_mButtonStyle != Style.Flat)
{
_glowAlpha = ;
}
_fadeIn.Stop();
_fadeOut.Stop();
Invalidate();
}
base.OnMouseDown(mevent);
} /// <summary>
/// 鼠标弹起
/// </summary>
/// <param name="mevent"></param>
protected override void OnMouseUp(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_mButtonState = State.Hover;
_fadeIn.Stop();
_fadeOut.Stop();
Invalidate();
}
base.OnMouseUp(mevent);
} /// <summary>
/// 按钮变化
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
Rectangle r = ClientRectangle;
r.X -= ;
r.Y -= ;
r.Width += ;
r.Height += ;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
Region = new Region(rr);
}
base.OnResize(e);
} /// <summary>
/// 鼠标活动类别
/// </summary>
public enum State
{
/// <summary>
/// 正常状态
/// </summary>
None,
/// <summary>
/// 鼠标悬浮状态
/// </summary>
Hover,
/// <summary>
/// 鼠标按下
/// </summary>
Pressed
} /// <summary>
/// 绘制样式
/// </summary>
public enum Style
{
/// <summary>
/// 只画背景的鼠标
/// </summary>
Default,
/// <summary>
/// 绘制按钮作为正常
/// </summary>
Flat
};
}

winfrom 水晶按钮的更多相关文章

  1. C#制作高仿360安全卫士窗体(四)- 水晶按钮

    项目越来越紧,我也乐此不疲.自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西.我想说的是,我是从事企业信息化工作的,所有程序都只对内部使用.所以只 ...

  2. winfrom中按钮文本&的显示问题/按钮快捷键设置问题

    其实这个问题是因为“&”有特殊的意义-就是可以作为快捷键 第一种:Alt + *(按钮快捷键) 在大家给button.label.menuStrip等控件设置Text属性时在名字后边加& ...

  3. 自绘实现半透明水晶按钮(继承CButton,设置BS_OWNERDRAW风格,覆盖DrawItem函数绘制按钮,把父窗口的背景复制到按钮上,实现视觉上的透明,最后通过AlphaBlend实现半透明)

    运行效果 实现方法 1.给按钮加上BS_OWNERDRAW样式2.重载DrawItem函数,在这里绘制按钮3.关键之处就是把父窗口的背景复制到按钮上,实现视觉上的透明4.最后通过AlphaBlend实 ...

  4. css3 实现水晶按钮

    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ee432e), color-sto ...

  5. C# 时间控件 竖直进度条 饼图显示 仪表盘 按钮基础控件库

    Prepare 本文将使用一个NuGet公开的组件来实现一些特殊的控件显示,方便大家进行快速的开发系统. 在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台 ...

  6. 198个经典C#WinForm实例源码(超赞) 里面的例子 .sln 目录

    \-窗体技巧\QQ窗体\QQFrm.sln; \-窗体技巧\仿XP系统的任务栏菜单\仿XP系统的任务栏菜单.sln; \-窗体技巧\向窗体中拖放图片并显示\向窗体中拖放图片并显示.sln; \-窗体技 ...

  7. 用scrollTop制作一个自动滚动公告栏

    我们在浏览网页时,经常会看到会一些滚动的栏目,比如向上滚动的公告.新闻等.其实他们的制作都不难,只要学了基础的html.css.javascript就可以做出来,用JavaScript的scrollT ...

  8. WinForm GDI+ 资料收集

    UI(User Interface)编程在整个项目开发过程中是个颇为重要的环节,任何好的解决方案若没有良好的用户界面呈现给最终用户,那么就算包含了最先进的技术也不能算是好程序.UI编程体现在两个方面, ...

  9. 二、JavaScript语言--JS实践--信息滚动效果制作

    运用JavaScript技术,掌握无缝滚动和歇间性滚动的制作方法. 一.marquee标签实现信息滚动 1 behavior滚动的方式 alternate:表示在两端之间来回滚动 scroll:表示由 ...

随机推荐

  1. xtrabackup原理1

    http://www.cnblogs.com/Amaranthus/archive/2014/08/19/3922570.html Percona XtraBackup User Manual 阅读笔 ...

  2. TFS服务器(微软源代码管理服务器)上彻底删除项目

    在TFS服务器上建立了很多项目,发现在Team Explorer中,只能移除团队项目,这种移除,只是将项目从当前Team Explorer项目列表中删除,下一次Connect到TFS服务器时,或者刷新 ...

  3. 如何用jquery操作table的方法

    今天我在做你约我吧交友www.niyuewo.com网项目时遇到一个问题,就是如何用qjuery控制table的添加.编辑与删除,经过网上查资料发现用jquery很容易实现,在此整理下来供大家参考: ...

  4. RTB实时竞价广告是未来趋势

    原文:http://www.inboundjournals.com/rtb-real-time-bidding-the-future-of-online-advertising/ [资讯图表] RTB ...

  5. C#_自动化测试1_模拟post,get_12306火车票网站自动登录工具

    还记得2011年春运,12306火车票预订网站经常崩溃无法登录吗. 今天我们就开发一个12306网站自动登录软件. 帮助您轻松订票 通过前两篇博客Fiddler教程和HTTP协议详解,我们了解了Web ...

  6. mysql索引详解,摘自《MySQL 5权威指南》

    本文介绍了数据库索引,及其优.缺点.针对MySQL索引的特点.应用进行了详细的描述.分析了如何避免MySQL无法使用,如何使用EXPLAIN分析查询语句,如何优化MySQL索引的应用.本文摘自< ...

  7. 炼数成金hadoop视频干货04

    视频地址:http://pan.baidu.com/s/1dDEgKwD 这一节讲的全是理论 任务执行优化 : 1.推测式执行: 2.重用JVM: 3.忽略模式. 除了手动修改Log4J.proper ...

  8. iOS之可拖拽重排的CollectionView

    修复了拖拽滚动时抖动的一个bug,新增编辑模式,进入编辑模式后不用长按触发手势,且在开启抖动的情况下会自动进入抖动模式,如图: test.gif 图1:垂直滚动 drag1.gif 图2:水平滚动 d ...

  9. Android AdapterView 源码分析以及其相关回收机制的分析

    忽然,发现,网上的公开资料都是教你怎么继承一个baseadapter,然后重写那几个方法,再调用相关view的 setAdpater()方法, 接着,你的item 就显示在手机屏幕上了.很少有人关注a ...

  10. shell(1)

    bash变量类别: 本地变量 : 又叫局部变量,仅对当前shell进程有效 环境变量 : 当前shell及其子shell,子子shell-. 特殊变量 : $?  上一个命令执行的状态,0表示执行成功 ...