MainWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.Dlg;
  4. using MeterVision.FreeAi;
  5. using MeterVision.Patch;
  6. using MeterVision.Single;
  7. using MeterVision.Stand;
  8. using MeterVision.Station;
  9. using MeterVision.Upgrade;
  10. using MeterVision.Util;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. namespace MeterVision
  28. {
  29. /// <summary>
  30. /// MainWindow.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class MainWindow : Window
  33. {
  34. //全局硬件码
  35. public static string MachineCode;
  36. //绑定ConfigItem的属性就可以完成绑定操作
  37. //public event PropertyChangedEventHandler PropertyChanged;
  38. //protected void OnPropertyChanged(string propertyName)
  39. //{
  40. // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  41. //}
  42. //private ConfigItem _configItem;
  43. //public ConfigItem mConfigItem
  44. //{
  45. // get => _configItem;
  46. // set
  47. // {
  48. // _configItem = value;
  49. // //确保通知属性更改
  50. // //OnPropertyChanged(nameof(mConfigItem));
  51. // }
  52. //}
  53. [DllImport("kernel32.dll", SetLastError = true)]
  54. static extern IntPtr AddDllDirectory(string NewDirectory);
  55. [DllImport("kernel32.dll")]
  56. static extern bool SetDefaultDllDirectories(uint DirectoryFlags);
  57. const uint LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000;
  58. //--------------------------------------------
  59. private SolidColorBrush SelectedForeground;
  60. public CfginiItem CurConfigItem { get; set; }
  61. //字典用于缓存已加载的页面
  62. private Dictionary<string, object> pageCache = new Dictionary<string, object>();
  63. public MainWindow()
  64. {
  65. InitializeComponent();
  66. SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
  67. //AddDllDirectory(AppDomain.CurrentDomain.BaseDirectory + "dlls");
  68. AddDllDirectory(AppDomain.CurrentDomain.BaseDirectory);
  69. this.Title = $"{this.Title} V{UpgradeHelper.GetCurrentVersion()}";
  70. SelectedForeground = Brushes.DarkGreen;
  71. //new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2196F3"));
  72. ResetButtonForegrounds();
  73. CurConfigItem = CfginiItem.GetConfigItem(); //ConfigItem.GetConfigItem();
  74. this.DataContext = this;
  75. this.Loaded += MainWindow_Loaded;
  76. UpdateDatabase();
  77. }
  78. //更新数据库(因为有添加的字段)
  79. private void UpdateDatabase()
  80. {
  81. DBPatch.UpdateTPatchDetailSchema();
  82. DBSingle.UpdateTSingleDetailSchema();
  83. }
  84. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  85. {
  86. if(LicenseMana.mLicenseModel != null)
  87. {
  88. if (LicenseMana.mLicenseModel.IsPermanent)
  89. {
  90. this.Title = $"{this.Title} (永久注册)";
  91. }
  92. else
  93. {
  94. this.Title = $"{this.Title} (L)";
  95. }
  96. }
  97. else
  98. {
  99. this.Title = $"{this.Title} (未注册)";
  100. }
  101. }
  102. private void BtnConfig_Click(object sender, RoutedEventArgs e)
  103. {
  104. EditConfigDlg dialog = new EditConfigDlg()
  105. {
  106. Owner = this,
  107. WindowStartupLocation = WindowStartupLocation.CenterOwner
  108. };
  109. if(dialog.ShowDialog() == true)
  110. {
  111. }
  112. //ResetButtonForegrounds();
  113. //txtConfig.Foreground = Brushes.DarkGreen;
  114. //string key = "ConfigPage";
  115. //// 检查字典中是否已有该页面
  116. //if (pageCache.ContainsKey(key))
  117. //{
  118. // ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  119. //}
  120. //else
  121. //{
  122. // var page = new UCConfig(); //UCSingleGrid(); // 新建页面
  123. // //pageCache[key] = page; // 缓存页面
  124. // ContentArea.Content = page; // 显示新页面
  125. //}
  126. }
  127. private void BtnSetStation_Click(object sender, RoutedEventArgs e)
  128. {
  129. ResetButtonForegrounds();
  130. txtSetStation.Foreground = Brushes.DarkGreen;
  131. string key = "SetStation";
  132. // 检查字典中是否已有该页面
  133. if (pageCache.ContainsKey(key))
  134. {
  135. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  136. }
  137. else
  138. {
  139. var page = new UCStationMain(); // 新建页面
  140. //pageCache[key] = page; // 缓存页面
  141. ContentArea.Content = page; // 显示新页面
  142. }
  143. }
  144. //单张图片的处理
  145. private void BtnSingleImage_Click(object sender, RoutedEventArgs e)
  146. {
  147. ResetButtonForegrounds();
  148. // 将被点击的按钮前景色设置为DarkGreen
  149. txtSingleImage.Foreground = SelectedForeground; //"#2196F3"
  150. //Brushes.DarkGreen;
  151. string key = "SingleImagePage";
  152. // 检查字典中是否已有该页面
  153. if (pageCache.ContainsKey(key))
  154. {
  155. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  156. }
  157. else
  158. {
  159. var page = new UCSingleMain(); //UCSingleGrid(); // 新建页面
  160. pageCache[key] = page; // 缓存页面
  161. ContentArea.Content = page; // 显示新页面
  162. }
  163. }
  164. private void BtnStandImport_Click(object sender,RoutedEventArgs e)
  165. {
  166. ResetButtonForegrounds();
  167. // 将被点击的按钮前景色设置为DarkGreen
  168. txtStandImport.Foreground = Brushes.DarkGreen;
  169. string key = "StandImportPage";
  170. // 检查字典中是否已有该页面
  171. if (pageCache.ContainsKey(key))
  172. {
  173. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  174. }
  175. else
  176. {
  177. var page = new UCStandMain(); // 新建页面
  178. pageCache[key] = page; // 缓存页面
  179. ContentArea.Content = page; // 显示新页面
  180. }
  181. }
  182. //批量图片的处理
  183. private void BtnPatchImage_Click(object sender, RoutedEventArgs e)
  184. {
  185. ResetButtonForegrounds();
  186. // 将被点击的按钮前景色设置为DarkGreen
  187. txtPatchImage.Foreground = Brushes.DarkGreen;
  188. string key = "PatchImagePage";
  189. // 检查字典中是否已有该页面
  190. if (pageCache.ContainsKey(key))
  191. {
  192. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  193. }
  194. else
  195. {
  196. var page = new UCPatchMain();
  197. pageCache[key] = page; // 缓存页面
  198. ContentArea.Content = page; // 显示新页面
  199. }
  200. }
  201. private void BtnPatchCompare_Click(object sender, RoutedEventArgs e)
  202. {
  203. ResetButtonForegrounds();
  204. // 将被点击的按钮前景色设置为DarkGreen
  205. txtCompareImage.Foreground = Brushes.DarkGreen;
  206. string key = "CompareImagePage";
  207. // 检查字典中是否已有该页面
  208. if (pageCache.ContainsKey(key))
  209. {
  210. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  211. }
  212. else
  213. {
  214. var page = new UCCompMain();
  215. pageCache[key] = page; // 缓存页面
  216. ContentArea.Content = page; // 显示新页面
  217. }
  218. }
  219. private void ResetButtonForegrounds()
  220. {
  221. //txtConfig.Foreground = Brushes.Black;
  222. txtSingleImage.Foreground = Brushes.Black;
  223. txtStandImport.Foreground = Brushes.Black;
  224. txtPatchImage.Foreground = Brushes.Black;
  225. txtCompareImage.Foreground = Brushes.Black;
  226. txtSetStation.Foreground = Brushes.Black;
  227. }
  228. private void BtnAbout_Click(object sender, RoutedEventArgs e)
  229. {
  230. AboutWindow aboutWindow = new AboutWindow()
  231. {
  232. Owner = Application.Current.MainWindow
  233. };
  234. aboutWindow.ShowDialog(); // 显示为模态窗口
  235. }
  236. private void BtnCheckUpgrade_Click(object sender, RoutedEventArgs e)
  237. {
  238. WaitWindow waitWindow = null;
  239. try
  240. {
  241. string titleInfo = "正在检查升级信息,请稍后...";
  242. waitWindow = new WaitWindow(titleInfo)
  243. {
  244. Owner = Application.Current.MainWindow,
  245. WindowStartupLocation = WindowStartupLocation.CenterOwner
  246. };
  247. waitWindow.Show();
  248. UpgradeModel upgradeModel = UpgradeHelper.ReadUpdateTxt();
  249. //waitWindow.Close();
  250. //获取当前的版本与versionCode比较
  251. string currentVersion = UpgradeHelper.GetCurrentVersion();
  252. // 获取服务器返回的 versionCode(假设是字符串 "1.0.0.6")
  253. string serverVersionCode = upgradeModel.VersionCode;
  254. if (UpgradeHelper.IsNewVersionAvailable(currentVersion, serverVersionCode))
  255. {
  256. //MessageBox.Show($"发现新版本({serverVersionCode})。请升级应用。", "升级提示",
  257. // MessageBoxButton.OK, MessageBoxImage.Information);
  258. WndUpdateAsk updateAsk = new WndUpdateAsk(upgradeModel)
  259. {
  260. Owner = Application.Current.MainWindow,
  261. WindowStartupLocation = WindowStartupLocation.CenterOwner
  262. };
  263. updateAsk.ShowDialog();
  264. }
  265. else
  266. {
  267. MessageBox.Show("当前已经是最新版本。", "无升级", MessageBoxButton.OK, MessageBoxImage.Information);
  268. }
  269. }
  270. catch(Exception ex)
  271. {
  272. MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  273. }
  274. if(waitWindow != null)
  275. {
  276. waitWindow.Close();
  277. }
  278. }
  279. //注册
  280. private void BtnReg_Click(object sender, RoutedEventArgs e)
  281. {
  282. RegisterDlg registerDlg = new RegisterDlg
  283. {
  284. Owner = this,
  285. WindowStartupLocation = WindowStartupLocation.CenterOwner
  286. };
  287. if (registerDlg.ShowDialog() == true)
  288. {
  289. }
  290. }
  291. private void Overlay_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  292. {
  293. // 如果覆盖层可见且用户点击了它,则隐藏覆盖层并关闭对话框
  294. //if (this.overlay.Visibility == Visibility.Visible)
  295. //{
  296. // this.overlay.Visibility = Visibility.Collapsed;
  297. // // 尝试关闭与覆盖层关联的对话框
  298. // foreach (Window window in Application.Current.Windows)
  299. // {
  300. // if (window != this && window.IsActive)
  301. // {
  302. // window.Close();
  303. // break;
  304. // }
  305. // }
  306. //}
  307. }
  308. private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  309. {
  310. // 查找所有打开的窗口
  311. foreach (Window child in Application.Current.Windows)
  312. {
  313. // 检查是否是你想要的窗口
  314. if (child is imgWindow || child is EditStandValueDlg || child is LogViewerWindow)
  315. {
  316. child.Close();
  317. }
  318. }
  319. }
  320. private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  321. {
  322. // 当按下 Esc 键时关闭对话框
  323. if (e.Key == Key.Escape)
  324. {
  325. foreach (Window child in Application.Current.Windows)
  326. {
  327. // 检查是否是你想要的窗口
  328. if (child is imgWindow || child is EditStandValueDlg || child is LogViewerWindow)
  329. {
  330. child.Close();
  331. }
  332. }
  333. }
  334. }
  335. public object GetActivityUC()
  336. {
  337. return ContentArea.Content;
  338. }
  339. private async void BtnFreeSpace_Click(object sender, RoutedEventArgs e)
  340. {
  341. string titleInfo = "正在释放数据库空间占用,请稍候...";
  342. WaitWindow waitWindow = new WaitWindow(titleInfo)
  343. {
  344. Owner = Application.Current.MainWindow,
  345. WindowStartupLocation = WindowStartupLocation.CenterOwner
  346. };
  347. waitWindow.Show();
  348. //DBStand.FreeDatabase();
  349. try
  350. {
  351. await Task.Run(() =>
  352. {
  353. DBStand.FreeDatabase();
  354. });
  355. }
  356. catch (Exception ex)
  357. {
  358. MessageBox.Show(Application.Current.MainWindow, $"失败:{ex.Message}", "错误",
  359. MessageBoxButton.OK, MessageBoxImage.Error);
  360. }
  361. finally
  362. {
  363. //关闭等待窗口
  364. waitWindow.Close();
  365. }
  366. MessageBox.Show(this, "已释放", "提示",MessageBoxButton.OK, MessageBoxImage.Information);
  367. }
  368. private void ChkRealSendAILog_Checked(object sender, RoutedEventArgs e)
  369. {
  370. FaRun.SendAiLogEnable = true;
  371. }
  372. private void ChkRealSendAILog_Unchecked(object sender, RoutedEventArgs e)
  373. {
  374. FaRun.SendAiLogEnable = false;
  375. }
  376. private void ChkRealRecordAILog_Checked(object sender, RoutedEventArgs e)
  377. {
  378. FaRun.RecordRealLogEnable = true;
  379. }
  380. private void ChkRealRecordAILog_Unchecked(object sender, RoutedEventArgs e)
  381. {
  382. FaRun.RecordRealLogEnable = false;
  383. }
  384. private void BtnViewRealLog_Click(object sender, RoutedEventArgs e)
  385. {
  386. ViewRealLogDlg dialog = new ViewRealLogDlg()
  387. {
  388. Owner = this,
  389. WindowStartupLocation = WindowStartupLocation.CenterOwner
  390. };
  391. dialog.ShowDialog();
  392. }
  393. //----------------------------------------------------------
  394. }
  395. }