问题
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?
回答1:
You might want to look at the TransparentTrackBar project on CodePlex.
回答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.
回答3:
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.
回答4:
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.)
来源:https://stackoverflow.com/questions/652859/trackbar-background-in-a-tabcontrol