Pregunta

Tengo una aplicación WinForm que tiene tabcontrols que son 3 capas de profundidad. Estoy dinámicamente colorear las pestañas con la clase de abajo. Cuando se va a colorear un tabcontrol incrustado se lanza un ataque.

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

¿Necesito hacer algo diferente para aquellos? Si comento hacia fuera las formas incorporado y llamadas a tabRenderer entonces no consigo estos errores. ¿No estoy disponiendo de mi objeto TabRenderer correctamente?

¿Es tal vez algo completamente distinto? La forma en que estoy embeding los controles de pestañas?

Un ejemplo de lo que mi programa actualmente parece que es aquí ->


(fuente: ggpht.com )
De DevFiles

Como se puede ver hay 3 capas de controles de pestañas. Esto ocurre dos veces en el programa y a la vez causa el error mencionado. Hay 6 llamadas a tabRenderer en total, ya que hay 5 controles de pestañas. 1 del nivel superior, 3 de segundo nivel y tercer nivel 2.

El código se utiliza para colorear los controles de pestañas:

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

Y así es como yo lo llamo:

        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);
    }
¿Fue útil?

Solución

Está bien, respondí a mi propia pregunta. (Un poco)

Creo que lo que sucedía era que a medida que mis cargas de aplicaciones que empieza a disparar cada evento Formas de carga () que a su vez los incendios de que se haya insertado Formas de carga evento () y así sucesivamente. Había tirado a mi llamada a TabRenderer en el evento de carga y es algo que no entiendo que estaba ocurriendo. Saqué que llamar a una función PaintTabs () y luego esperar a que el primero en terminar antes de que llama al siguiente (creo?).

De cualquier manera ya no genera ningún error. Tiene ahora la palabra como si fuera lo que desde el NIVEL SUPERIOR TabControl:

        public void PaintTabs()
    {
        new psTabRenderer(tclWWCModuleHost, Color.LightSteelBlue, Color.Khaki, Color.Black, Color.Black);
        FrmWwcMemberHost.PaintTabs();
        FrmWwcMcpHost.PaintTabs();
        FrmCaseNotes.PaintTabs();
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top