0

我在尝试将其提取到类文件中的 TabConttrols DrawItem 事件下有以下代码。我遇到了麻烦,因为它与一个事件有关。任何提示或指针将不胜感激。

        private void tabCaseNotes_DrawItem(object sender, DrawItemEventArgs e)
    {
        TabPage currentTab = tabCaseNotes.TabPages[e.Index];
        Rectangle itemRect = tabCaseNotes.GetTabRect(e.Index);
        SolidBrush fillBrush = new SolidBrush(Color.Linen);
        SolidBrush textBrush = new SolidBrush(Color.Black);
        StringFormat sf = new StringFormat
                              {
                                  Alignment = StringAlignment.Center,
                                  LineAlignment = StringAlignment.Center
                              };

        //If we are currently painting the Selected TabItem we'll
        //change the brush colors and inflate the rectangle.
        if (System.Convert.ToBoolean(e.State & DrawItemState.Selected))
        {
            fillBrush.Color = Color.LightSteelBlue;
            textBrush.Color = Color.Black;
            itemRect.Inflate(2, 2);
        }

        //Set up rotation for left and right aligned tabs
        if (tabCaseNotes.Alignment == TabAlignment.Left || tabCaseNotes.Alignment == TabAlignment.Right)
        {
            float rotateAngle = 90;
            if (tabCaseNotes.Alignment == TabAlignment.Left)
                rotateAngle = 270;
            PointF cp = new PointF(itemRect.Left + (itemRect.Width / 2), itemRect.Top + (itemRect.Height / 2));
            e.Graphics.TranslateTransform(cp.X, cp.Y);
            e.Graphics.RotateTransform(rotateAngle);
            itemRect = new Rectangle(-(itemRect.Height / 2), -(itemRect.Width / 2), itemRect.Height, itemRect.Width);
        }

        //Next we'll paint the TabItem with our Fill Brush
        e.Graphics.FillRectangle(fillBrush, itemRect);

        //Now draw the text.
        e.Graphics.DrawString(currentTab.Text, e.Font, textBrush, (RectangleF)itemRect, sf);

        //Reset any Graphics rotation
        e.Graphics.ResetTransform();

        //Finally, we should Dispose of our brushes.
        fillBrush.Dispose();
        textBrush.Dispose();
    }
4

1 回答 1

1

取决于您要达到的目标。您总是可以将 TabControl 子类化,或者您可以将绘图代码封装在您将 TabControl 传递给的类中。

public class TabRenderer
{
    private TabControl _tabControl;

    public TabRenderer(TabControl tabControl)
    {
        _tabControl = tabControl;
        _tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
        _tabControl.DrawItem += TabControlDrawItem;
    }

    private void TabControlDrawItem(object sender, DrawItemEventArgs e)
    {
        // Your drawing code...
    }
}
于 2009-04-10T15:39:29.500 回答