123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 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
- {
- [ToolboxBitmap(typeof(Button))]
- public class ButtonExpand :System.Windows.Forms.Button // Control
- {
- /// <summary>
- /// 枚举按钮状态
- /// </summary>
- public enum State
- {
- /// <summary>
- /// 按钮默认时1
- /// </summary>
- Normal = 1,//
- /// <summary>
- /// 鼠标移上按钮时3
- /// </summary>
- MouseOver = 2,//
- /// <summary>
- /// 鼠标按下按钮时2
- /// </summary>
- MouseDown = 3,//
-
- /// <summary>
- /// 控件得到Tab焦点时
- /// </summary>
- Default = 4, //
- /// <summary>
- /// 当不启用按钮时5
- /// </summary>
- Disable =5 //(也就是按钮属性Enabled==Ture时)
- }
- #region//私有变量
-
- private State state = State.Normal;
- private SolidBrush brush;
- /// <summary>
- /// 默认背景
- /// </summary>
- private Bitmap _DfBackImg = null;
- private Bitmap _BackImg = new Bitmap(LYFZ.ComponentLibrary.Properties.Resources.btn_bg);
- /// <summary>
- ///
- /// </summary>
- [DescriptionAttribute("设置画到组件上背景图片"), CategoryAttribute("组件扩展属性")]
- public Bitmap BackImg
- {
- get { return _BackImg; }
- set { _BackImg = value; Invalidate(false); }
- }
- private Rectangle _BacklightLTRB=new Rectangle(10,10,27,22);
- /// <summary>
- /// 设置画到组件上背景图片位置
- /// </summary>
- [DescriptionAttribute("设置画到组件上背景图片位置"), CategoryAttribute("组件扩展属性"), TypeConverter(typeof(Rectangle))]
- public Rectangle BacklightLTRB
- {
- get { return _BacklightLTRB; }
- set { _BacklightLTRB = value; Invalidate(false); }
- }
- bool _isShowText = false;
- /// <summary>
- ///
- /// </summary>
- [DescriptionAttribute("是否显示文字"), CategoryAttribute("组件扩展属性")]
- public bool IsShowText
- {
- get { return _isShowText; }
- set {
- _isShowText = value;
- if (_isShowText) {
- if (this.Width == 27&&this.Height==22)
- {
- this.Size = new Size(75,22);
- }
- if (this.BackImg == null)
- {
- this.BackImg = LYFZ.ComponentLibrary.Properties.Resources.btn_bg4;
- }
- _BacklightLTRB = new Rectangle(10, 10, 26, 21);
- }
- Invalidate(false); }
-
- }
- bool isCustomBackImg = false;
- /// <summary>
- ///
- /// </summary>
- [DescriptionAttribute("设置是否可以自定义按钮背景图片"), CategoryAttribute("组件扩展属性")]
- public bool IsCustomBackImg
- {
- get { return isCustomBackImg; }
- set { isCustomBackImg = value; Invalidate(false); }
- }
- #endregion
- public ButtonExpand()
- // : base()
- {
- 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(27, 22);//初始大小
- Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
- }
- catch { }
- }
- public ButtonExpand(Bitmap dfBackImg)
- : this()
- {
- this._DfBackImg = dfBackImg;
- }
-
- /// <summary>
- /// 按下
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- this.state = State.MouseDown;
- Invalidate(false);
-
- }
- protected override void OnGotFocus(EventArgs e)
- {
- base.OnGotFocus(e);
- this.state = State.MouseOver;
- Invalidate(false);
- }
- /// <summary>
- /// 失去焦点后
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- this.state = State.Normal;
- Invalidate(false);
- }
- /// <summary>
- /// 鼠标弹起后
- /// </summary>
- /// <param name="mevent"></param>
- protected override void OnMouseUp(MouseEventArgs mevent)
- {
- base.OnMouseUp(mevent);
- this.state = State.MouseOver;
- Invalidate(false);
- }
- /// <summary>
- /// 鼠标移上控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseEnter(EventArgs e)
- {
- base.OnMouseEnter(e);
- state = State.MouseOver;
- 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="e"></param>
- protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
- {
-
- if (BackImg == null)
- {
- base.OnPaint(e);
- return;
- }
-
- int i = (int)state;
- //if (this.Focused && state != State.MouseDown ) i = 4;
- if (!this.Enabled) i = 5;
- Rectangle rc = this.ClientRectangle;
- Graphics g = e.Graphics;
- base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));
- try
- {
- if (BackImg != null)
- {
- if (!isCustomBackImg && this._DfBackImg != null)
- {
- this._BackImg = this._DfBackImg;
- }
- else {
- if (!isCustomBackImg)
- {
- switch (this.Name)
- {
- case "btnAppFormExit":
- this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_close;
- break;
- case "btnAppFormMaximize":
- this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_max;
- break;
- case "btnAppFormMinimize":
- this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_mini;
- break;
- case "btnAppFormMenu":
- this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_QuickMenu;
- break;
- case "btnAppFormSkin":
- this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_skin;
- break;
- }
- }
- }
- ImageDrawRect.DrawRect(g, BackImg, rc, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), i, 5);
-
- }
- }
- catch
- { }
- Image img = null;
- Size txts, imgs;
- txts = Size.Empty;
- imgs = Size.Empty;
- if (this.Image != null)
- {
- img = this.Image;
- }
- else if (this.ImageList != null && this.ImageIndex != -1)
- {
- img = this.ImageList.Images[this.ImageIndex];
- }
- if (img != null)
- {
- imgs.Width = img.Width;
- imgs.Height = img.Height;
- }
- StringFormat format1;
- using (format1 = new StringFormat())
- {
- format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
- SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1);
- txts = Size.Ceiling(ef1);
-
- }
- rc.Inflate(-4, -4);
- if (imgs.Width * imgs.Height != 0)
- {
- Rectangle imgr = rc;
- imgr = ImageDrawRect.HAlignWithin(imgs, imgr, this.ImageAlign);
- imgr = ImageDrawRect.VAlignWithin(imgs, imgr, this.ImageAlign);
-
- if (!this.Enabled)
- {
- ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor);
- }
- else
- {
- g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height);
- }
- }
-
- Rectangle txtr = rc;
-
- txtr = ImageDrawRect.HAlignWithin(txts, txtr, this.TextAlign);
- txtr = ImageDrawRect.VAlignWithin(txts, txtr, this.TextAlign);
- txtr.Y += 5;
- format1 = new StringFormat();
- format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
- if (this.RightToLeft == RightToLeft.Yes)
- {
- format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
- }
- brush = new SolidBrush(this.ForeColor);
- if (IsShowText)
- g.DrawString(this.Text, this.Font, brush, (RectangleF)txtr, format1);
-
- brush.Dispose();
-
- }
- }
- }
|