J'ai une application winform avec des contrôles de tabulation de 3 couches de profondeur. Je colorie dynamiquement les onglets avec la classe ci-dessous. Quand il s'agit de colorer un tabcontrol embarqué, il fait un ajustement.Dessin personnalisé de TabControls incorporés (multicouches) Erreur
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll
Dois-je faire quelque chose de différent pour ceux-là? Si je commente les appels de formulaires incorporés à tabRenderer, je n'obtiens pas ces erreurs. Est-ce que je ne dispose pas correctement de mon objet TabRenderer?
Est-ce peut-être autre chose? La façon dont j'embarque les contrôles de l'onglet?
Un exemple de ce que mon programme ressemble actuellement est ici ->
http://lh6.ggpht.com/_DqVwi_JXyS0/Sf8EI0ETYrI/AAAAAAAABhI/pmAhozErubo/s288/MATRIXUIExample.JPG De DevFilesComme vous pouvez le voir, il y a 3 couches de contrôle de l'onglet. Cela se produit deux fois dans le programme et les deux provoquent l'erreur mentionnée. Il y a 6 appels à tabRenderer au total car il y a 5 contrôles de tabulation. 1 niveau supérieur, 3 niveau secondaire et 2 niveau troisième.
Le code utilisé pour colorer les contrôles Tab:
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();
}
}
Et voici comment je l'appelle.
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);
}