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?
4 Answers
2
votes
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.
2
votes
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
votes