ApplicationParameters.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 struct FormParameters
  92. {
  93. public bool startingWithWindows;
  94. public Point points;
  95. public Size sizes;
  96. public int countDay;
  97. }
  98. /// <summary>
  99. /// получение параметров формы
  100. /// </summary>
  101. private FormParameters GetParameters()
  102. {
  103. //получить параметры из реестра и возвратить в качестве структуры
  104. return new FormParameters();
  105. }
  106. public void DeleteAllParameter()
  107. {
  108. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software", true);
  109. try
  110. {
  111. Reg.DeleteSubKeyTree("AppDemo");
  112. MessageBox.Show("Ключ реестра удалён!");
  113. }
  114. catch
  115. {
  116. MessageBox.Show("Указанного ключа реестра не существует!");
  117. }
  118. Reg.Close();
  119. }
  120. #region --Загрузка вместе с ОС--
  121. /// <summary>
  122. /// задание параметров автозагрузки
  123. /// </summary>
  124. private void SetAutoRun(string executablePath)
  125. {
  126. RegistryKey Reg = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\");
  127. try
  128. {
  129. executablePath = executablePath.Replace("/", "\\");
  130. Reg.SetValue("ImpulseVisionApp", executablePath);
  131. }
  132. catch (Exception ex)
  133. {
  134. MessageBox.Show(ex.Message, "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Error);
  135. }
  136. Reg.Close();
  137. StreamWriter Sw = new StreamWriter(Application.StartupPath + "\\autorun.txt");
  138. Sw.Close();
  139. }
  140. /// <summary>
  141. /// получение статуса автозагрузки
  142. /// </summary>
  143. /// <returns>включена или выключена (true/false)</returns>
  144. private bool GetStatusAutoRun()
  145. {
  146. bool Status = false;
  147. RegistryKey Reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\",true);
  148. if (Reg != null)
  149. {
  150. Status = Reg.GetValue("ImpulseVisionApp") != null;
  151. }
  152. Reg.Close();
  153. return Status;
  154. }
  155. /// <summary>
  156. /// удаление автозагрузки
  157. /// </summary>
  158. private void RemoveAutoRun()
  159. {
  160. RegistryKey Reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\", true);
  161. if (Reg != null)
  162. {
  163. if (Reg.GetValue("ImpulseVisionApp") != null)
  164. {
  165. Reg.DeleteValue("ImpulseVisionApp");
  166. }
  167. }
  168. Reg.Close();
  169. if (File.Exists(Application.StartupPath + "\\autorun.txt"))
  170. {
  171. File.Delete(Application.StartupPath + "\\autorun.txt");
  172. }
  173. }
  174. #endregion
  175. #region --Положение формы--
  176. /// <summary>
  177. /// установка параметров положения формы
  178. /// </summary>
  179. private void SetFormLocation(Point points)
  180. {
  181. int PosX = points.X;
  182. int PosY = points.Y;
  183. RegistryKey Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp", true);
  184. Reg.SetValue("LocX", points.X);
  185. Reg.SetValue("LocY", points.Y);
  186. Reg.Close();
  187. }
  188. private Point GetFormLocation()
  189. {
  190. int PosX = -1;
  191. int PosY = -1;
  192. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  193. if(Reg != null)
  194. {
  195. if (Reg.GetValue("LocX") != null)
  196. {
  197. PosX = (int)Reg.GetValue("LocX");
  198. }
  199. if (Reg.GetValue("LocY") != null)
  200. {
  201. PosY = (int)Reg.GetValue("LocY");
  202. }
  203. }
  204. Reg.Close();
  205. return new Point(PosX, PosY);
  206. }
  207. /// <summary>
  208. /// удаление сохранения положения формы
  209. /// </summary>
  210. private void RemoveFormLocation()
  211. {
  212. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  213. if(Reg != null)
  214. {
  215. if (Reg.GetValue("LocX") != null)
  216. {
  217. Reg.DeleteValue("LocX");
  218. }
  219. if (Reg.GetValue("LocY") != null)
  220. {
  221. Reg.DeleteValue("LocY");
  222. }
  223. }
  224. Reg.Close();
  225. }
  226. #endregion
  227. #region --Размеры окна--
  228. /// <summary>
  229. /// установка настроек для сохранения размеров окна
  230. /// </summary>
  231. private void SetSizeWindow(Size sizes)
  232. {
  233. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  234. if (Reg == null)
  235. Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp");
  236. Reg.SetValue("width", sizes.Width);
  237. Reg.SetValue("height", sizes.Height);
  238. Reg.Close();
  239. }
  240. /// <summary>
  241. /// получение ранее сохранённых размеров формы
  242. /// </summary>
  243. /// <returns></returns>
  244. private Size GetSizeForm()
  245. {
  246. int Wigth = -1, Height = -1;
  247. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp");
  248. if(Reg != null)
  249. {
  250. if (Reg.GetValue("width") != null)
  251. {
  252. Wigth = (int)Reg.GetValue("width");
  253. }
  254. if (Reg.GetValue("height") != null)
  255. {
  256. Height = (int)Reg.GetValue("height");
  257. }
  258. }
  259. Reg.Close();
  260. return new Size(Wigth, Height);
  261. }
  262. /// <summary>
  263. /// удалить настройки для сохранения размеров окна
  264. /// </summary>
  265. private void RemoveSizeWindow()
  266. {
  267. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  268. if (Reg != null)
  269. {
  270. if(Reg.GetValue("width") != null)
  271. Reg.DeleteValue("width");
  272. if(Reg.GetValue("height") != null)
  273. Reg.DeleteValue("height");
  274. }
  275. Reg.Close();
  276. }
  277. #endregion
  278. #region --Хранение файлов журнала--
  279. /// <summary>
  280. /// установка количества дней хранения файлов журнала
  281. /// </summary>
  282. private void SetSaveLogFiles(int countDay)
  283. {
  284. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  285. if (Reg == null)
  286. Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp");
  287. Reg.SetValue("countDay", countDay);
  288. Reg.Close();
  289. }
  290. private int GetCountDay()
  291. {
  292. int CountDay = 2;
  293. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp");
  294. if (Reg != null)
  295. {
  296. if (Reg.GetValue("countDay") != null)
  297. {
  298. CountDay = (int)Reg.GetValue("countDay");
  299. }
  300. Reg.Close();
  301. }
  302. return CountDay;
  303. }
  304. /// <summary>
  305. /// удаление параметра для сохранения файлов журнала
  306. /// </summary>
  307. private void RemoveSaveLogFiles()
  308. {
  309. RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
  310. if(Reg != null)
  311. {
  312. if (Reg.GetValue("countDay") != null)
  313. {
  314. Reg.DeleteValue("countDay");
  315. }
  316. }
  317. }
  318. #endregion
  319. }
  320. }