C# 重绘tabControl,添加关闭按钮(续)

时间:2023-03-08 17:18:50
C# 重绘tabControl,添加关闭按钮(续)

在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,tableControl都会自动跳到第一个页面,显然 这不是我们想要的,为此,我修改了部分的代码。除此之外,我还添加了一些两个新的方法,用于创建新的tablePage.以下是我实现 的类

 #region 重绘tablecontrol的类(添加关闭页面功能),ablepage的创建,以及窗体的附加
public class DrawTabControl
{
TabControl tabControl1 = null;
Font font1 = null; //打开和关闭的页面索引
int tabindex_show = ;
int tabindex_close = ; public DrawTabControl() { }
/// <summary>
/// 初始化
/// </summary>
/// <param name="tabcontrol">TacControl控件</param>
/// <param name="fo">主程序this.Font</param>
public DrawTabControl(TabControl tabcontrol,Font fo)
{
tabControl1 = tabcontrol;
font1 = fo;
} #region 关于tabcontrol的重绘 const int CLOSE_SIZE = ;//大小
//tabPage标签图片
Bitmap image = new Bitmap("D:\\power_003.png"); public void ClearPage()
{ //清空控件
//this.MainTabControl.TabPages.Clear();
//绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样
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);
} /// <summary>
/// 产生新的窗体时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index); //先添加TabPage属性
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.font1, SystemBrushes.ControlText, myTabRect.X + , myTabRect.Y + ); //再画一个矩形框
using (Pen p = new Pen(Color.White))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + ), );
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
} ////填充矩形框
Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
} //画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
////=============================================
//自己画X
////"\"线
Point p1 = new Point(myTabRect.X + , myTabRect.Y + );
Point p2 = new Point(myTabRect.X + myTabRect.Width - , myTabRect.Y + myTabRect.Height - );
e.Graphics.DrawLine(objpen, p1, p2);
////"/"线
Point p3 = new Point(myTabRect.X + , myTabRect.Y + myTabRect.Height - );
Point p4 = new Point(myTabRect.X + myTabRect.Width - , myTabRect.Y + );
e.Graphics.DrawLine(objpen, p3, p4); ////=============================================
//使用图片
//Bitmap bt = new Bitmap(image);
//Point p5 = new Point(myTabRect.X, 4);
//e.Graphics.DrawImage(bt, p5);
//e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.font1, objpen.Brush, p5);
}
e.Graphics.Dispose();
}
catch (Exception)
{ } } /// <summary>
/// 鼠标点击选项卡时的触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{ int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex); myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + ), );
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)
{
//判断关闭的页面索引是否比正在显示的页面的索引
//如果关闭的页面的索引大于或等于显示正在显示的页面索引,刚tabindex_close的值为关闭的页面的索引;
//相反 的,刚tabindex_close则为tabindex_show-1; tabindex_close = this.tabControl1.SelectedIndex >= tabindex_show ? this.tabControl1.SelectedIndex : tabindex_close - ;
this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
}
else
{
tabindex_close = this.tabControl1.SelectedIndex;
tabindex_show = this.tabControl1.SelectedIndex;
}
if (tabindex_close < tabindex_show)
{
tabindex_show = tabindex_show - ; }
//显示页面
try { this.tabControl1.SelectedTab = this.tabControl1.TabPages[tabindex_show]; }
catch (Exception ex) { } Console.WriteLine("显示页面" + tabindex_show + "关闭页面" + tabindex_close); }
}
#endregion #region 关于tablepage的创建,以及窗体的附加 /// <summary>
/// 创建新的选项卡
/// </summary>
/// <param name="ParentForm">父窗体</param>
/// <param name="ChildForm">子窗体</param>
/// <param name="Pagename">页面名称</param>
public void CreatePage(Form ParentForm,Form ChildForm, string Pagename)
{ ChildForm.MdiParent = ParentForm;
ChildForm.Text = "第二个窗体";
ChildForm.Dock = DockStyle.Fill;
ChildForm.TopMost = true;
ChildForm.FormBorderStyle = FormBorderStyle.None;
TabPage tb1 = AddPage(Pagename);
tb1.Controls.Add(ChildForm);
this.tabControl1.Controls.Add(tb1);
ChildForm.Show();
}
/// <summary>
/// 添加选项卡
/// </summary>
/// <param name="PageName">页面名称</param>
/// <returns>一个页面</returns>
public TabPage AddPage(string PageName)
{
TabPage tabPage = new TabPage();
tabPage.Location = new System.Drawing.Point(, );
tabPage.Name = PageName;
tabPage.Padding = new System.Windows.Forms.Padding();
tabPage.Size = new System.Drawing.Size(, );
tabPage.Text = PageName;
tabPage.UseVisualStyleBackColor = true;
return tabPage;
} #endregion }
#endregion

调用的方法,和上一篇的一样,

DrawTabControl dtc=null;
dtc=new DrawTabControl(tabControl1,this.Font);
dtc.ClearPage();

上面的代码是实例化了一个类,并调用ClearPage方法,注意,

1、在添加选项卡之前一定要调用ClearPage()这个方法;

2、this.Font 解释为:System.Drawing.Font,它定义字符串的文本格式。具体作用还不太清楚,如果有懂的朋友,还望帮忙讲解一下;

Form3 fo3 = new Form3();
dtc.CreatePage(this,fo3, "窗体");

上面的代码 为创建选项卡,其中this为父窗体 ,fo3为子窗体 ,第三个参数为窗体名