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); } }}
下载C# 自定义tab按钮控件 示例 代码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论