I have a Forms application that has a TrackBar on a form. After the TrackBar instance is created I remove its window theme by:
SetWindowTheme(trackBar.Handle, string.Empty, string.Empty);
That gives the control a basic look. At some point I change the TickStyle property to some other value and that reverts the control's (TrackBar) theme to the default Windows theme and changes the outlook of the control. It does not matter if I change back to the original value of TickStyle. The theme is changed and the control looks differently even with the originial TickStyle value. Any ideas why that happens and how to preserve the basic look of the control?
The following code demonstrates the problem:
Of course I can call ResetVisualStyle right after setting the TickStyle, but this is not applicable in my case.public partial class Form1 : Form { // Pinvoke declaration of SetWindowTheme function used to reset visual style for Forms components [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)] static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList);
public Form1() { InitializeComponent(); Form1.ResetVisualStyle(trackBar1); } static void ResetVisualStyle(Control c) { // applying empty theme to control int res = SetWindowTheme(c.Handle, string.Empty, string.Empty); } private void button1_Click(object sender, EventArgs e) { // preserve the old style
TickStyle oldStyle = trackBar1.TickStyle;
// change the default style. This actually reverts back the window theme - weird!
trackBar1.TickStyle = TickStyle.TopLeft;
// restore the original tick style. Now you see how the outlook of the control changed from the original.
trackBar1.TickStyle = oldStyle;
} }
Regards,
Pavel