我想知道是否有某种方法可以将TabControl的背景透明化(TabControl,而不是TabPages),而不是采用父窗体背景颜色。我有一些自定义绘画的形式,我绘制一个渐变作为背景,但这个渐变不会在tabcontrols后面绘制。我尝试设置TabControl.Backcolor = Color.Transparent,但它告诉我它不受支持。我正在使用VS2005和framework 2.0。 (设置样式没有帮助) 有没有人对这个问题有很好的解决方法?
答案 0 :(得分:3)
自定义标签控件:
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
protected override void OnPaintBackground(PaintEventArgs e)
{
if (this.BackColor == Color.Transparent)
{
IntPtr hdc = e.Graphics.GetHdc();
Rectangle rec = new Rectangle(e.ClipRectangle.Left,
e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height);
DrawThemeParentBackground(this.Handle, hdc, ref rec);
e.Graphics.ReleaseHdc(hdc);
}
else
{
base.OnPaintBackground(e);
}
}
答案 1 :(得分:1)
根据this thread on msdn,tabcontrol不支持将背景颜色更改为透明,但您可以覆盖drawitem方法。
答案 2 :(得分:1)
我将扩展Golan
的回答(因为他不活跃?)
您可以使用DrawThemeParentBackground完成大部分工作。创建自定义TabControl:
[System.ComponentModel.DesignerCategory("Code")]
public class MyTabControl : TabControl
{
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
// use with care, as it may cause strange effects
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
public MyTabControl() { }
protected override void OnPaintBackground(PaintEventArgs e)
{
IntPtr hdc = e.Graphics.GetHdc();
Rectangle rect = ClientRectangle;
DrawThemeParentBackground(this.Handle, hdc, ref rect);
e.Graphics.ReleaseHdc(hdc);
}
}
并为每个TabPage
BackColor=Transparent
明确设置(在Designer中或在运行时,如果不这样做 - TabPage
将具有白色背景)。就像一个奇迹,透明的无闪烁TabControl,我正在梦想。
答案 3 :(得分:1)
作为explained on MSDN,你应该
Application.RenderWithVisualStyles
是否返回true UseVisualStyleBackColor
属性设置为true Appearance
属性设置为Normal
然后你的TabPage应该有一个透明的背景。