Windows窗体中的自定义选项卡控件

时间:2011-09-14 12:08:41

标签: c# winforms custom-controls

我在Windows XP和Windows 7中遇到过标签控件的奇怪行为。在XP上的文本包含在tab上,而7中则没有。选项卡的问题是什么?enter image description here

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

2 个答案:

答案 0 :(得分:1)

为什么会这样?我不确定...我相信框架只使用底层的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"; 

答案 1 :(得分:1)

尝试按照这篇文章。

http://www.codeproject.com/KB/tabs/CustomTabControl.aspx

问候。