实例介绍
【实例简介】
【实例截图】
【核心代码】
namespace GlassButton { #region 控件状态 /// <summary> /// 控件状态 /// </summary> public enum State { /// <summary> /// 无 /// </summary> Normal = 0, /// <summary> /// 获得焦点 /// </summary> Focused = 1, /// <summary> /// 失去焦点 /// </summary> LostFocused = 2, /// <summary> /// 鼠标指针进入控件 /// </summary> MouseEnter = 3 } #endregion public class GlassButton : Control { #region 私有变量 private int bmp_Left; private const int bmp_Top = 5; private const int bmp_Size = 45; private bool _focused = false; private State state = State.Normal; private Bitmap _bmp = null; #endregion #region 构造函数 public GlassButton() { SetStyle(ControlStyles.DoubleBuffer, true); //双缓冲防止重绘时闪烁 SetStyle(ControlStyles.AllPaintingInWmPaint, true); //忽略 WM_ERASEBKGND 窗口消息减少闪烁 SetStyle(ControlStyles.UserPaint, true); //自定义绘制控件内容 SetStyle(ControlStyles.SupportsTransparentBackColor, true); //模拟透明 SetStyle(ControlStyles.Selectable, true); //接收焦点 Size = new Size(73, 81); //初始大小 Font = new Font("微软雅黑", 9); //控件字体 } #endregion #region 属性 /// <summary> /// 获取或设置控件显示的图片 /// </summary> [Description("获取或设置控件显示的图标")] public Bitmap Bitmap { get { return _bmp; } set { _bmp = value; Invalidate(false); } } /// <summary> /// 重写控件焦点属性 /// </summary> [Description("重写控件焦点属性")] public new bool Focused { get { return _focused; } set { _focused = value; if (_focused) state = State.Focused; else state = State.LostFocused; Invalidate(false); } } #endregion #region 重载事件 //自定义绘制 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; switch (state) { case State.Focused: { DrawSelected(g); break; } case State.MouseEnter: { if (!Focused) g.DrawImage(Properties.Resources.enter, ClientRectangle); else DrawSelected(g); break; } } DrawImage(g); DrawText(g); } //焦点进入 protected override void OnEnter(EventArgs e) { base.OnEnter(e); Focused = true; } //失去焦点 protected override void OnLeave(EventArgs e) { base.OnLeave(e); Focused = false; } //禁止调整大小 protected override void OnResize(EventArgs e) { Width = 73; Height = 81; } protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); state = State.MouseEnter; Invalidate(false); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); if (!Focused) { state = State.LostFocused; Invalidate(false); } } //只响应单击鼠标左键事件 protected override void OnClick(EventArgs e) { MouseEventArgs m = (MouseEventArgs)e; if (m.Button == MouseButtons.Left) { base.OnClick(e); Focus(); } } #endregion #region 方法 #region Draw void DrawSelected(Graphics g) { g.DrawImage(Properties.Resources.down, ClientRectangle); } void DrawImage(Graphics g) { if (_bmp != null) { Bitmap bmp = ScaleZoom(_bmp); bmp_Left = (Width - bmp.Width) / 2; g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height)); } } void DrawText(Graphics g) { SizeF size = g.MeasureString(Text, Font); g.DrawString(Text, Font, new SolidBrush(ForeColor), (Width - size.Width) / 2, 58); } #endregion #region 按比例缩放图片 public Bitmap ScaleZoom(Bitmap bmp) { if (bmp != null) { double zoomScale; if (bmp.Width > bmp_Size || bmp.Height > bmp_Size) { double imageScale = (double)bmp.Width / (double)bmp.Height; double thisScale = 1; if (imageScale > thisScale) { zoomScale = (double)bmp_Size / (double)bmp.Width; return BitMapZoom(bmp, bmp_Size, (int)(bmp.Height * zoomScale)); } else { zoomScale = (double)bmp_Size / (double)bmp.Height; return BitMapZoom(bmp, (int)(bmp.Width * zoomScale), bmp_Size); } } } return bmp; } #endregion #region 缩放BitMap /// <summary> /// 图片缩放 /// </summary> /// <param name="bmpSource">源图片</param> /// <param name="bmpSize">缩放图片的大小</param> /// <returns>缩放的图片</returns> public Bitmap BitMapZoom(Bitmap bmpSource, int bmpWidth, int bmpHeight) { Bitmap bmp, zoomBmp; try { bmp = new Bitmap(bmpSource); zoomBmp = new Bitmap(bmpWidth, bmpHeight); Graphics gh = Graphics.FromImage(zoomBmp); gh.InterpolationMode = InterpolationMode.HighQualityBicubic; gh.DrawImage(bmp, new Rectangle(0, 0, bmpWidth, bmpHeight), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); gh.Dispose(); return zoomBmp; } catch { } finally { bmp = null; zoomBmp = null; GC.Collect(); } return null; } #endregion #endregion } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)