Last active
August 29, 2015 14:02
-
-
Save tfwio/efe9f1144ede535eba0f to your computer and use it in GitHub Desktop.
windows csharp registry reader/writer snippit. How to implement would be to add a call to LocalMuiSettings.Assert() after the window's call to InitializeComponent().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Windows.Media; | |
using FntSize = FirstFloor.ModernUI.Presentation.FontSize; | |
namespace genericwav | |
{ | |
static class LocalMuiSettings | |
{ | |
static public void Assert() | |
{ | |
string localX = LocalMuiSettings.MuiColorTheme; | |
if (localX!=null) { | |
Color c = ColorUtil.ColorFromHex(localX); | |
FirstFloor.ModernUI.Presentation.AppearanceManager.Current.AccentColor = c; | |
} | |
localX = LocalMuiSettings.MuiFontSize; | |
if (localX!=null) { | |
FirstFloor.ModernUI.Presentation.AppearanceManager.Current.FontSize = localX == "large" ? FntSize.Large : FntSize.Small; | |
} | |
localX = LocalMuiSettings.MuiTheme; | |
if (localX!=null) { | |
FirstFloor.ModernUI.Presentation.AppearanceManager.Current.ThemeSource = new Uri(localX,UriKind.RelativeOrAbsolute); | |
} | |
} | |
#region Registry | |
// root registry path for your app | |
const string regpath = @"Software\tfwio\generator-wpf"; | |
// a key --- I only use string values FYI | |
const string MUI_COLOR = "mui-color"; // such as light or dark | |
const string MUI_SIZE = "mui-size"; | |
const string MUI_THEME = "mui-theme"; | |
/// <summary> | |
/// Set/Get the MuiTheme setting (Light/Dark) | |
/// </summary> | |
static public string MuiColorTheme | |
{ | |
get { return Reg.GetKeyValueString(regpath, MUI_COLOR); } | |
set { Reg.SetKeyValueString(regpath, MUI_COLOR, value); } | |
} | |
/// <summary> | |
/// Set/Get the MuiTheme setting | |
/// </summary> | |
static public string MuiFontSize | |
{ | |
get { return Reg.GetKeyValueString(regpath, MUI_SIZE); } | |
set { Reg.SetKeyValueString(regpath, MUI_SIZE, value); } | |
} | |
/// <summary> | |
/// Set/Get the MuiTheme setting | |
/// </summary> | |
static public string MuiTheme | |
{ | |
get { return Reg.GetKeyValueString(regpath, MUI_THEME); } | |
set { Reg.SetKeyValueString(regpath, MUI_THEME, value); } | |
} | |
#endregion | |
} | |
static class ColorUtil | |
{ | |
static public int IntFromHexColor(string color) | |
{ | |
Color c = (Color)ColorConverter.ConvertFromString(color); | |
return (c.A << 24) + (c.R << 16) + (c.G << 8) + c.B; | |
} | |
static public Color ColorFromHex(string hexString) | |
{ | |
if (hexString[0] != '#') return (Color)ColorConverter.ConvertFromString(String.Concat("#",hexString)); | |
return (Color)ColorConverter.ConvertFromString(hexString); | |
} | |
/// <summary> | |
/// For RGBA in hex | |
/// </summary> | |
/// <param name="c"></param> | |
/// <param name="useHash"></param> | |
/// <returns></returns> | |
static public string ColorToHex8(Color c, bool useHash) | |
{ | |
return string.Format(useHash ? "{0:X8}" : "{0:X8}", (c.A << 24) + (c.R << 16) + (c.G << 8) + c.B); | |
} | |
static public string ColorToHex8(Color c) | |
{ | |
return ColorToHex8(c,true); | |
} | |
/// <summary> | |
/// Here, the alpha value is ignored. | |
/// </summary> | |
/// <param name="c"></param> | |
/// <param name="useHash"></param> | |
/// <returns></returns> | |
static public string ColorToHex6(Color c, bool useHash) | |
{ | |
return string.Format(useHash ? "#{0:X6}" : "{0:X6}", (c.R << 16) + (c.G << 8) + c.B); | |
} | |
} | |
static class Reg | |
{ | |
/// <summary> | |
/// Current User | |
/// </summary> | |
/// <param name="path"></param> | |
/// <param name="key"></param> | |
/// <returns></returns> | |
static public string GetKeyValueString(string path, string key) | |
{ | |
string value = null; | |
using (Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path, false)) | |
{ | |
if (rkey == null) return null; | |
value = string.Format("{0}", rkey.GetValue(key)); | |
rkey.Close(); | |
} | |
return value; | |
} | |
static public void SetKeyValueString(string path, string key, object value) | |
{ | |
Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path, true); | |
if (rkey == null) | |
{ | |
rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(path, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree); | |
} | |
if (value != null) rkey.SetValue(key, value, Microsoft.Win32.RegistryValueKind.String); | |
rkey.Close(); | |
rkey = null; | |
GC.Collect(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a class that is common to the MUI demo application. | |
* All we've done is add three calls in the three properties which | |
* sets a value to the windows CU registry. | |
*/ | |
/// <summary> | |
/// A simple view model for configuring theme, font and accent colors. | |
/// </summary> | |
public class SettingsAppearanceViewModel | |
: NotifyPropertyChanged | |
{ | |
private const string FontSmall = "small"; | |
private const string FontLarge = "large"; | |
// 9 accent colors from metro design principles | |
/*private Color[] accentColors = new Color[]{ | |
Color.FromRgb(0x33, 0x99, 0xff), // blue | |
Color.FromRgb(0x00, 0xab, 0xa9), // teal | |
Color.FromRgb(0x33, 0x99, 0x33), // green | |
Color.FromRgb(0x8c, 0xbf, 0x26), // lime | |
Color.FromRgb(0xf0, 0x96, 0x09), // orange | |
Color.FromRgb(0xff, 0x45, 0x00), // orange red | |
Color.FromRgb(0xe5, 0x14, 0x00), // red | |
Color.FromRgb(0xff, 0x00, 0x97), // magenta | |
Color.FromRgb(0xa2, 0x00, 0xff), // purple | |
};*/ | |
// 20 accent colors from Windows Phone 8 | |
private Color[] accentColors = new Color[]{ | |
Color.FromArgb(0xFF,166,205, 95), // lime 0 | |
Color.FromRgb(0xa4, 0xc4, 0x00), // lime | |
Color.FromRgb(0x60, 0xa9, 0x17), // green | |
Color.FromRgb(0x00, 0x8a, 0x00), // emerald | |
Color.FromRgb(0x00, 0xab, 0xa9), // teal | |
Color.FromRgb(0x1b, 0xa1, 0xe2), // cyan | |
Color.FromRgb(0x00, 0x50, 0xef), // cobalt | |
Color.FromRgb(0x6a, 0x00, 0xff), // indigo | |
Color.FromRgb(0xaa, 0x00, 0xff), // violet | |
Color.FromRgb(0xf4, 0x72, 0xd0), // pink | |
Color.FromRgb(0xd8, 0x00, 0x73), // magenta | |
Color.FromRgb(0xa2, 0x00, 0x25), // crimson | |
Color.FromRgb(0xe5, 0x14, 0x00), // red | |
Color.FromRgb(0xfa, 0x68, 0x00), // orange | |
Color.FromRgb(0xf0, 0xa3, 0x0a), // amber | |
Color.FromRgb(0xe3, 0xc8, 0x00), // yellow | |
Color.FromRgb(0x82, 0x5a, 0x2c), // brown | |
Color.FromRgb(0x6d, 0x87, 0x64), // olive | |
Color.FromRgb(0x64, 0x76, 0x87), // steel | |
Color.FromRgb(0x76, 0x60, 0x8a), // mauve | |
Color.FromRgb(0x87, 0x79, 0x4e), // taupe | |
}; | |
private Color selectedAccentColor; | |
private LinkCollection themes = new LinkCollection(); | |
private Link selectedTheme; | |
private string selectedFontSize; | |
public SettingsAppearanceViewModel() | |
{ | |
// add the default themes | |
this.themes.Add(new Link { DisplayName = "dark", Source = AppearanceManager.DarkThemeSource }); | |
this.themes.Add(new Link { DisplayName = "light", Source = AppearanceManager.LightThemeSource }); | |
// add additional themes | |
// this.themes.Add(new Link { DisplayName = "bing image", Source = new Uri("/ModernUIDemo;component/Assets/ModernUI.BingImage.xaml", UriKind.Relative) }); | |
// this.themes.Add(new Link { DisplayName = "hello kitty", Source = new Uri("/ModernUIDemo;component/Assets/ModernUI.HelloKitty.xaml", UriKind.Relative) }); | |
// this.themes.Add(new Link { DisplayName = "love", Source = new Uri("/ModernUIDemo;component/Assets/ModernUI.Love.xaml", UriKind.Relative) }); | |
// this.themes.Add(new Link { DisplayName = "snowflakes", Source = new Uri("/ModernUIDemo;component/Assets/ModernUI.Snowflakes.xaml", UriKind.Relative) }); | |
this.SelectedFontSize = AppearanceManager.Current.FontSize == FontSize.Large ? FontLarge : FontSmall; | |
SyncThemeAndColor(); | |
AppearanceManager.Current.PropertyChanged += OnAppearanceManagerPropertyChanged; | |
} | |
private void SyncThemeAndColor() | |
{ | |
// synchronizes the selected viewmodel theme with the actual theme used by the appearance manager. | |
this.SelectedTheme = this.themes.FirstOrDefault(l => l.Source.Equals(AppearanceManager.Current.ThemeSource)); | |
// and make sure accent color is up-to-date | |
this.SelectedAccentColor = AppearanceManager.Current.AccentColor; | |
} | |
private void OnAppearanceManagerPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
if (e.PropertyName == "ThemeSource" || e.PropertyName == "AccentColor") { | |
SyncThemeAndColor(); | |
} | |
} | |
public LinkCollection Themes | |
{ | |
get { return this.themes; } | |
} | |
public string[] FontSizes | |
{ | |
get { return new string[] { FontSmall, FontLarge }; } | |
} | |
public Color[] AccentColors | |
{ | |
get { return this.accentColors; } | |
} | |
public Link SelectedTheme | |
{ | |
get { return this.selectedTheme; } | |
set | |
{ | |
if (this.selectedTheme != value) { | |
this.selectedTheme = value; | |
OnPropertyChanged("SelectedTheme"); | |
LocalMuiSettings.MuiTheme=value.Source.ToString(); | |
// and update the actual theme | |
AppearanceManager.Current.ThemeSource = value.Source; | |
} | |
} | |
} | |
public string SelectedFontSize | |
{ | |
get { return this.selectedFontSize; } | |
set | |
{ | |
if (this.selectedFontSize != value) { | |
this.selectedFontSize = value; | |
OnPropertyChanged("SelectedFontSize"); | |
LocalMuiSettings.MuiFontSize=value; | |
AppearanceManager.Current.FontSize = value == FontLarge ? FontSize.Large : FontSize.Small; | |
} | |
} | |
} | |
public Color SelectedAccentColor | |
{ | |
get { return this.selectedAccentColor; } | |
set | |
{ | |
if (this.selectedAccentColor != value) { | |
this.selectedAccentColor = value; | |
OnPropertyChanged("SelectedAccentColor"); | |
LocalMuiSettings.MuiColorTheme = ColorUtil.ColorToHex8(value); | |
AppearanceManager.Current.AccentColor = value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment