簡體   English   中英

Windows表單中的自定義標簽控件

[英]Custom tab control in windows forms

在Windows XP和Windows 7中,我曾遇到過選項卡控件的奇怪行為。在XP中,位於選項卡上的文本被換行,而在7中則為否。 標簽有什么關系? 在此處輸入圖片說明

PS。 非英文字母包裝

更新:這是一個自定義選項卡控件

這是代碼:

protected override void OnPaint(PaintEventArgs e)
    {
        DrawControl(e.Graphics);
    }

internal void DrawControl(Graphics g)
    {
        if (!Visible)
            return;

        Brush br = new SolidBrush(clr_cl_area);
        Brush brTab = new SolidBrush(clr_client);

        Rectangle TabControlArea = ClientRectangle;
        Rectangle TabArea = DisplayRectangle;

        g.FillRectangle(br, TabControlArea);
        g.FillRectangle(brTab, TabArea);

        br.Dispose();
        brTab.Dispose();

        for (int i = 0; i < TabCount; i++)
            DrawTab(g, TabPages[i], i, false);

        if (_mouseTabIndex != null && _mouseTabIndex != _mouseTabIndexSave && _mouseTabIndex != SelectedIndex)
            DrawTab(g, TabPages[(int)_mouseTabIndex], (int)_mouseTabIndex, true);

        _mouseTabIndexSave = _mouseTabIndex;

    }

    internal void DrawTab(Graphics g, TabPage tabPage, int nIndex, bool mouseOverTab)
    {

        var recBounds = GetTabRect(nIndex);


        SetBounds(ref recBounds);
        var pt = SetPointsForTabFill(recBounds);


        DrawTabBounds(g, recBounds);


        FillTabl(g, recBounds, pt, false);


        DrawTabSeparators(g, recBounds, nIndex, 0 /*y-bottom*/);


        if (SelectedIndex == nIndex) 
        {    
            DrawTabGradient(g, recBounds, pt, nIndex,0/*width*/,1/*height*/);  
            DrawTabSeparators(g, recBounds, nIndex, 1 /*y-bottom*/); 
        }

        if (mouseOverTab)
            DrawTabGradient(g, recBounds, pt, nIndex, -2/*width*/, 0/*height*/);

        DrawText(g, recBounds, tabPage.Text);

    }

    private void DrawText(Graphics g, Rectangle recBounds, string text)
    {
        var strFormat = new StringFormat();
        strFormat.Alignment = strFormat.LineAlignment = StringAlignment.Center;

        g.TextRenderingHint =
            System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

        //var fnt = new Font(MsFonts.familyPTSans, 8F, FontStyle.Regular,  GraphicsUnit.Point, (byte)204);
        var fnt = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(204)));

        RectangleF tabTextArea = recBounds;
        var br = new SolidBrush(clr_txt);
        g.DrawString(text, fnt, br, tabTextArea, FormatString());

        br.Dispose();
        strFormat.Dispose();
        fnt.Dispose();
    }

為什么會這樣? 我不確定...我相信Framework僅使用基礎Windows API,因此,如果Windows版本之間有所不同,.Net應用程序的外觀可能會略有不同。 我想知道是否有某個地方可以解決此問題的文化/字符串行為設置...

無論如何,這是我為您的問題草率但有效的解決方案:只需在選項卡的文本中插入一個換行符即可(我不知道您是否可以從設計器中做到這一點)。 您可以從您自己的C#代碼或設計器背后的C#代碼中執行此操作。 您還必須使制表符大小更大,以便容納其他文本行。

        // Set the size of the tabs.  I multiply the default height by 2 for 2 lines
        tabControl1.ItemSize = new System.Drawing.Size(77, 18 * 2);

        // Force a line break within the string
        tabControl1.Controls[0].Text = "Hello\r\nWorld"; 

暫無
暫無

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

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