using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; namespace LYFZ.ComponentLibrary { /// /// 水晶按钮 /// public class GlassButton : Control { #region 控件状态 /// /// 控件状态 /// public enum State { /// /// 无0 /// Normal = 0, /// /// 获得焦点1 /// Focused = 1, /// /// 失去焦点2 /// LostFocused = 2, /// /// 鼠标指针进入控件3 /// MouseEnter = 3, /// /// 鼠标按下按钮时4 /// MouseDown = 4 // } #endregion #region//私有变量 private int bmp_Left; private int bmp_Top = 6; private int bmp_Size = 48; // private bool _focused = false; private State state = State.Normal; private Bitmap _BackImg = new Bitmap(LYFZ.ComponentLibrary.Properties.Resources.toolbar_hover); /// /// 设置背景图片 /// public Bitmap BackImg { get { return _BackImg; } set { _BackImg = value; } } private Bitmap _bmp = LYFZ.ComponentLibrary.Properties.Resources._062; #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//属性 private bool _thisButton = false; /// /// 当前铵钮 /// public bool ThisButton { get { return _thisButton; } set { _thisButton = value; Invalidate(false); } } /* /// /// 图片大小 /// [CategoryAttribute("组件扩展属性"), Description("获取或设置控件显示的图标的大小")] public int Bmp_Size { get { return bmp_Size; } set { bmp_Size = value; } }*/ /// /// 获取或设置控件显示的图片 /// [CategoryAttribute("组件扩展属性"), Description("获取或设置控件显示的图标")] public Bitmap Bitmap { get { return _bmp; } set { _bmp = value; Invalidate(false); } } /// /// 重写Text属性 /// [DescriptionAttribute("重写Text属性"), CategoryAttribute("组件扩展属性")] public override string Text { set { base.Text = value; Invalidate(false); } get { return base.Text; } } #endregion #region//重载事件 //自定义绘制 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; DrawImage(g); switch (state) { case State.MouseEnter: case State.Focused: { DrawIsSelected(g,2); break; } case State.MouseDown: DrawIsSelected(g, 1); break; case State.Normal: case State.LostFocused: if (ThisButton) { DrawIsSelected(g,2); } break; } DrawText(g); } bool isProhibitedResize = true; /// /// 获取或设置控件显示的图片 /// [CategoryAttribute("组件扩展属性"), Description("禁止调整大小")] public bool IsProhibitedResize { get { return isProhibitedResize; } set { isProhibitedResize = value; Invalidate(false); } } /// /// 禁止调整大小 /// /// protected override void OnResize(EventArgs e) { if (IsProhibitedResize) { Width = 80; Height = 75; } else { if (Width > Height) { this.bmp_Size = Height-6; } else { this.bmp_Size = Width-6; } } } /// /// 获取焦点后 /// /// protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); this.state = State.Focused; Invalidate(false); } /// /// 失去焦点后 /// /// protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); this.state = State.LostFocused; Invalidate(false); } /// /// 鼠标进入控件可见部份 /// /// protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); state = State.MouseEnter; Invalidate(false); } /// /// 鼠标离开控件可见部份 /// /// protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); state = State.Normal; Invalidate(false); } /// /// 鼠标弹起后 /// /// protected override void OnMouseUp(MouseEventArgs mevent) { base.OnMouseUp(mevent); this.state = State.MouseEnter; Invalidate(false); } /// /// 按下 /// /// protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.state = State.MouseDown; Invalidate(false); } #endregion #region//方法 #region//Draw void DrawIsSelected(Graphics g,int index) { ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, index, 2); } void DrawImage(Graphics g) { if (_bmp != null) { Bitmap bmp = ScaleZoom(_bmp); if (!this.Enabled) { bmp =LYFZ.WinAPI.CustomPublicMethod.GetGrayImage(bmp); } bmp_Left = (Width - bmp.Width) / 2; bmp_Top = (Height - bmp.Height) / 2-6; if (bmp_Top <= 6) { bmp_Top = 6; } g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height)); } } void DrawText(Graphics g) { SizeF size = g.MeasureString(this.Text, Font); Color fColor = ForeColor; if (!this.Enabled) fColor = Color.Gray; int txtTop = this.Height - 20; txtTop = (Height -Convert.ToInt32(size.Height)) / 2 + 25; if (txtTop > Height - 20) { txtTop = this.Height - 20; } g.DrawString(this.Text, Font, new SolidBrush(fColor), (this.Width - size.Width) / 2, txtTop); } #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 } }