C# 带有关闭和添加按钮的选项卡控件
我试图使选项卡控件具有“x”(关闭按钮)和“+”(新选项卡按钮)。我找到了添加的解决方案,该选项卡现在如下所示: 但是我想添加一个C# 带有关闭和添加按钮的选项卡控件,c#,.net,winforms,custom-controls,tabcontrol,C#,.net,Winforms,Custom Controls,Tabcontrol,我试图使选项卡控件具有“x”(关闭按钮)和“+”(新选项卡按钮)。我找到了添加的解决方案,该选项卡现在如下所示: 但是我想添加一个+黑圈现在在哪里。我不知道如何绘制,我尝试在最后一个选项卡的Paint事件上绘制,如下所示: var p = tabs.TabPages[tabs.TabCount - 1]; p.Paint += new PaintEventHandler(tab_OnDrawPage); private void tab_OnDrawPage(object sender, P
+
黑圈现在在哪里。我不知道如何绘制,我尝试在最后一个选项卡的Paint
事件上绘制,如下所示:
var p = tabs.TabPages[tabs.TabCount - 1];
p.Paint += new PaintEventHandler(tab_OnDrawPage);
private void tab_OnDrawPage(object sender, PaintEventArgs e)
{
// e.ClipRectangle.
e.Graphics.DrawString("+",
new Font("verdana",
10,
FontStyle.Bold),
Brushes.Black,
e.ClipRectangle.X + 10,
e.ClipRectangle.Y + 10);
}
但它没有显示任何吸引人的地方。我想这与我传递给DrawString()
call的位置有关,但我不知道应该使用哪个位置。我使用+10将它从最后一个选项卡中拉开。如何解决这个问题?。我自己没有做过任何自定义绘图,我正在学习。通常,直接的“低级”方法是处理Paint
事件并绘制到TabControl
本身,然后处理鼠标输入事件以检测绘制位置的单击
但是,a)这是一种痛苦,b)选项卡Control会抑制绘制
事件,因此,如果不进行更低级别的处理,并且在WndProc()
方法覆盖中处理WM_绘制
消息,则无法进行处理
出于您的目的,我建议只需在表单
中添加一个新控件,例如按钮
,将其放置在选项卡控件
上您希望用户能够单击的位置上。然后在按钮中,单击事件处理程序,可以根据需要添加新页面。如果要封装按钮
和选项卡控件
的组合,可以使用用户控件
例如:
TabControlWithAdd.Designer.cs:
partial class TabControlWithAdd
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(247, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(23, 23);
this.button1.TabIndex = 0;
this.button1.Text = "+";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabControl1
//
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(3, 3);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(267, 181);
this.tabControl1.TabIndex = 1;
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 25);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(259, 152);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 25);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(192, 71);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// TabControlWithAdd
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.button1);
this.Controls.Add(this.tabControl1);
this.Name = "TabControlWithAdd";
this.Size = new System.Drawing.Size(273, 187);
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
}
public partial class TabControlWithAdd : UserControl
{
public TabControlWithAdd()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Add("Tab " + (tabControl1.TabPages.Count + 1));
}
}
上面使用的是按钮
,但是您当然可以使用任何其他您喜欢的可单击控件,包括标签
(例如,如果您不希望按钮边框的外观),以产生您想要的视觉效果。作为一个选项,您可以添加一个额外的选项卡,该选项卡显示一个添加图标,并在用户单击该选项卡时进行检查,然后在其前面插入一个新的
此外,您还可以防止仅使用事件来选择额外的选项卡。这样,最后一个选项卡的作用就像IE和Chrome一样,只是一个添加按钮
实施细节
我们将使用所有者绘制选项卡在每个选项卡上显示关闭图标,在最后一个选项卡上显示添加图标。我们使用绘制关闭并添加图标,MouseDown
来处理单击关闭并添加按钮,选择
来防止选择最后一个选项卡,HandleCreated
来调整选项卡宽度。您可以在下面看到所有实施设置和代码
初始化
为DrawItem
、MouseDown
、选择和HandleCreated
事件设置填充和分配事件处理程序
this.tabControl1.Padding = new Point(12, 4);
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += tabControl1_DrawItem;
this.tabControl1.MouseDown += tabControl1_MouseDown;
this.tabControl1.Selecting += tabControl1_Selecting;
this.tabControl1.HandleCreated += tabControl1_HandleCreated;
点击关闭按钮和添加按钮
您可以处理MouseDown
或MouseClick
事件,检查最后一个选项卡矩形是否包含鼠标单击点,然后在最后一个选项卡之前插入一个选项卡。否则,请检查其中一个关闭按钮是否包含单击的位置,然后关闭单击其关闭按钮的选项卡:
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
var lastIndex = this.tabControl1.TabCount - 1;
if (this.tabControl1.GetTabRect(lastIndex).Contains(e.Location))
{
this.tabControl1.TabPages.Insert(lastIndex, "New Tab");
this.tabControl1.SelectedIndex = lastIndex;
}
else
{
for (var i = 0; i < this.tabControl1.TabPages.Count; i++)
{
var tabRect = this.tabControl1.GetTabRect(i);
tabRect.Inflate(-2, -2);
var closeImage = Properties.Resources.DeleteButton_Image;
var imageRect = new Rectangle(
(tabRect.Right - closeImage.Width),
tabRect.Top + (tabRect.Height - closeImage.Height) / 2,
closeImage.Width,
closeImage.Height);
if (imageRect.Contains(e.Location))
{
this.tabControl1.TabPages.RemoveAt(i);
break;
}
}
}
}
绘制关闭按钮和添加按钮
要绘制关闭按钮和添加按钮,可以处理DrawItem
事件。我将这些图标用于添加和关闭按钮
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
var tabPage = this.tabControl1.TabPages[e.Index];
var tabRect = this.tabControl1.GetTabRect(e.Index);
tabRect.Inflate(-2, -2);
if (e.Index == this.tabControl1.TabCount - 1)
{
var addImage = Properties.Resources.AddButton_Image;
e.Graphics.DrawImage(addImage,
tabRect.Left + (tabRect.Width - addImage.Width) / 2,
tabRect.Top + (tabRect.Height - addImage.Height) / 2);
}
else
{
var closeImage = Properties.Resources.DeleteButton_Image;
e.Graphics.DrawImage(closeImage,
(tabRect.Right - closeImage.Width),
tabRect.Top + (tabRect.Height - closeImage.Height) / 2);
TextRenderer.DrawText(e.Graphics, tabPage.Text, tabPage.Font,
tabRect, tabPage.ForeColor, TextFormatFlags.Left);
}
}
调整标签宽度
要调整制表符宽度并使最后一个制表符具有较小的宽度,您可以将hanldeHandleCreated
事件发送到控件并指定制表符宽度允许的最小大小:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int TCM_SETMINTABWIDTH = 0x1300 + 49;
private void tabControl1_HandleCreated(object sender, EventArgs e)
{
SendMessage(this.tabControl1.Handle, TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)16);
}
下载
您可以在此处下载代码或克隆存储库:
另一种方法是创建一个新的TabControl
,它扩展了TabControl
类。我曾经遇到过同样的问题,我就是这样做的,我找不到完成的代码,但这将在选项卡中添加一个X
,同样可以应用于+
符号:
public delegate bool PreRemoveTab(int indx);
public class TabControlEx : TabControl
{
public TabControlEx()
: base()
{
PreRemoveTabPage = null;
this.DrawMode = TabDrawMode.OwnerDrawFixed;
}
public PreRemoveTab PreRemoveTabPage;
protected const int size = 5;
protected int moveRight = 0;
protected int MoveRight
{
get { return moveRight; }
set { moveRight = value; }
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
Brush b = new SolidBrush(Color.Salmon);
Brush b1 = new SolidBrush(Color.Black);
Font f = this.Font;
Font f1 = new Font("Arial", 9,FontStyle.Bold);
if (e.Index != 0)
{
Rectangle r = e.Bounds;
r = GetTabRect(e.Index);
r.Offset(2, 2);
r.Width = size;
r.Height = size;
Pen p = new Pen(b,2);
string title = this.TabPages[e.Index].Text;
string boldLetter = title.Substring(0, 1);
title = title.Remove(0, 1);
MoveRight = ((Int32)e.Graphics.MeasureString(title, f, 200).Width) + 1; // -1
e.Graphics.DrawLine(p, r.X +10 + MoveRight - 2, r.Y, r.X +10 + MoveRight + r.Width, r.Y + r.Height+2);
e.Graphics.DrawLine(p, r.X +10 + MoveRight + r.Width, r.Y, r.X + 10 + MoveRight-2, r.Y + r.Height+2);
e.Graphics.DrawString(boldLetter, f1, b1, new PointF(r.X, r.Y));
e.Graphics.DrawString(title, f, b1, new PointF(r.X+8, r.Y+1));
}
else
{
Rectangle r = GetTabRect(e.Index);
e.Graphics.DrawString(this.TabPages[e.Index].Text, f, b1, new PointF(r.X + 5, r.Y));
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
Point p = e.Location;
for (int i = 0; i < TabCount; i++)
{
Rectangle r = GetTabRect(i);
r.Offset(2, 2);
r.Width = size+2;
r.Height = size+2;
r.X = r.X + MoveRight + 8;
if (r.Contains(p))
{
if (i != 0)
{
CloseTab(i);
}
}
}
}
private void CloseTab(int i)
{
if (PreRemoveTabPage != null)
{
bool closeIt = PreRemoveTabPage(i);
if (!closeIt)
return;
}
TabPages.Remove(TabPages[i]);
}
}
public delegate bool PreRemoveTab(intindx);
公共类TabControlEx:TabControl
{
公共TabControlEx()
:base()
{
PreRemoveTabPage=null;
this.DrawMode=TabDrawMode.OwnerDrawFixed;
}
公共预移除选项卡预移除选项卡页面;
受保护常量int size=5;
受保护的int moveRight=0;
受保护的整数移动权
{
获取{return moveRight;}
设置{moveRight=value;}
}
受保护的覆盖无效OnDrawItem(DrawItemEventArgs e)
{
笔刷b=新的SolidBrush(颜色为鲑鱼色);
笔刷b1=新的SolidBrush(颜色为黑色);
Font f=这个.Font;
字体f1=新字体(“Arial”,9,FontStyle.Bold);
如果(例如索引!=0)
{
矩形r=e.界;
r=GetTabRect(e.索引);
r、 偏移量(2,2);
r、 宽度=尺寸;
r、 高度=尺寸;
笔p=新笔(b,2);
字符串标题=this.TabPages[e.Index].Text;
字符串粗体字母=标题。子字符串(0,1);
title=title.Remove(0,1);
MoveRight=((Int32)e.Graphics.MeasureString(标题,f,200).宽度)+1;//-1
e、 图形绘制线(p,r.X+10+MoveRight-2,r.Y,r.X+10+MoveRight+r.Width,r.Y+r.Height+2);
e、 图形绘制线(p,r.X+10+右移动+r.宽度,r.Y,r.X+10+右移动-2,r.Y+r.高度+2);
e、 图形.抽绳(粗体字母,f1,b1,新点F(r.X,r.Y));
e、 图形.抽绳(标题,f,b1,新点f(r.X+8,r.Y+1));
}
其他的
{
矩形r=GetTabRect(即索引);
e、 Graphics.DrawString(this.TabPages[e.Index].Text,f,b1,新的点f(r.X+5,r.Y));
}
}
鼠标单击时受保护的覆盖无效(鼠标目标e)
{
点p=e.位置;
为了
public delegate bool PreRemoveTab(int indx);
public class TabControlEx : TabControl
{
public TabControlEx()
: base()
{
PreRemoveTabPage = null;
this.DrawMode = TabDrawMode.OwnerDrawFixed;
}
public PreRemoveTab PreRemoveTabPage;
protected const int size = 5;
protected int moveRight = 0;
protected int MoveRight
{
get { return moveRight; }
set { moveRight = value; }
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
Brush b = new SolidBrush(Color.Salmon);
Brush b1 = new SolidBrush(Color.Black);
Font f = this.Font;
Font f1 = new Font("Arial", 9,FontStyle.Bold);
if (e.Index != 0)
{
Rectangle r = e.Bounds;
r = GetTabRect(e.Index);
r.Offset(2, 2);
r.Width = size;
r.Height = size;
Pen p = new Pen(b,2);
string title = this.TabPages[e.Index].Text;
string boldLetter = title.Substring(0, 1);
title = title.Remove(0, 1);
MoveRight = ((Int32)e.Graphics.MeasureString(title, f, 200).Width) + 1; // -1
e.Graphics.DrawLine(p, r.X +10 + MoveRight - 2, r.Y, r.X +10 + MoveRight + r.Width, r.Y + r.Height+2);
e.Graphics.DrawLine(p, r.X +10 + MoveRight + r.Width, r.Y, r.X + 10 + MoveRight-2, r.Y + r.Height+2);
e.Graphics.DrawString(boldLetter, f1, b1, new PointF(r.X, r.Y));
e.Graphics.DrawString(title, f, b1, new PointF(r.X+8, r.Y+1));
}
else
{
Rectangle r = GetTabRect(e.Index);
e.Graphics.DrawString(this.TabPages[e.Index].Text, f, b1, new PointF(r.X + 5, r.Y));
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
Point p = e.Location;
for (int i = 0; i < TabCount; i++)
{
Rectangle r = GetTabRect(i);
r.Offset(2, 2);
r.Width = size+2;
r.Height = size+2;
r.X = r.X + MoveRight + 8;
if (r.Contains(p))
{
if (i != 0)
{
CloseTab(i);
}
}
}
}
private void CloseTab(int i)
{
if (PreRemoveTabPage != null)
{
bool closeIt = PreRemoveTabPage(i);
if (!closeIt)
return;
}
TabPages.Remove(TabPages[i]);
}
}