SettingForm.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using BatteryIndicator.Model;
  2. using BatteryIndicator.Views;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace BatteryIndicator.UI
  15. {
  16. public partial class SettingForm : BaseForm
  17. {
  18. public SettingForm()
  19. {
  20. PTConfig cfg = new PTConfig();
  21. cfg.Load();
  22. string fontsize = cfg.FontSize;
  23. string xoffset = cfg.XOffset;
  24. string yoffset = cfg.YOffset;
  25. string normalColor = cfg.NormalColor;
  26. string chargingColor = cfg.ChargingColor;
  27. string lowColor = cfg.LowColor;
  28. InitializeComponent(); //绘制控件
  29. textBox1.Text = fontsize;
  30. textBox3.Text = xoffset;
  31. textBox4.Text = yoffset;
  32. pictureBox1.BackColor = (Color)new ColorConverter().ConvertFromString(normalColor); // 从字符串获取颜色
  33. pictureBox2.BackColor = (Color)new ColorConverter().ConvertFromString(chargingColor);
  34. pictureBox3.BackColor = (Color)new ColorConverter().ConvertFromString(lowColor);
  35. // 解决textbox1自动聚焦的问题
  36. textBox2.Visible = false;
  37. textBox2.Enabled = false;
  38. ActiveControl = textBox2;
  39. }
  40. private void label1_Click(object sender, EventArgs e)
  41. {
  42. }
  43. private void label2_Click(object sender, EventArgs e)
  44. {
  45. }
  46. private void label4_Click(object sender, EventArgs e)
  47. {
  48. }
  49. private void label3_Click(object sender, EventArgs e)
  50. {
  51. }
  52. private void textBox1_TextChanged(object sender, EventArgs e)
  53. {
  54. try
  55. {
  56. TrayIcon.iconFontSize = Convert.ToInt32(textBox1.Text);
  57. // 将设置的字号保存到注册表中
  58. //RegistryKey hklm = Registry.CurrentUser;
  59. //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
  60. //lgn.SetValue("fontsize", textBox1.Text, RegistryValueKind.String);
  61. PTConfig cfg = new PTConfig();
  62. cfg.Load();
  63. cfg.FontSize = textBox1.Text;
  64. cfg.Update();
  65. }
  66. catch
  67. {
  68. // do nothing
  69. }
  70. }
  71. private void textBox3_TextChanged(object sender, EventArgs e)
  72. {
  73. try
  74. {
  75. TrayIcon.xoffset = Convert.ToInt32(textBox3.Text);
  76. //RegistryKey hklm = Registry.CurrentUser;
  77. //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
  78. //lgn.SetValue("xoffset", textBox3.Text, RegistryValueKind.String);
  79. PTConfig cfg = new PTConfig();
  80. cfg.Load();
  81. cfg.XOffset = textBox3.Text;
  82. cfg.Update();
  83. }
  84. catch
  85. {
  86. // do nothing
  87. }
  88. }
  89. private void textBox4_TextChanged(object sender, EventArgs e)
  90. {
  91. try
  92. {
  93. TrayIcon.yoffset = Convert.ToInt32(textBox4.Text);
  94. //RegistryKey hklm = Registry.CurrentUser;
  95. //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
  96. //lgn.SetValue("yoffset", textBox4.Text, RegistryValueKind.String);
  97. PTConfig cfg = new PTConfig();
  98. cfg.Load();
  99. cfg.YOffset = textBox4.Text;
  100. cfg.Update();
  101. }
  102. catch
  103. {
  104. // do nothing
  105. }
  106. }
  107. private void pictureBox1_Click_1(object sender, EventArgs e)
  108. {
  109. ColorDialog loColorForm = new ColorDialog();
  110. if (loColorForm.ShowDialog() == DialogResult.OK)
  111. {
  112. pictureBox1.BackColor = loColorForm.Color;
  113. TrayIcon.normalColor = loColorForm.Color;
  114. // 将设置的颜色保存到注册表中
  115. //RegistryKey hklm = Registry.CurrentUser;
  116. //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
  117. //lgn.SetValue("normalColor", new ColorConverter().ConvertToString(TrayIcon.normalColor), RegistryValueKind.String);
  118. PTConfig cfg = new PTConfig();
  119. cfg.Load();
  120. cfg.NormalColor = new ColorConverter().ConvertToString(TrayIcon.normalColor);
  121. cfg.Update();
  122. }
  123. }
  124. private void pictureBox2_Click(object sender, EventArgs e)
  125. {
  126. ColorDialog loColorForm = new ColorDialog();
  127. if (loColorForm.ShowDialog() == DialogResult.OK)
  128. {
  129. pictureBox2.BackColor = loColorForm.Color;
  130. TrayIcon.chargingColor = loColorForm.Color;
  131. // 将设置的颜色保存到注册表中
  132. //RegistryKey hklm = Registry.CurrentUser;
  133. //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
  134. //lgn.SetValue("chargingColor", new ColorConverter().ConvertToString(TrayIcon.chargingColor), RegistryValueKind.String);
  135. PTConfig cfg = new PTConfig();
  136. cfg.Load();
  137. cfg.ChargingColor = new ColorConverter().ConvertToString(TrayIcon.chargingColor);
  138. cfg.Update();
  139. }
  140. }
  141. private void pictureBox3_Click(object sender, EventArgs e)
  142. {
  143. ColorDialog loColorForm = new ColorDialog();
  144. if (loColorForm.ShowDialog() == DialogResult.OK)
  145. {
  146. pictureBox3.BackColor = loColorForm.Color;
  147. TrayIcon.lowColor = loColorForm.Color; // 更新颜色
  148. // 将设置的颜色保存到注册表中
  149. //RegistryKey hklm = Registry.CurrentUser;
  150. //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
  151. //lgn.SetValue("lowColor", new ColorConverter().ConvertToString(TrayIcon.lowColor), RegistryValueKind.String);
  152. PTConfig cfg = new PTConfig();
  153. cfg.Load();
  154. cfg.LowColor = new ColorConverter().ConvertToString(TrayIcon.lowColor);
  155. cfg.Update();
  156. }
  157. }
  158. // 限制文本框只能输入数字
  159. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  160. {
  161. if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
  162. {
  163. e.Handled = true;
  164. }
  165. }
  166. private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
  167. {
  168. if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != '-')
  169. {
  170. e.Handled = true;
  171. }
  172. }
  173. private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
  174. {
  175. if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != '-')
  176. {
  177. e.Handled = true;
  178. }
  179. }
  180. private void pictureBox4_Click(object sender, EventArgs e)
  181. {
  182. }
  183. private void label7_Click(object sender, EventArgs e)
  184. {
  185. RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
  186. string s = key.GetValue("").ToString();
  187. Regex reg = new Regex("\"([^\"]+)\"");
  188. MatchCollection matchs = reg.Matches(s);
  189. string filename = "";
  190. if (matchs.Count > 0)
  191. {
  192. filename = matchs[0].Groups[1].Value;
  193. System.Diagnostics.Process.Start(filename, "https://github.com/loliMay/PercentageBatteryIcon");
  194. }
  195. }
  196. private void label8_Click(object sender, EventArgs e)
  197. {
  198. }
  199. private void label9_Click(object sender, EventArgs e)
  200. {
  201. }
  202. // 电池电量充满后是否自动隐藏电池图标
  203. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  204. {
  205. //RegistryKey hklm = Registry.CurrentUser;
  206. //RegistryKey lgn = hklm.OpenSubKey(@"Software\BatteryIcon", true);
  207. PTConfig cfg = new PTConfig();
  208. cfg.Load();
  209. if (checkBox1.Checked == true)
  210. {
  211. TrayIcon.autoHide = "true";
  212. //lgn.SetValue("autoHide", "true", RegistryValueKind.String);
  213. cfg.AutoHide = "true";
  214. }
  215. else
  216. {
  217. TrayIcon.autoHide = "false";
  218. //lgn.SetValue("autoHide", "false", RegistryValueKind.String);
  219. cfg.AutoHide = "false";
  220. }
  221. cfg.Update();
  222. }
  223. }
  224. }