Jon Tackabury
Jon Tackabury

Reputation: 49249

Trackbar Background in a TabControl

I have a TrackBar control on a TabPage inside a TabControl. The background of the TrackBar is being drawn in grey while the TabPage is being drawn as white. There is no way to set the BackColor property of the TrackBar to transparent, and I can't override the drawing because there is no DrawMode property for the TrackBar. What options do I have to make the TrackBar fit in? Why doesn't it support visual styles?

Upvotes: 5

Views: 7781

Answers (5)

NoMoreFood
NoMoreFood

Reputation: 21

internal class TransparentTrackBar : System.Windows.Forms.TrackBar
{
    protected override void OnCreateControl()
    {
        VisualStyleRenderer oRenderer = new VisualStyleRenderer(
          VisualStyleElement.Tab.Pane.Normal);

        BackColor = oRenderer.GetColor(ColorProperty.FillColorHint);
            base.OnCreateControl();
    }
}

Upvotes: 0

RenniePet
RenniePet

Reputation: 11658

The obvious solution seems to be to set the TrackBar's BackColor to System-ControlLightLight.

(But the problem with an obvious solution to four-year-old question is that it probably implies that I've misunderstood something.)

Upvotes: -2

Shockwaverider
Shockwaverider

Reputation: 41

Wouldn't interfere here, but neither of the above suggestions worked for me. What did the trick were the following lines:

private const int WM_DWMCOMPOSITIONCHANGED = 0x031A;
private const int WM_THEMECHANGED = 0x031E;

protected override void OnVisibleChanged(EventArgs e)
{
    Color color = this.BackColor;
    trackBarQuality.BackColor = Color.FromArgb(color.R, color.G, color.B);
}

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_DWMCOMPOSITIONCHANGED || m.Msg == WM_THEMECHANGED)
        OnVisibleChanged(new EventArgs());

    base.WndProc(ref m);
}

So I basically eliminate the alpha channel from the background color. Still have to test this with Vista and Win 7, though.

Upvotes: 2

Alex from Jitbit
Alex from Jitbit

Reputation: 60782

Simple

class MyTransparentTrackBar : TrackBar
{
    protected override void OnCreateControl()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        if (Parent != null)
            BackColor = Parent.BackColor;

        base.OnCreateControl();
    }
}

I also faced this (needed a transparent-background trackbar on a tab-control, that will work with both visualstyles enabled and disabled). And this worked for me.

Upvotes: 2

Phil Price
Phil Price

Reputation: 2313

You might want to look at the TransparentTrackBar project on CodePlex.

Upvotes: 1

Related Questions