public static void SaveSetting(ButtonEffect effect, ColorMatrix cm, int FontColour) { string key = AppController.ApplicationRegistryKeyName; string subkey = effect.ToString(); RegistryKey reg = Registry.CurrentUser.CreateSubKey(key + @"\UserColours\" + subkey); if (reg == null) return; reg.SetValue("FontColour", FontColour); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { string name = "Matrix" + i.ToString(System.Globalization.CultureInfo.InvariantCulture) + j.ToString(System.Globalization.CultureInfo.InvariantCulture); object value = cm.GetType().GetProperty(name).GetValue(cm, null); // Console.WriteLine("i: {0}, j: {1}, value: {2}", i, j, value); reg.SetValue(name, (float)System.Decimal.Parse(value.ToString(), System.Globalization.CultureInfo.InvariantCulture)); } } RaiseColoursChangedEvent(); }
private void RandomizeButtonClick(object sender, EventArgs e) { ColorMatrix cm = new ColorMatrix(); int numberOfChanges = 5; Random r = new Random(); for (int i = 0; i < numberOfChanges; i++) { int x = r.Next(0, 5); int y = r.Next(0, 3); string name = "Matrix" + x.ToString(CultureInfo.InvariantCulture) + y.ToString(CultureInfo.InvariantCulture); float val = (r.Next(-10, 11) / 10F); // Update control... // (this.Controls[name] as NumericUpDown).Value = val; // Or update field. cm.GetType().GetProperty(name).SetValue(cm, val, null); } _fontColour = Color.FromArgb(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)); UpdateMatrix(cm); SaveSetting(); }
private static UserColourSetting GetColourSettingFromRegistry(ButtonEffect effect) { // Need to be defensively minded as user could change, // delete, or change type of registry settings. string subkey = AppController.ApplicationRegistryKeyName + @"\UserColours\" + effect.ToString(); RegistryKey reg = Registry.CurrentUser.OpenSubKey(subkey); if (reg == null) // No settings have been defined for this effect return null; UserColourSetting setting = new UserColourSetting(); // User may have changed type of FontColour // Using nullable int as any possible integer value could be a valid // ToARGB() result (well, I'm assuming it could anyway) int? fontColourArgb; object value = reg.GetValue("FontColour"); if (value == null || reg.GetValueKind("FontColour") != RegistryValueKind.DWord) fontColourArgb = Color.Black.ToArgb(); else fontColourArgb = (int?)value; setting.FontColour = Color.FromArgb((int)fontColourArgb); ColorMatrix cm = new ColorMatrix(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { string name = "Matrix" + i.ToString(System.Globalization.CultureInfo.InvariantCulture) + j.ToString(System.Globalization.CultureInfo.InvariantCulture); value = reg.GetValue(name); if (value != null) { Single svalue; if (System.Single.TryParse(value.ToString(), out svalue)) { cm.GetType().GetProperty(name).SetValue(cm, svalue, null); } } } } setting.Matrix = cm; return setting; }