Transparent TabControl background
I would like to know if there is a way to render the background of a transparent TabControl (TabControl, not TabPages) instead of taking the background color of the parent form. I have some custom painting shapes where I draw a gradient as a background, but this gradient is not drawn behind the tabs. I tried to set TabControl.Backcolor = Color.Transparent but it tells me it is not supported. I am using VS2005 and framework 2.0. (typing style doesn't help) Does anyone have any good ways to solve this problem?
custom tab:
[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);
}
}
According to this thread in msdn , tabcontrol does not support changing the inverse color to transparent, however you could possibly override the drawitem method.
source to share
I am going to extend the answer Golan
(since it is inactive?)
You can use DrawThemeParentBackground for most tasks. Create your own 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);
}
}
and set explicitly for each TabPage
BackColor=Transparent
(in the constructor or at runtime, if you don't - TabPage
will have a white background). Works like a miracle, the transparent shimmery TabControl I've dreamed of.
source to share
As explained on MSDN , you should
- Make sure to
Application.RenderWithVisualStyles
return true - TabPage property
UseVisualStyleBackColor
set to true - The TabControl property is
Appearance
set toNormal
then your tab should have a transparent background.
source to share