简体   繁体   中英

Custom tab control in windows forms

I have faced with strange behavior of tab control in Windows XP and in Windows 7. In XP text that lay on tab is wrapped and in 7 is no. Whats the matter with tab? 在此处输入图片说明

PS. non English letters are wrapped

UPDATE: this is a custom tab control

Here is the code:

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

Why this happens? I'm not sure... I believe the Framework just uses the underlying Windows API, so if it is different between Windows versions, a .Net application might look/behave slightly differently. I wonder if there is a culture/string behavior setting somewhere that might take care of this...

Anyways, here is my sloppy-but-effective solution to your problem: Just insert a line break into the text of your tab (I don't know if you can do this from the designer). You can do this from your own C# code or from the designer's code-behind C#. You also must make the tab size bigger in order to accommodate for the additional text line.

        // 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"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM