ApplicationParameters.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using DirectShowLib;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace ImpulseVision
  12. {
  13. class ApplicationParameters
  14. {
  15. #region <Переменные>
  16. private bool autoRun;
  17. private Point points;//положение окна
  18. private Size sizes;//размеры окна
  19. private int countDay;
  20. public bool AutoRun
  21. {
  22. get { return GetStatusAutoRun(); }
  23. set
  24. {
  25. autoRun = value;
  26. if (autoRun)
  27. {
  28. SetAutoRun(Application.ExecutablePath);
  29. }
  30. else
  31. {
  32. RemoveAutoRun();
  33. }
  34. }
  35. }
  36. /// <summary>
  37. /// положение формы
  38. /// </summary>
  39. public Point Points
  40. {
  41. get { return GetFormLocation(); }
  42. set
  43. {
  44. points = value;
  45. if (points.X != -1 && points.Y != -1)
  46. {
  47. SetFormLocation(points);
  48. }
  49. else
  50. {
  51. RemoveFormLocation();
  52. }
  53. }
  54. }
  55. public Size Sizes
  56. {
  57. get { return GetSizeForm(); }
  58. set
  59. {
  60. sizes = value;
  61. if (sizes.Width != -1 && sizes.Height != -1)
  62. {
  63. SetSizeWindow(sizes);
  64. }
  65. else
  66. {
  67. RemoveSizeWindow();
  68. }
  69. }
  70. }
  71. public int CountDay
  72. {
  73. get { return GetCountDay(); }
  74. set
  75. {
  76. countDay = value;
  77. if (countDay != -1)
  78. {
  79. SetSaveLogFiles(countDay);
  80. }
  81. else
  82. {
  83. RemoveSaveLogFiles();
  84. }
  85. }
  86. }
  87. #endregion
  88. public ApplicationParameters()
  89. {
  90. }
  91. public void DeleteAllParameter()
  92. {
  93. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software", true);
  94. try
  95. {
  96. Reg.DeleteSubKeyTree("AppDemo");
  97. MessageBox.Show("Ключ реестра удалён!");
  98. }
  99. catch
  100. {
  101. MessageBox.Show("Указанного ключа реестра не существует!");
  102. }
  103. Reg.Close();
  104. }
  105. #region --Загрузка вместе с ОС--
  106. /// <summary>
  107. /// задание параметров автозагрузки
  108. /// </summary>
  109. private void SetAutoRun(string executablePath)
  110. {
  111. RegistryKey Reg = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\");
  112. try
  113. {
  114. executablePath = executablePath.Replace("/", "\\");
  115. Reg.SetValue("ImpulseVisionApp", executablePath);
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show(ex.Message, "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Error);
  120. }
  121. Reg.Close();
  122. StreamWriter Sw = new StreamWriter(Application.StartupPath + "\\autorun.txt");
  123. Sw.Close();
  124. }
  125. /// <summary>
  126. /// получение статуса автозагрузки
  127. /// </summary>
  128. /// <returns>включена или выключена (true/false)</returns>
  129. private bool GetStatusAutoRun()
  130. {
  131. bool Status = false;
  132. RegistryKey Reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\",true);
  133. if (Reg != null)
  134. {
  135. Status = Reg.GetValue("ImpulseVisionApp") != null;
  136. }
  137. Reg.Close();
  138. return Status;
  139. }
  140. /// <summary>
  141. /// удаление автозагрузки
  142. /// </summary>
  143. private void RemoveAutoRun()
  144. {
  145. RegistryKey Reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\", true);
  146. if (Reg != null)
  147. {
  148. if (Reg.GetValue("ImpulseVisionApp") != null)
  149. {
  150. Reg.DeleteValue("ImpulseVisionApp");
  151. }
  152. }
  153. Reg.Close();
  154. if (File.Exists(Application.StartupPath + "\\autorun.txt"))
  155. {
  156. File.Delete(Application.StartupPath + "\\autorun.txt");
  157. }
  158. }
  159. #endregion
  160. #region --Положение формы--
  161. /// <summary>
  162. /// установка параметров положения формы
  163. /// </summary>
  164. private void SetFormLocation(Point points)
  165. {
  166. int PosX = points.X;
  167. int PosY = points.Y;
  168. RegistryKey Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp", true);
  169. Reg.SetValue("LocX", points.X);
  170. Reg.SetValue("LocY", points.Y);
  171. Reg.Close();
  172. }
  173. private Point GetFormLocation()
  174. {
  175. int PosX = -1;
  176. int PosY = -1;
  177. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  178. if(Reg != null)
  179. {
  180. if (Reg.GetValue("LocX") != null)
  181. {
  182. PosX = (int)Reg.GetValue("LocX");
  183. }
  184. if (Reg.GetValue("LocY") != null)
  185. {
  186. PosY = (int)Reg.GetValue("LocY");
  187. }
  188. }
  189. Reg.Close();
  190. return new Point(PosX, PosY);
  191. }
  192. /// <summary>
  193. /// удаление сохранения положения формы
  194. /// </summary>
  195. private void RemoveFormLocation()
  196. {
  197. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  198. if(Reg != null)
  199. {
  200. if (Reg.GetValue("LocX") != null)
  201. {
  202. Reg.DeleteValue("LocX");
  203. }
  204. if (Reg.GetValue("LocY") != null)
  205. {
  206. Reg.DeleteValue("LocY");
  207. }
  208. }
  209. Reg.Close();
  210. }
  211. #endregion
  212. #region --Размеры окна--
  213. /// <summary>
  214. /// установка настроек для сохранения размеров окна
  215. /// </summary>
  216. private void SetSizeWindow(Size sizes)
  217. {
  218. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  219. if (Reg == null)
  220. Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp");
  221. Reg.SetValue("width", sizes.Width);
  222. Reg.SetValue("height", sizes.Height);
  223. Reg.Close();
  224. }
  225. /// <summary>
  226. /// получение ранее сохранённых размеров формы
  227. /// </summary>
  228. /// <returns></returns>
  229. private Size GetSizeForm()
  230. {
  231. int Wigth = -1, Height = -1;
  232. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp");
  233. if(Reg != null)
  234. {
  235. if (Reg.GetValue("width") != null)
  236. {
  237. Wigth = (int)Reg.GetValue("width");
  238. }
  239. if (Reg.GetValue("height") != null)
  240. {
  241. Height = (int)Reg.GetValue("height");
  242. }
  243. }
  244. Reg.Close();
  245. return new Size(Wigth, Height);
  246. }
  247. /// <summary>
  248. /// удалить настройки для сохранения размеров окна
  249. /// </summary>
  250. private void RemoveSizeWindow()
  251. {
  252. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  253. if (Reg != null)
  254. {
  255. if(Reg.GetValue("width") != null)
  256. Reg.DeleteValue("width");
  257. if(Reg.GetValue("height") != null)
  258. Reg.DeleteValue("height");
  259. }
  260. Reg.Close();
  261. }
  262. #endregion
  263. #region --Хранение файлов журнала--
  264. /// <summary>
  265. /// установка количества дней хранения файлов журнала
  266. /// </summary>
  267. private void SetSaveLogFiles(int countDay)
  268. {
  269. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  270. if (Reg == null)
  271. Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp");
  272. Reg.SetValue("countDay", countDay);
  273. Reg.Close();
  274. }
  275. private int GetCountDay()
  276. {
  277. int CountDay = 2;
  278. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp");
  279. if (Reg != null)
  280. {
  281. if (Reg.GetValue("countDay") != null)
  282. {
  283. CountDay = (int)Reg.GetValue("countDay");
  284. }
  285. Reg.Close();
  286. }
  287. return CountDay;
  288. }
  289. /// <summary>
  290. /// удаление параметра для сохранения файлов журнала
  291. /// </summary>
  292. private void RemoveSaveLogFiles()
  293. {
  294. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  295. if(Reg != null)
  296. {
  297. if (Reg.GetValue("countDay") != null)
  298. {
  299. Reg.DeleteValue("countDay");
  300. }
  301. }
  302. }
  303. #endregion
  304. }
  305. }