通过菜单栏按钮打开的标签页,但通过标签页上的图像关闭 header

Tabpage that opens by menustrip button, but closes by the image on the tabpage header

我有主窗体,其中有 tabcontrol 和 menustrip。在 menustrip 中,有几个按钮应该打开 tabcontrol 中具有定义名称的选项卡页面和用自己的内容填充它的透视图。但是关闭标签页的功能应该也存在。我的想法不是使用不同的按钮,而是使用标签页上的特殊按钮header。很明显,它应该是一些带有交叉图像的图片。

我使用了 属性 的 tabcontrol DrawMode=OwnerDrawFixed。尽管目录和文件名正确,但标签页header中没有图片。它位于exe文件所在的文件夹中。 header 中也没有标签,而 header 的宽度证明文本存在但不可见。

public partial class Core : Form
{
    private string closeButtonFullPath = "button-close.png";

    public Core()
    {
        InitializeComponent();
    }

     //  Do not forget this namespace or else DllImport won't work       
    [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);
    }

    private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
    {
        try
        {
            var tabPage = this.tabControl1.TabPages[e.Index];
            var tabRect = this.tabControl1.GetTabRect(e.Index);
            tabRect.Inflate(-2, -2);
            var closeImage = new Bitmap(closeButtonFullPath);
            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);
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }

    private void списокПерсонToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string texttab = "Список персон";

        TabPage ListPersons = new TabPage(texttab);
        tabControl1.TabPages.Add(ListPersons);
    }

    private void списокОрганізаційToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TabPage tp = new TabPage();
        tp.Name = "Tab_OrganizationList";
        tabControl1.TabPages.Add(tp);
    }

    private void tabControl1_MouseDown(object sender, MouseEventArgs e)
    {
        for (var i = 0; i < this.tabControl1.TabPages.Count - 1; i++)
        {
            var tabRect = this.tabControl1.GetTabRect(i);
            tabRect.Inflate(-2, -2);
            var closeImage = new Bitmap(closeButtonFullPath);
            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;
            }
        }
    }
}

您描述的不是 TabPage 功能,而是 Window 停靠/MDI。

其中每个文档(页面)实际上是一个单独的表单实例,可以关闭、隐藏等

这是一个简单的 MDI window 创建指南 https://www.c-sharpcorner.com/UploadFile/84c85b/building-mdi-winforms-application-using-C-Sharp/

如果你想要对接和更多高级功能,那么你需要使用 3PP 库。我建议检查这些:

https://github.com/dockpanelsuite/dockpanelsuite

https://github.com/ComponentFactory/Krypton