Golan
Golan

Reputation:

TabControl transparent background

I'd like to know if there's some way to render the background of a TabControl transparent (the TabControl, not the TabPages), instead of taking the parent form background color. I've some forms with custom painting where I draw a gradient as the background but this gradient doesn't paint behind the tabcontrols. I tried setting TabControl.Backcolor = Color.Transparent but it tells me it's not supported. I'm using VS2005 and framework 2.0. (set style does not help) Does anyone have good workaround to this issue?

Upvotes: 2

Views: 6414

Answers (4)

Larry
Larry

Reputation: 18041

As explained on MSDN, you should

  • Check if Application.RenderWithVisualStyles returns true
  • Have the TabPage's UseVisualStyleBackColor property set to true
  • Have the TabControl Appearance property set to Normal

then your TabPage should have a transparent background.

Upvotes: 1

Golan
Golan

Reputation:

custom tab conrol:

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

Upvotes: 3

Sinatr
Sinatr

Reputation: 21998

I am going to extend answer of Golan (as he is inactive?)

You can use DrawThemeParentBackground to do most of job. Create custom 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 every TabPage BackColor=Transparent (in Designer or during run-time, if you don't - TabPage's will have white background). Works like a miracle, transparent flicker-free TabControl which I was dreaming for.

Upvotes: 1

JYelton
JYelton

Reputation: 36522

According to this thread on msdn, the tabcontrol does not support changing the backcolor to transparent, however you can supposedly override the drawitem method.

Upvotes: 1

Related Questions