Window1.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using ivf_tl_Control;
  2. using ivf_tl_ControlMain;
  3. using IvfTl.Control.Entity.GlobalEnums;
  4. using IvfTl.Control.Services;
  5. using ivf_tl_UtilHelper;
  6. using System;
  7. using System.ComponentModel;
  8. using System.Configuration;
  9. using System.IO;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Media.Imaging;
  14. using System.Xml;
  15. namespace ivf_tl_ControlTest
  16. {
  17. /// <summary>
  18. /// Window1.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class Window1 : Window
  21. {
  22. bool stop = false;
  23. int stopPro = 15;
  24. public Window1()
  25. {
  26. InitializeComponent();
  27. if (int.TryParse(ConfigurationManager.AppSettings["StopPro"].ToString(), out int newTime12)) stopPro = newTime12;
  28. AppData.Instance.StopProEvent += Instance_StopProEvent;
  29. string mvcFileName = @"C:\Windows\System32\MVCCamera.ini";
  30. if (File.Exists(mvcFileName)) File.Delete(mvcFileName);
  31. Loaded += Window1_Loaded1;
  32. //Closed += Window1_Closed;
  33. //Closing += Window1_Closing;
  34. }
  35. private void Window1_Closing(object? sender, CancelEventArgs e)
  36. {
  37. Log4netHelper.WriteLog("Window1_Closing");
  38. }
  39. private void Window1_Closed(object? sender, EventArgs e)
  40. {
  41. Log4netHelper.WriteLog("窗口关闭");
  42. }
  43. private void Window1_Loaded1(object sender, RoutedEventArgs e)
  44. {
  45. string account = ConfigurationManager.AppSettings["userName"].ToString();
  46. string password = ConfigurationManager.AppSettings["passWord"].ToString();
  47. string cacheDisk = ConfigurationManager.AppSettings["cacheDisk"].ToString();
  48. this._account_TextBox.Text = account;
  49. this._password_TextBox.Password = password;
  50. this._button.IsEnabled = false;
  51. this._button1.IsEnabled = true;
  52. Task.Run(() =>
  53. {
  54. if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
  55. {
  56. AppData.Instance.LogService.TLLog($"请输入账号或密码", LogEnum.RunRecord);
  57. Environment.Exit(0);
  58. return;
  59. }
  60. if (!AppData.Instance.Login(account, password))
  61. {
  62. AppData.Instance.LogService.TLLog($"登录失败", LogEnum.RunRecord);
  63. Environment.Exit(0);
  64. return;
  65. }
  66. PathHelper.pan = cacheDisk;
  67. AppData.Instance.LogService.Pan = cacheDisk;
  68. SetApp1(account, password);
  69. StartMain startMain = new StartMain();
  70. string ss = startMain.StartRun();
  71. if (!string.IsNullOrEmpty(ss))
  72. {
  73. AppData.Instance.LogService.TLLog($"启动失败:{ss}", LogEnum.RunRecord);
  74. Environment.Exit(0);
  75. return;
  76. }
  77. SetApp1(AppData.Instance.TLSetting.tmpDir);
  78. });
  79. }
  80. private void Window1_Loaded(object sender, RoutedEventArgs e)
  81. {
  82. this._account_TextBox.Text = ConfigurationManager.AppSettings["userName"].ToString();
  83. this._password_TextBox.Password = ConfigurationManager.AppSettings["passWord"].ToString();
  84. }
  85. //[DllImport("user32.dll", SetLastError = true)]
  86. //static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  87. //[DllImport("user32.dll", CharSet = CharSet.Auto)]
  88. //static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  89. //delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  90. //[DllImport("user32.dll")]
  91. //static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
  92. //[DllImport("user32.dll")]
  93. //static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  94. //[DllImport("user32.dll", CharSet = CharSet.Auto)]
  95. //static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  96. //const uint WM_CLOSE = 0x0010;
  97. //const uint BM_CLICK = 0x00F5;
  98. /// <summary>
  99. /// 开始按钮
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. private void Button_Click(object sender, RoutedEventArgs e)
  104. {
  105. //AppData.Instance.TestEvent();
  106. //return;
  107. //var aaa = MessageBoxResult.Cancel;
  108. //var dialogTask = Task.Run(() =>
  109. //{
  110. // aaa = MessageBox.Show("messageinfo", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
  111. // Debug.WriteLine(1);
  112. //});
  113. //Task.Delay(3000).ContinueWith(t =>
  114. //{
  115. // if (!dialogTask.IsCompleted)
  116. // {
  117. // IntPtr hWnd = FindWindow("#32770", "提示");
  118. // if (hWnd != IntPtr.Zero)
  119. // {
  120. // EnumChildWindows(hWnd, (childHwnd, lParam) =>
  121. // {
  122. // StringBuilder className = new StringBuilder(256);
  123. // GetClassName(childHwnd, className, className.Capacity);
  124. // if (className.ToString() == "Button")
  125. // {
  126. // StringBuilder text = new StringBuilder(256);
  127. // GetWindowText(childHwnd, text, text.Capacity);
  128. // if (text.ToString().Contains("Y"))
  129. // {
  130. // SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
  131. // return false;
  132. // }
  133. // }
  134. // return true;
  135. // }, IntPtr.Zero);
  136. // }
  137. // Debug.WriteLine(2);
  138. // }
  139. //});
  140. //dialogTask.Wait();
  141. //Debug.WriteLine(aaa);
  142. //return;
  143. string account = this._account_TextBox.Text.Trim();
  144. string password = this._password_TextBox.Password.Trim();
  145. if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
  146. {
  147. MessageBox.Show("请输入账号或密码");
  148. return;
  149. }
  150. if (!AppData.Instance.Login(account, password))
  151. {
  152. MessageBox.Show("登录失败");
  153. return;
  154. }
  155. SetApp1(account, password);
  156. //var aa = AppData.Instance.SerialBinController.SearchPictureController("3_1_7_69_2023-09-28-22-41-41_71900_75000.jpg", "NEO-1-20230107");
  157. StartMain startMain = new StartMain();
  158. MessageBox.Show(startMain.StartRun());
  159. this._button.IsEnabled = false;
  160. this._button1.IsEnabled = true;
  161. }
  162. private object lockObject = new object();
  163. private void Instance_StopProEvent()
  164. {
  165. return;
  166. AppData.Instance.LogService.TLLog($"拍照异常退出软件", LogEnum.RunError);
  167. lock (lockObject)
  168. {
  169. if (stop) return;
  170. stop = true;
  171. }
  172. Dispatcher.Invoke(() =>
  173. {
  174. StopWindow stopWindow = new StopWindow(this, stopPro);
  175. stopWindow.Show();
  176. });
  177. }
  178. private void SetApp(string name, string pass)
  179. {
  180. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  181. config.AppSettings.Settings["userName"].Value = name;
  182. config.AppSettings.Settings["passWord"].Value = pass;
  183. config.Save(ConfigurationSaveMode.Modified);
  184. System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  185. }
  186. public void SetApp1(string name, string pass)
  187. {
  188. try
  189. {
  190. string fileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}ivf_tl_ControlMain.dll.config";
  191. if (!File.Exists(fileName)) return;
  192. var xmlDoc = new XmlDocument();
  193. xmlDoc.Load(fileName);
  194. var tags = xmlDoc.GetElementsByTagName("appSettings");
  195. XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
  196. if (appSettingsNode != null)
  197. {
  198. XmlNodeList addNodes = appSettingsNode.SelectNodes("add");
  199. foreach (XmlNode addNode in addNodes)
  200. {
  201. //string key = addNode.Attributes["key"].Value;
  202. //string value = addNode.Attributes["value"].Value;
  203. //Debug.WriteLine("键: " + key + ", 值: " + value);
  204. if (addNode.Attributes["key"].Value == "userName")
  205. {
  206. addNode.Attributes["value"].Value = name;
  207. }
  208. if (addNode.Attributes["key"].Value == "passWord")
  209. {
  210. addNode.Attributes["value"].Value = pass;
  211. }
  212. }
  213. }
  214. xmlDoc.Save(fileName);
  215. System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  216. xmlDoc = null;
  217. }
  218. catch (Exception ex)
  219. {
  220. return;
  221. }
  222. }
  223. private void SetApp(string newDisk)
  224. {
  225. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  226. config.AppSettings.Settings["cacheDisk"].Value = newDisk;
  227. config.Save(ConfigurationSaveMode.Modified);
  228. System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  229. }
  230. public void SetApp1(string newDisk)
  231. {
  232. try
  233. {
  234. string fileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}ivf_tl_ControlMain.dll.config";
  235. if (!File.Exists(fileName)) return;
  236. var xmlDoc = new XmlDocument();
  237. xmlDoc.Load(fileName);
  238. var tags = xmlDoc.GetElementsByTagName("appSettings");
  239. XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
  240. if (appSettingsNode != null)
  241. {
  242. XmlNodeList addNodes = appSettingsNode.SelectNodes("add");
  243. foreach (XmlNode addNode in addNodes)
  244. {
  245. //string key = addNode.Attributes["key"].Value;
  246. //string value = addNode.Attributes["value"].Value;
  247. //Debug.WriteLine("键: " + key + ", 值: " + value);
  248. if (addNode.Attributes["key"].Value == "cacheDisk")
  249. {
  250. addNode.Attributes["value"].Value = newDisk;
  251. break;
  252. }
  253. }
  254. }
  255. xmlDoc.Save(fileName);
  256. System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  257. xmlDoc = null;
  258. }
  259. catch (Exception ex)
  260. {
  261. return;
  262. }
  263. }
  264. private void End_Click(object sender, RoutedEventArgs e)
  265. {
  266. //Log4netHelper.WriteLog("点击退出按钮");
  267. //Application.Current.Shutdown();
  268. //e.Handled = true;
  269. //return;
  270. Log4netHelper.WriteLog("点击退出按钮");
  271. AppData.Instance.SerialBinController.PushMessageController(AppData.Instance.TLSetting.tlSn, 0, null);
  272. Application.Current.Shutdown();
  273. e.Handled = true;
  274. }
  275. private void Button1_Click(object sender, RoutedEventArgs e)
  276. {
  277. if (AppData.Instance.MqttService != null)
  278. {
  279. AppData.Instance.MqttService.DisconnectAsync();
  280. }
  281. //string account = this._account_TextBox.Text.Trim();
  282. //string password = this._password_TextBox.Password.Trim();
  283. //if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
  284. //{
  285. // MessageBox.Show("请输入账号或密码");
  286. // return;
  287. //}
  288. //if (!AppData.Instance.Login(account, password))
  289. //{
  290. // MessageBox.Show("登录失败");
  291. // return;
  292. //}
  293. //AppData.Instance.UpdateSetting("NEO-1-20230107");
  294. }
  295. private void Button2_Click(object sender, RoutedEventArgs e)
  296. {
  297. if (AppData.Instance.MqttService != null)
  298. {
  299. AppData.Instance.MqttService.ClientMqtt();
  300. }
  301. }
  302. private void End_1Click(object sender, RoutedEventArgs e)
  303. {
  304. System.Diagnostics.Process.Start("shutdown", " /r /t 0");
  305. }
  306. }
  307. }