5

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?

Ksempac
  • 1,842
  • 2
  • 18
  • 23
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168

5 Answers5

2

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.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
  • I found the visual form editor started adding code to set the BackColor to Color.Transparent which errored at runtime. To get around this I added the following `public new Color BackColor { get { return base.BackColor; } set { if (value != Color.Transparent) { base.BackColor = value; } } }` – Colin Mar 13 '15 at 09:51
2

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.

1

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

Phil Price
  • 2,283
  • 20
  • 22
  • I was hoping to use built-in controls to keep dependencies down. I can't understand why the built-in control doesn't support visual styles. – Jon Tackabury Mar 17 '09 at 03:20
0
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();
    }
}
NoMoreFood
  • 21
  • 1
-2

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.)

RenniePet
  • 11,420
  • 7
  • 80
  • 106