实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BaiduTabs { class TabButton:Control { private Image _HoverImage; private Image _DownImage; private Image _DefaultImage; private ButtonStyleShow _ButtonStyleShow=ButtonStyleShow.Default; private Boolean _Border = true; private Boolean _BorderTop = true; private Boolean _BorderLeft = true; private Boolean _BorderRigth = true; private Boolean _BorderBottom = true; private Color _BorderColor = Color.CadetBlue; //属性 [Description("默认背景"), Category("ButtonBG")] [DefaultValue(null)] public Image DefaultImage { get { return _DefaultImage; } set { _DefaultImage = value; Invalidate(); } } [Description("进入背景"), Category("ButtonBG")] [DefaultValue(null)] public Image HoverImage { get { return _HoverImage; } set { _HoverImage = value; } } [Category("ButtonBG")] [Description("按下背景")] [DefaultValue(null)] public Image DownImage { get { return _DownImage; } set { _DownImage = value; } } [Category("显示")] [Description("样式")] [DefaultValue(ButtonStyleShow.Default)] public ButtonStyleShow ButtonStyle { get { return _ButtonStyleShow; } set { _ButtonStyleShow = value; Invalidate(); } } [Category("边框")] [Description("边框")] [DefaultValue(true)] public Boolean Border { get { return _Border; } set { _Border = value; Invalidate(); } } [Category("边框")] [Description("上边框")] [DefaultValue(true)] public Boolean BorderTop { get { return _BorderTop; } set { _BorderTop = value; Invalidate(); } } [Category("边框")] [Description("下边框")] [DefaultValue(true)] public Boolean BorderBottom { get { return _BorderBottom; } set { _BorderBottom = value; Invalidate(); } } [Category("边框")] [Description("左边框")] [DefaultValue(true)] public Boolean BorderLeft { get { return _BorderLeft; } set { _BorderLeft = value; Invalidate(); } } [Category("边框")] [Description("右边框")] [DefaultValue(true)] public Boolean BordeRigth { get { return _BorderRigth; } set { _BorderRigth = value; Invalidate(); } } [Category("边框")] [Description("边框颜色")] [DefaultValue(true)] public Color BordeColor { get { return _BorderColor; } set { _BorderColor = value; Invalidate(); } } public override string Text { get { return base.Text; } set { base.Text = value; Invalidate(); } } //方法 public TabButton() { this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.Transparent; this.Size = new Size(100, 30); } //枚举显示 public enum ButtonStyleShow { Default=0, Image=1 } private int show = 0; protected override void OnPaint(PaintEventArgs e) { Graphics cavan=e.Graphics; Size textsize = cavan.MeasureString(this.Text, this.Font).ToSize(); if (show == 0) { switch (_ButtonStyleShow) { case ButtonStyleShow.Default: cavan.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(3, 3, this.Size.Width - 6, this.Size.Height - 6)); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); if (_Border) { if (_BorderTop){cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, this.Size.Width, 3)); }//上 if (_BorderLeft) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, 3, this.Size.Height)); }//右 if (_BorderRigth) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(this.Size.Width - 3, 0, 3, this.Size.Height)); }//左 if (_BorderBottom) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, this.Height - 3, this.Size.Width, 3)); }//下 } break; case ButtonStyleShow.Image: if (_DefaultImage != null) { cavan.DrawImage(_DefaultImage, 0, 0, this.Size.Width, this.Size.Height); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); } break; } } else if (show == 1)//按下 { switch (_ButtonStyleShow) { case ButtonStyleShow.Default: cavan.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(4, 4, this.Size.Width - 6, this.Size.Height - 6)); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); if (_Border) { if (_BorderTop) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, this.Size.Width, 4)); }//上 if (_BorderLeft) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, 3, this.Size.Height)); }//右 if (_BorderRigth) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(this.Size.Width - 3, 0, 3, this.Size.Height)); }//左 if (_BorderBottom) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, this.Height - 3, this.Size.Width, 2)); }//下 } break; case ButtonStyleShow.Image: cavan.DrawImage(_DownImage, 0, 0, this.Size.Width, this.Size.Height); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); break; } } else if (show == 2)//移上 { switch (_ButtonStyleShow) { case ButtonStyleShow.Default: cavan.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(3, 3, this.Size.Width - 6, this.Size.Height - 6)); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); if (_Border) { if (_BorderTop){cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, this.Size.Width, 3)); }//上 if (_BorderLeft) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, 3, this.Size.Height)); }//右 if (_BorderRigth) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(this.Size.Width - 3, 0, 3, this.Size.Height)); }//左 if (_BorderBottom) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, this.Height - 3, this.Size.Width, 3)); }//下 } break; case ButtonStyleShow.Image: cavan.DrawImage(_HoverImage, 0, 0, this.Size.Width, this.Size.Height); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); break; } } base.OnPaint(e); } protected override void OnMouseDown(MouseEventArgs e) { show = 1; Invalidate(); base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { show = 0; Invalidate(); base.OnMouseUp(e); } private Color FontColor1; protected override void OnMouseEnter(EventArgs e) { FontColor1 = this.ForeColor; this.ForeColor = Color.FromKnownColor(KnownColor.Highlight); show = 2; Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { this.ForeColor=FontColor1; show = 0; Invalidate(); base.OnMouseLeave(e); } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论