123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- 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
- {
-
- /// <summary>
- /// 水晶按钮
- /// </summary>
- public class GlassButton : Control
- {
- #region 控件状态
- /// <summary>
- /// 控件状态
- /// </summary>
- public enum State
- {
- /// <summary>
- /// 无0
- /// </summary>
- Normal = 0,
- /// <summary>
- /// 获得焦点1
- /// </summary>
- Focused = 1,
- /// <summary>
- /// 失去焦点2
- /// </summary>
- LostFocused = 2,
- /// <summary>
- /// 鼠标指针进入控件3
- /// </summary>
- MouseEnter = 3,
- /// <summary>
- /// 鼠标按下按钮时4
- /// </summary>
- 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);
- /// <summary>
- /// 设置背景图片
- /// </summary>
- 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;
- /// <summary>
- /// 当前铵钮
- /// </summary>
- public bool ThisButton
- {
- get { return _thisButton; }
- set { _thisButton = value; Invalidate(false); }
- }
- /* /// <summary>
- /// 图片大小
- /// </summary>
- [CategoryAttribute("组件扩展属性"), Description("获取或设置控件显示的图标的大小")]
- public int Bmp_Size
- {
- get { return bmp_Size; }
- set { bmp_Size = value; }
- }*/
- /// <summary>
- /// 获取或设置控件显示的图片
- /// </summary>
- [CategoryAttribute("组件扩展属性"), Description("获取或设置控件显示的图标")]
- public Bitmap Bitmap
- {
- get { return _bmp; }
- set
- {
- _bmp = value;
- Invalidate(false);
- }
- }
- /// <summary>
- /// 重写Text属性
- /// </summary>
- [DescriptionAttribute("重写Text属性"), CategoryAttribute("组件扩展属性")]
- public override string Text
- {
- set { base.Text = value; Invalidate(false); }
- get
- {
- return base.Text;
- }
- }
- public object DefaultTGA = null;
- public string DefaultTEXT = "";
- public Bitmap DefaultBitMap;
- #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;
- /// <summary>
- /// 获取或设置控件显示的图片
- /// </summary>
- [CategoryAttribute("组件扩展属性"), Description("禁止调整大小")]
- public bool IsProhibitedResize
- {
- get { return isProhibitedResize; }
- set
- {
- isProhibitedResize = value;
- Invalidate(false);
- }
- }
-
- /// <summary>
- /// 禁止调整大小
- /// </summary>
- /// <param name="e"></param>
- 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;
- }
-
- }
- }
- /// <summary>
- /// 获取焦点后
- /// </summary>
- /// <param name="e"></param>
- protected override void OnGotFocus(EventArgs e)
- {
- base.OnGotFocus(e);
- this.state = State.Focused;
- Invalidate(false);
- }
- /// <summary>
- /// 失去焦点后
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- this.state = State.LostFocused;
- Invalidate(false);
- }
- /// <summary>
- /// 鼠标进入控件可见部份
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseEnter(EventArgs e)
- {
- base.OnMouseEnter(e);
- state = State.MouseEnter;
- Invalidate(false);
- }
- /// <summary>
- /// 鼠标离开控件可见部份
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
-
- state = State.Normal;
- Invalidate(false);
-
- }
- /// <summary>
- /// 鼠标弹起后
- /// </summary>
- /// <param name="mevent"></param>
- protected override void OnMouseUp(MouseEventArgs mevent)
- {
- base.OnMouseUp(mevent);
- this.state = State.MouseEnter;
- Invalidate(false);
- }
- /// <summary>
- /// 按下
- /// </summary>
- /// <param name="e"></param>
- 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)
- {
- if (IsZoomSize48)
- {
- if (this.bmp_Size >= 50)
- {
- this.bmp_Size = 48;
- }
- }
- 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));
- // g.DrawImage(bmp, new Rectangle(bmp_Left + 5, bmp_Top, bmp.Width - 10, bmp.Height - 10), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- }
- }
- 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+3;
- //txtTop = (Height - Convert.ToInt32(size.Height));
- if (txtTop > Height - 20) { txtTop = this.Height - 20; }
- g.DrawString(this.Text, Font, new SolidBrush(fColor), (this.Width - size.Width) / 2, txtTop);
- }
- #endregion
- public bool IsZoomSize48 = false;
- #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
- }
- }
|