带关闭和添加按钮的TabControl

时间:2016-04-27 16:02:15

标签: c# .net winforms custom-controls tabcontrol

我想让一个标签控件有一个" x" (关闭按钮)和" +" (新标签按钮)。我找到了一个添加x button的解决方案,该标签现在看起来像这样:

enter image description here

但是我想在其中添加一个+黑色圆圈。我不知道怎么做,我试着在最后一个标签的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()电话的位置有关,但我不知道要使用的正确位置。我使用+10将它从最后一个标签中拉出来。如何解决?我自己还没有完成任何自定义绘图,我正在学习它。

3 个答案:

答案 0 :(得分:19)

作为一个选项,您可以添加一个额外的标签,其中会显示添加图标Add并检查用户点击该标签的时间,然后在其前面插入新的TabPage

此外,您可以使用Selecting TabControl事件来阻止选择该额外标签。这样,最后一个标签的作用就像是一个添加按钮,如IE和Chrome。

Tab with close and add button

实施细则

我们将使用所有者绘制标签在每个标签上显示最后一个标签上的添加图标。我们使用DrawItem绘制关闭并添加图标,MouseDown来处理点击关闭和添加按钮,Selecting以防止选择最后一个标签,HandleCreated来调整标签宽度。您可以在下面看到所有实施设置和代码。

<强>初始化

设置填充和DrawMode并为DrawItemMouseDownSelectingHandleCreated事件分配事件处理程序。

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;

处理单击关闭按钮并添加按钮

您可以处理MouseDownMouseClick事件,并检查最后一个标签矩形是否包含鼠标单击的点,然后在最后一个标签前插入一个标签。其他检查其中一个关闭按钮是否包含单击的位置,然后关闭其单击关闭按钮的选项卡:

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;
            }
        }
    }
}

防止选择最后一个标签

为防止选择最后一个标签,您可以处理Selecting控制事件并检查选择标签是否是最后一个标签,取消该事件:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPageIndex == this.tabControl1.TabCount - 1)
        e.Cancel = true;
}

绘制关闭按钮和添加按钮

要绘制关闭按钮并添加按钮,您可以处理DrawItem事件。我使用这些图标添加Add并关闭Close按钮。

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);
    }
}

调整标签宽度

要调整标签宽度并让最后一个标签宽度较小,您可以处理HandleCreated事件并向控件发送TCM_SETMINTABWIDTH并指定标签宽度允许的最小尺寸:

[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);
}

下载

您可以在此处下载代码或克隆存储库:

答案 1 :(得分:1)

通常,直接,&#34;低级&#34;做这样的事情的方法是处理Paint事件并绘制到TabControl本身,然后处理鼠标输入事件以检测你绘制的点击。

然而,a)痛苦,以及b)TabControl抑制了Paint事件,所以如果不进行更低级别的处理,它就无法处理使用WM_PAINT方法覆盖WndProc()消息。

出于您的目的,我建议您只需添加一个新控件,例如一个ButtonForm,将其放在您希望用户点击的TabControl上的位置上。然后在Button.Click事件处理程序中,您可以根据需要添加新页面。如果要封装ButtonTabControl的组合,可以使用UserControl

例如:

<强> 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;
}

<强> TabControlWithAdd.cs:

public partial class TabControlWithAdd : UserControl
{
    public TabControlWithAdd()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tabControl1.TabPages.Add("Tab " + (tabControl1.TabPages.Count + 1));
    }
}

以上使用Button,但您当然可以使用您喜欢的任何其他可点击控件,包括Label(例如,如果您不想要按钮边框外观),则可以使用你想要的视觉效果。

答案 2 :(得分:0)

另一种方法是创建一个新的TabControl,它扩展了TabControl类。我曾经遇到过同样的问题,这就是我做的方式,我无法找到完成的代码,但这可以在您的标签中添加X,同样可以应用于{{1签名:

+

您可以尝试稍微修改它,直到它符合您的需要。