WinTheme Class
public class WinTheme
{
//Extern methods
[DllImport("uxtheme.dll", EntryPoint = "#95")]
private static extern uint GetImmersiveColorFromColorSetEx(uint dwImmersiveColorSet, uint dwImmersiveColorType,
bool bIgnoreHighContrast, uint dwHighContrastCacheMode);
[DllImport("uxtheme.dll", EntryPoint = "#96")]
private static extern uint GetImmersiveColorTypeFromName(IntPtr pName);
[DllImport("uxtheme.dll", EntryPoint = "#98")]
private static extern int GetImmersiveUserColorSetPreference(bool bForceCheckRegistry, bool bSkipCheckOnFail);
//Public methods
public static Color GetAccentColor()
{
var userColorSet = GetImmersiveUserColorSetPreference(false, false);
var colorType = GetImmersiveColorTypeFromName(Marshal.StringToHGlobalUni("ImmersiveStartSelectionBackground"));
var colorSetEx = GetImmersiveColorFromColorSetEx((uint)userColorSet, colorType, false, 0);
return ConvertDWordColorToRGB(colorSetEx);
}
//Private methods
private static Color ConvertDWordColorToRGB(uint colorSetEx)
{
byte redColor = (byte)((0x000000FF & colorSetEx) >> 0);
byte greenColor = (byte)((0x0000FF00 & colorSetEx) >> 8);
byte blueColor = (byte)((0x00FF0000 & colorSetEx) >> 16);
//byte alphaColor = (byte)((0xFF000000 & colorSetEx) >> 24);
return Color.FromArgb(redColor, greenColor, blueColor);
}
}
Form Design
Form Code
public partial class Form1 : Form
{
private UserPreferenceChangedEventHandler UserPreferenceChanged;
public Form1()
{
InitializeComponent();
LoadTheme();
UserPreferenceChanged = new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
SystemEvents.UserPreferenceChanged += UserPreferenceChanged;
this.Disposed += new EventHandler(Form_Disposed);
}
private void LoadTheme()
{
var themeColor = WinTheme.GetAccentColor();//Windows Accent Color
var lightColor = ControlPaint.Light(themeColor);
var darkColor = ControlPaint.Dark(themeColor);
panelTitleBar.BackColor = themeColor;
panelWelcome.BackColor = lightColor;
panelLine.BackColor = lightColor;
rjCircularPictureBox1.BorderColor = themeColor;
rjCircularPictureBox1.BorderColor2 = darkColor;
labelVersion.ForeColor = lightColor;
rjButton7.BackColor = darkColor;
//Buttons
foreach (Button button in this.Controls.OfType<Button>())
{
button.BackColor = themeColor;
}
foreach (Button button in this.panelMenu.Controls.OfType<Button>())
{
button.FlatAppearance.MouseOverBackColor = themeColor;
button.FlatAppearance.MouseDownBackColor = lightColor;
}
}
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category == UserPreferenceCategory.General || e.Category == UserPreferenceCategory.VisualStyle)
{
LoadTheme();
}
}
private void Form_Disposed(object sender, EventArgs e)
{
SystemEvents.UserPreferenceChanged -= UserPreferenceChanged;
}
}
Video Tutorial