Skip to content

Commit

Permalink
Merge pull request #3 from yumdouoliver/master
Browse files Browse the repository at this point in the history
[NEW] Migrate configurations to cfg file
  • Loading branch information
shiqimei authored May 9, 2019
2 parents 0ced5fe + dfe7358 commit 24dcdc6
Show file tree
Hide file tree
Showing 24 changed files with 299 additions and 88 deletions.
Binary file removed .vs/percentage/v15/.suo
Binary file not shown.
Binary file removed .vs/percentage/v15/Server/sqlite3/storage.ide-shm
Binary file not shown.
Binary file removed .vs/percentage/v15/Server/sqlite3/storage.ide-wal
Binary file not shown.
Binary file added .vs/percentage/v16/.suo
Binary file not shown.
File renamed without changes.
Binary file not shown.
97 changes: 97 additions & 0 deletions percentage/PTConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace percentage
{
class PTConfig
{
private string _cfgFile = null;


public string FontSize { get; set; }
public string XOffset { get; set; }
public string YOffset { get; set; }
public string NormalColor { get; set; }
public string ChargingColor { get; set; }
public string LowColor { get; set; }
public string AutoHide { get; set; }


public PTConfig()
{
_cfgFile = AppDomain.CurrentDomain.BaseDirectory + "ptcfg.xml";
}

public bool ConfigExist()
{
if (_cfgFile != null && File.Exists(_cfgFile))
{
try
{
XDocument.Load(_cfgFile);
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}

private void InitConfig()
{
XDocument doc = new XDocument();
XElement root = new XElement("configs");
root.Add(new XElement("fontsize") { Value = "28" });
root.Add(new XElement("xoffset") { Value = "0" });
root.Add(new XElement("yoffset") { Value = "0" });
root.Add(new XElement("normalColor") { Value = "255,255,255" });
root.Add(new XElement("chargingColor") { Value = "254,190,4" });
root.Add(new XElement("lowColor") { Value = "254,97,82" });
root.Add(new XElement("autoHide") { Value = "false" });
doc.Add(root);
doc.Save(_cfgFile);
}

public void Load()
{
if (!ConfigExist())
{
InitConfig();
}
XDocument doc = XDocument.Load(_cfgFile);
XElement root = doc.Root;
FontSize = root.Element("fontsize").Value;
XOffset = root.Element("xoffset").Value;
YOffset = root.Element("yoffset").Value;
NormalColor = root.Element("normalColor").Value;
ChargingColor = root.Element("chargingColor").Value;
LowColor = root.Element("lowColor").Value;
AutoHide = root.Element("autoHide").Value;
}

public void Update()
{
XDocument doc = new XDocument();
XElement root = new XElement("configs");
root.Add(new XElement("fontsize") { Value = FontSize });
root.Add(new XElement("xoffset") { Value = XOffset });
root.Add(new XElement("yoffset") { Value = YOffset });
root.Add(new XElement("normalColor") { Value = NormalColor });
root.Add(new XElement("chargingColor") { Value = ChargingColor });
root.Add(new XElement("lowColor") { Value = LowColor });
root.Add(new XElement("autoHide") { Value = AutoHide });
doc.Add(root);
doc.Save(_cfgFile);
}
}
}
143 changes: 96 additions & 47 deletions percentage/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,37 @@ public partial class Settings : Form
{
public Settings()
{
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn0 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
if (lgn0 == null) //初始化
{
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("xoffset", "0", RegistryValueKind.String);
lgn.SetValue("yoffset", "0", RegistryValueKind.String);
lgn.SetValue("fontsize", "28", RegistryValueKind.String);
lgn.SetValue("normalColor", "255, 255, 255", RegistryValueKind.String);
lgn.SetValue("chargingColor", "254, 190, 4", RegistryValueKind.String);
lgn.SetValue("lowColor", "254, 97, 82", RegistryValueKind.String);
lgn.SetValue("autoHide", "false", RegistryValueKind.String); // 电池电量充满后是否自动隐藏
}
RegistryKey lgn1 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
// 从注册表获取数据
string fontsize = lgn1.GetValue("fontsize").ToString();
string xoffset = lgn1.GetValue("xoffset").ToString();
string yoffset = lgn1.GetValue("yoffset").ToString();
string normalColor = lgn1.GetValue("normalColor").ToString();
string chargingColor = lgn1.GetValue("chargingColor").ToString();
string lowColor = lgn1.GetValue("lowColor").ToString();
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn0 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
//if (lgn0 == null) //初始化
//{
// RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
// lgn.SetValue("xoffset", "0", RegistryValueKind.String);
// lgn.SetValue("yoffset", "0", RegistryValueKind.String);
// lgn.SetValue("fontsize", "28", RegistryValueKind.String);
// lgn.SetValue("normalColor", "255, 255, 255", RegistryValueKind.String);
// lgn.SetValue("chargingColor", "254, 190, 4", RegistryValueKind.String);
// lgn.SetValue("lowColor", "254, 97, 82", RegistryValueKind.String);
// lgn.SetValue("autoHide", "false", RegistryValueKind.String); // 电池电量充满后是否自动隐藏
//}
//RegistryKey lgn1 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
//// 从注册表获取数据
//string fontsize = lgn1.GetValue("fontsize").ToString();
//string xoffset = lgn1.GetValue("xoffset").ToString();
//string yoffset = lgn1.GetValue("yoffset").ToString();
//string normalColor = lgn1.GetValue("normalColor").ToString();
//string chargingColor = lgn1.GetValue("chargingColor").ToString();
//string lowColor = lgn1.GetValue("lowColor").ToString();


PTConfig cfg = new PTConfig();
cfg.Load();
string fontsize = cfg.FontSize;
string xoffset = cfg.XOffset;
string yoffset = cfg.YOffset;
string normalColor = cfg.NormalColor;
string chargingColor = cfg.ChargingColor;
string lowColor = cfg.LowColor;

InitializeComponent(); //绘制控件
textBox1.Text = fontsize;
Expand Down Expand Up @@ -73,10 +83,16 @@ private void textBox1_TextChanged(object sender, EventArgs e)
{
TrayIcon.iconFontSize = Convert.ToInt32(textBox1.Text);
// 将设置的字号保存到注册表中
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("fontsize", textBox1.Text, RegistryValueKind.String);
} catch
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
//lgn.SetValue("fontsize", textBox1.Text, RegistryValueKind.String);

PTConfig cfg = new PTConfig();
cfg.Load();
cfg.FontSize = textBox1.Text;
cfg.Update();
}
catch
{
// do nothing
}
Expand All @@ -87,9 +103,14 @@ private void textBox3_TextChanged(object sender, EventArgs e)
try
{
TrayIcon.xoffset = Convert.ToInt32(textBox3.Text);
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("xoffset", textBox3.Text, RegistryValueKind.String);
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
//lgn.SetValue("xoffset", textBox3.Text, RegistryValueKind.String);

PTConfig cfg = new PTConfig();
cfg.Load();
cfg.XOffset = textBox3.Text;
cfg.Update();
}
catch
{
Expand All @@ -103,9 +124,14 @@ private void textBox4_TextChanged(object sender, EventArgs e)
try
{
TrayIcon.yoffset = Convert.ToInt32(textBox4.Text);
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("yoffset", textBox4.Text, RegistryValueKind.String);
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
//lgn.SetValue("yoffset", textBox4.Text, RegistryValueKind.String);

PTConfig cfg = new PTConfig();
cfg.Load();
cfg.YOffset = textBox4.Text;
cfg.Update();
}
catch
{
Expand All @@ -121,9 +147,14 @@ private void pictureBox1_Click_1(object sender, EventArgs e)
pictureBox1.BackColor = loColorForm.Color;
TrayIcon.normalColor = loColorForm.Color;
// 将设置的颜色保存到注册表中
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("normalColor", new ColorConverter().ConvertToString(TrayIcon.normalColor), RegistryValueKind.String);
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
//lgn.SetValue("normalColor", new ColorConverter().ConvertToString(TrayIcon.normalColor), RegistryValueKind.String);

PTConfig cfg = new PTConfig();
cfg.Load();
cfg.NormalColor = new ColorConverter().ConvertToString(TrayIcon.normalColor);
cfg.Update();
}
}

Expand All @@ -135,9 +166,14 @@ private void pictureBox2_Click(object sender, EventArgs e)
pictureBox2.BackColor = loColorForm.Color;
TrayIcon.chargingColor = loColorForm.Color;
// 将设置的颜色保存到注册表中
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("chargingColor", new ColorConverter().ConvertToString(TrayIcon.chargingColor), RegistryValueKind.String);
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
//lgn.SetValue("chargingColor", new ColorConverter().ConvertToString(TrayIcon.chargingColor), RegistryValueKind.String);

PTConfig cfg = new PTConfig();
cfg.Load();
cfg.ChargingColor = new ColorConverter().ConvertToString(TrayIcon.chargingColor);
cfg.Update();
}
}

Expand All @@ -148,17 +184,22 @@ private void pictureBox3_Click(object sender, EventArgs e)
{
pictureBox3.BackColor = loColorForm.Color;
TrayIcon.lowColor = loColorForm.Color; // 更新颜色
// 将设置的颜色保存到注册表中
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
lgn.SetValue("lowColor", new ColorConverter().ConvertToString(TrayIcon.lowColor), RegistryValueKind.String);
// 将设置的颜色保存到注册表中
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
//lgn.SetValue("lowColor", new ColorConverter().ConvertToString(TrayIcon.lowColor), RegistryValueKind.String);

PTConfig cfg = new PTConfig();
cfg.Load();
cfg.LowColor = new ColorConverter().ConvertToString(TrayIcon.lowColor);
cfg.Update();
}
}

// 限制文本框只能输入数字
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
{
e.Handled = true;
}
Expand Down Expand Up @@ -219,17 +260,25 @@ private void Settings_Load(object sender, EventArgs e)
// 电池电量充满后是否自动隐藏电池图标
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
RegistryKey hklm = Registry.CurrentUser;
RegistryKey lgn = hklm.OpenSubKey(@"Software\BatteryIcon", true);
//RegistryKey hklm = Registry.CurrentUser;
//RegistryKey lgn = hklm.OpenSubKey(@"Software\BatteryIcon", true);

PTConfig cfg = new PTConfig();
cfg.Load();

if (checkBox1.Checked == true)
{
TrayIcon.autoHide = "true";
lgn.SetValue("autoHide", "true", RegistryValueKind.String);
} else
//lgn.SetValue("autoHide", "true", RegistryValueKind.String);
cfg.AutoHide = "true";
}
else
{
TrayIcon.autoHide = "false";
lgn.SetValue("autoHide", "false", RegistryValueKind.String);
//lgn.SetValue("autoHide", "false", RegistryValueKind.String);
cfg.AutoHide = "false";
}
cfg.Update();
}
}
}
Loading

0 comments on commit 24dcdc6

Please sign in to comment.