簡體   English   中英

通過菜單條按鈕打開的標簽頁,但通過標簽頁上的圖像關閉 header

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

我有主窗體,其中有 tabcontrol 和 menustrip。 在 menustrip 中,有幾個按鈕應該打開 tabcontrol 中具有定義名稱的選項卡頁面和用自己的內容填充它的透視圖。 但是關閉標簽頁的function應該也存在。 我的想法不是使用不同的按鈕,而是使用標簽頁 header 上的特殊按鈕。顯然,它會是一些帶有交叉圖像的圖片。

我使用了 tabcontrol DrawMode=OwnerDrawFixed 的屬性。 header標簽頁中沒有圖片,盡管目錄和文件名都是正確的。 它位於exe文件所在的文件夾中。 header 中也沒有 label,而 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 Docking / 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/氪

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM