using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 控件库_
{
public partial class tabcontrol重绘 : UserControl
{
public tabcontrol重绘()
{
InitializeComponent();
}
const int CLOSE_SIZE = 15;
//重绘关闭按钮
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
//Rectangle rec = tabControl1.ClientRectangle;
//SolidBrush br = new SolidBrush(Color.White);
//e.Graphics.FillRectangle(br, rec);
// 重绘tabPage
Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);
//添加TabPage属性
//新建一个StringFormat对象,用于对标签文字的布局设置
StringFormat StrFormat = new StringFormat();
//重绘字体和标签颜色
var tabcon = sender as TabControl;
//重绘标签头=======================
SolidBrush back;
SolidBrush white;
if (e.Index == tabcon.SelectedIndex)//当前Tab page页的样式
{
//背景颜色
back = new SolidBrush(/*Color.FromArgb(45, 45, 48)*/Color.FromArgb(1, 158, 213));
//字体颜色
white = new SolidBrush(Color.White);
}
else//其余Tab page页的样式
{
//背景颜色
back = new SolidBrush(Color.White);
//字体颜色
white = new SolidBrush(Color.FromArgb(3, 41, 81));
}
StringFormat sf = new StringFormat()
{
//文本水平/垂直居中对齐
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Center,
};
//绑定选项卡
Rectangle rec = tabcon.GetTabRect(e.Index);
//设置选项卡背景
e.Graphics.FillRectangle(back, rec);
//设置选项卡字体及颜色
e.Graphics.DrawString(tabcon.TabPages[e.Index].Text, new Font("楷体", 13, FontStyle.Bold), white, rec, sf);
//再画一个矩形框
using (Pen p = new Pen(Color.Black))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
}
//填充矩形框
Color recColor = e.State == DrawItemState.Selected ? Color.FromArgb(200, 227, 253) : Color.White;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
}
tabControl1.Appearance = TabAppearance.Normal;
//画关闭符号
using (Pen p = new Pen(Color.FromArgb(3, 41, 81)))
{
//画"/"线
Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
e.Graphics.DrawLine(p, p1, p2);
//画"/"线
Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
e.Graphics.DrawLine(p, p3, p4);
}
e.Graphics.Dispose();
}
catch (Exception ex)
{
}
}
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.tabControl1.SelectedIndex > 0)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
//如果鼠标在区域内就关闭选项卡
bool isClose = x > myTabRect.X && x < myTabRect.Right
&& y > myTabRect.Y && y < myTabRect.Bottom;
if (isClose == true)
{
this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
}
}
return;
}
private void tabcontrol重绘_Load(object sender, EventArgs e)
{
//this.tabControl1.TabPages.Clear();
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE);
this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);
}
}
}
C#-UI-Tabcontrol控件重绘以及关闭按钮绘制
于 2023-02-22 15:02:54 首次发布