自定义绘图(多层)嵌入式TabControls错误

时间:2009-05-21 19:15:53

标签: c# winforms .net-3.5 tabcontrol ownerdrawn

我有一个winform应用程序,其中包含3层深的tabcontrol。我正在使用下面的类动态着色选项卡。当它为嵌入式tabcontrol着色时,它会适合。

A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll

我需要为那些做些不同的事吗?如果我注释掉tabRenderer的嵌入式表单调用,那么我不会收到这些错误。我没有正确处理我的TabRenderer对象吗?

它可能完全是另类吗?我嵌入标签控件的方式是什么?

我的程序目前的样子就是 - >

http://lh6.ggpht.com/_DqVwi_JXyS0/Sf8EI0ETYrI/AAAAAAAABhI/pmAhozErubo/s288/MATRIXUIExample.JPG来自DevFiles

正如您所看到的,有三层标签控件。这在程序中出现两次,都会导致上述错误。总共有6个调用tabRenderer,因为有5个制表符控件。 1个顶级,3个第二级和2个第三级。

用于对选项卡控件进行着色的代码:

public class psTabRenderer
{
    private TabControl _tabControl;
    private Color _fillColor;
    private Color _selectedFillColor;
    private Color _textColor;
    private Color _selectedTextColor;

    public psTabRenderer(TabControl tabControl, Color fillColor, Color selectedFillColor, Color textColor, Color selectedTextColor)
    {
        _tabControl = tabControl;
        _fillColor = fillColor;
        _selectedFillColor = selectedFillColor;
        _textColor = textColor;
        _selectedTextColor = selectedTextColor;

        _tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
        _tabControl.DrawItem += TabControlDrawItem;
    }

    private void TabControlDrawItem(object sender, DrawItemEventArgs e)
    {
        TabPage currentTab = _tabControl.TabPages[e.Index];
        Rectangle itemRect = _tabControl.GetTabRect(e.Index);
        var fillBrush = new SolidBrush(_fillColor);
        var textBrush = new SolidBrush(_textColor);
        var 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 (Convert.ToBoolean(e.State & DrawItemState.Selected))
        {
            fillBrush.Color = _selectedFillColor;
            textBrush.Color = _selectedTextColor;
            itemRect.Inflate(2, 2);
        }

        //Set up rotation for left and right aligned tabs
        if (_tabControl.Alignment == TabAlignment.Left || _tabControl.Alignment == TabAlignment.Right)
        {
            float rotateAngle = 90;
            if (_tabControl.Alignment == TabAlignment.Left)
                rotateAngle = 270;
            var 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();
    }
}

这就是我所说的:

        private void frmMCPEmployment_Load(object sender, EventArgs e)
    {
        FormPaint();
    }

    public void FormPaint()
    {
        // ToDo: This call to the Tab Renderer is throwing a Win32 "Error Creating Window Handle" 
        new psTabRenderer(tclEmployment, Color.LightSteelBlue, Color.Khaki, Color.Black, Color.Black);
    }

1 个答案:

答案 0 :(得分:0)

好的,我回答了自己的问题。(有点)

我相信发生的事情是,当我的应用程序加载时,它开始触发每个Forms Load()事件,这反过来触发它的嵌入式Forms Load()事件等等。我在加载事件中向TabRenderer抛出了我的调用,而我不明白的事情正在发生。我将该调用拉出到PaintTabs()函数,然后在调用下一个函数之前等待第一个调用(我认为?)。

无论哪种方式,它都不会再产生任何错误。我现在从TOP LEVEL TabControl中调用它:

        public void PaintTabs()
    {
        new psTabRenderer(tclWWCModuleHost, Color.LightSteelBlue, Color.Khaki, Color.Black, Color.Black);
        FrmWwcMemberHost.PaintTabs();
        FrmWwcMcpHost.PaintTabs();
        FrmCaseNotes.PaintTabs();
    }