基本信息
源码名称:C# 自定义tab按钮控件 示例 代码
源码大小:1.63KB
文件格式:.zip
开发语言:C#
更新时间:2018-05-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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);
}
}
}