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 { /// /// 枚举按钮状态 /// public enum State { /// /// 按钮默认时1 /// Normal = 1,// /// /// 鼠标移上按钮时3 /// MouseOver = 2,// /// /// 鼠标按下按钮时2 /// MouseDown = 3,// /// /// 控件得到Tab焦点时 /// Default = 4, // /// /// 当不启用按钮时5 /// Disable =5 //(也就是按钮属性Enabled==Ture时) } #region//私有变量 private State state = State.Normal; private SolidBrush brush; private Bitmap _BackImg = new Bitmap(LYFZ.ComponentLibrary.Properties.Resources.btn_bg); /// /// /// [DescriptionAttribute("设置画到组件上背景图片"), CategoryAttribute("组件扩展属性")] public Bitmap BackImg { get { return _BackImg; } set { _BackImg = value; Invalidate(false); } } private Rectangle _BacklightLTRB=new Rectangle(10,10,27,22); /// /// 设置画到组件上背景图片位置 /// [DescriptionAttribute("设置画到组件上背景图片位置"), CategoryAttribute("组件扩展属性"), TypeConverter(typeof(Rectangle))] public Rectangle BacklightLTRB { get { return _BacklightLTRB; } set { _BacklightLTRB = value; Invalidate(false); } } bool _isShowText = false; /// /// /// [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); } this.BackImg = LYFZ.ComponentLibrary.Properties.Resources.btn_bg4; _BacklightLTRB = new Rectangle(10, 10, 26, 21); } 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 { } } /// /// 按下 /// /// 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); } /// /// 失去焦点后 /// /// protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); this.state = State.Normal; Invalidate(false); } /// /// 鼠标弹起后 /// /// protected override void OnMouseUp(MouseEventArgs mevent) { base.OnMouseUp(mevent); this.state = State.MouseOver; Invalidate(false); } /// /// 鼠标移上控件 /// /// protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); state = State.MouseOver; Invalidate(false); } /// /// 鼠标离开控件 /// /// protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); state = State.Normal; Invalidate(false); } /// /// 重绘控件 /// /// 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) { 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(); } } }