using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace ButtonImages { #region 控件状态 /// /// 控件状态 /// public enum State { /// /// 无 /// Normal = 0, /// /// 获得焦点 /// Focused = 1, /// /// 失去焦点 /// LostFocused = 2, /// /// 鼠标指针进入控件 /// MouseEnter = 3 } #endregion /// /// Toolbar控件 /// public partial class GlassButton : Control { #region//私有变量 private int bmp_Left; private const int bmp_Top = 6; private const int bmp_Size = 48; private bool _focused = false; private State state = State.Normal; /// /// /// public Bitmap _BackImg = ImageObject.GetResBitmap(ryControls.SkinHelp.SkinFolder + "ButtonImages.Toolbar_Hover.png"); private Bitmap _bmp = null; #endregion #region//构造函数 /// /// /// public GlassButton() { try { this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.StandardDoubleClick, false); this.SetStyle(ControlStyles.Selectable, true); ResizeRedraw = true; BackColor = Color.Transparent; ForeColor = Color.White;//初始文本颜色 Size = new Size(80, 75);//初始大小 Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体 } catch { } } #endregion #region//属性 /// /// 获取或设置控件显示的图片 /// [CategoryAttribute("扩展属性"), Description("获取或设置控件显示的图标")] public Bitmap Bitmap { get { return _bmp; } set { _bmp = value; Invalidate(false); } } /// /// 重写控件焦点属性 /// [CategoryAttribute("扩展属性"), 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); ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, 1, 2); 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 = 80; Height = 75; } /// /// /// /// 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) { ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, 2, 2); } 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, 55); } #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 /// /// 图片缩放 /// /// 源图片 /// 缩放图片的宽度 /// 缩放图片的高度 /// 缩放的图片 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 } }