0

我有一个具有 3 层深的选项卡控件的 winform 应用程序。我正在使用下面的类动态地为选项卡着色。当它为嵌入的 tabcontrol 着色时,它会很合适。

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

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

可能完全是另外一回事吗?我嵌入选项卡控件的方式?

我的程序目前看起来的一个例子在这里 -->


(来源:ggpht.com
来自DevFiles

如您所见,有 3 层选项卡控件。这在程序中发生了两次,都导致了上述错误。因为有 5 个选项卡控件,所以总共有 6 次对 tabRenderer 的调用。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);
    }
4

1 回答 1

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();
    }
于 2009-05-21T20:22:22.810 回答