如图:
部分代码:
/// <summary>
/// 添加新的page页
/// </summary>
/// <param name="str">页标题</param>
/// <param name="myForm">需要打开的窗体</param>
public void Add_TabPage(string str, Form myForm)
{
if (tabControlCheckHave(this.tabControl1, str))
{
return;
}
else
{
tabControl1.TabPages.Add(str);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
myForm.FormBorderStyle = FormBorderStyle.None;
myForm.Dock = DockStyle.Fill;
myForm.TopLevel = false;
myForm.Show();
myForm.Parent = tabControl1.SelectedTab;
}
}
/// <summary>
/// 判断窗体是否打开
/// </summary>
/// <param name="tab">tabControl1控件</param>
/// <param name="tabName">窗体标题</param>
/// <returns>false:没有打开 ;true:打开状态</returns>
public bool tabControlCheckHave(TabControl tab, string tabName)
{
for (int i = 0; i < tab.TabCount; i++)
{
if (tab.TabPages[i].Text == tabName)
{
tab.SelectedIndex = i;
return true;
}
}
return false;
}
//重新绘制page页
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
/*如果将 DrawMode 属性设置为 OwnerDrawFixed,
则每当 TabControl 需要绘制它的一个选项卡时,它就会引发 DrawItem 事件*/
try
{
this.tabControl1.TabPages[e.Index].BackColor = Color.LightBlue;
Rectangle tabRect = this.tabControl1.GetTabRect(e.Index);
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, (float)(tabRect.X + 2), (float)(tabRect.Y + 2));
using (Pen pen = new Pen(Color.White))
{
tabRect.Offset(tabRect.Width - 15, 2);
tabRect.Width = 15;
tabRect.Height = 15;
e.Graphics.DrawRectangle(pen, tabRect);
}
Color color = (e.State == DrawItemState.Selected) ? Color.LightBlue : Color.White;
using (Brush brush = new SolidBrush(color))
{
e.Graphics.FillRectangle(brush, tabRect);
}
using (Pen pen2 = new Pen(Color.Red))
{
Point point = new Point(tabRect.X + 3, tabRect.Y + 3);
Point point2 = new Point((tabRect.X + tabRect.Width) - 3, (tabRect.Y + tabRect.Height) - 3);
e.Graphics.DrawLine(pen2, point, point2);
Point point3 = new Point(tabRect.X + 3, (tabRect.Y + tabRect.Height) - 3);
Point point4 = new Point((tabRect.X + tabRect.Width) - 3, tabRect.Y + 3);
e.Graphics.DrawLine(pen2, point3, point4);
}
e.Graphics.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
去下载资源那里搜索“tabControl控件填充窗体并且可以关闭”,可以下载源码
下载地址:https://download.csdn.net/download/u012408847/11014282
个人微信公众号已经上线,欢迎大家扫描关注: