定制的绘图(多层)嵌入TabControls错误
-
23-08-2019 - |
题
我有一个winform应用程序,有tabcontrols是3层的深度。我是动态的色彩标签与以下类。当它关系到一个嵌入式颜色卡这球场的一个合适的。
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll
我需要做一些不同的东西对于那些?如果我出评论的嵌入式的呼吁tabRenderer然后我没有得到这些错误。我不处置我的TabRenderer对象是否正确?
是吗,也许完全不同的东西?我的方式隐藏于公开的选项控制?
一个例子是我的程序目前看起来像是在这里-->
(资料来源: ggpht.com)
从 DevFiles
正如你可以看到有3层的卡控制。这发生两次的程序,两者导致所提到的错误。有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);
}
解决方案
好吧,我回答我自己的问题。(有点)
我相信发生了什么事是,当我的应用程序加载时它开始发射了这反过来它火灾内容内嵌形式的load()事件等各种形式的load()事件。我扔掉我的电话给TabRenderer负载事件和东西,我不明白发生了什么。我把那个叫出来一个PaintTabs()函数,然后等待第一个完成它调用下之前(我想?)。
无论哪种方式,不再产生任何错误。我现在请它像这样从顶层的TabControl:
public void PaintTabs()
{
new psTabRenderer(tclWWCModuleHost, Color.LightSteelBlue, Color.Khaki, Color.Black, Color.Black);
FrmWwcMemberHost.PaintTabs();
FrmWwcMcpHost.PaintTabs();
FrmCaseNotes.PaintTabs();
}
不隶属于 StackOverflow