AuthenticationUC.xaml.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using Microsoft.WindowsAPICodePack.Dialogs;
  17. namespace Phihong_EVSE_UI_Tool
  18. {
  19. /// <summary>
  20. /// AuthenticationUC.xaml 的互動邏輯
  21. /// </summary>
  22. public partial class AuthenticationUC : UserControl
  23. {
  24. private Properties.Settings mySettings = Properties.Settings.Default;
  25. private string verifyCustomImgPath;
  26. private string verifyOkCustomImgPath;
  27. private string verifyFailCustomImgPath;
  28. public AuthenticationUC()
  29. {
  30. InitializeComponent();
  31. verifyCustomImgPath = mySettings.VerifyCustomImgPath;
  32. uxVerifyDefaultRadioButton.IsChecked = mySettings.IsVerifyDefault;
  33. uxVerifyCustomRadioButton.IsChecked = !mySettings.IsVerifyDefault;
  34. uxVerifyExploreTextBox.Text = mySettings.VerifyCustomImgPath_org;
  35. verifyOkCustomImgPath = mySettings.VerifyOkCustomImgPath;
  36. uxVerifyOkDefaultRadioButton.IsChecked = mySettings.IsVerifyOkDefault;
  37. uxVerifyOkCustomRadioButton.IsChecked = !mySettings.IsVerifyOkDefault;
  38. uxVerifyOkExploreTextBox.Text = mySettings.VerifyOkCustomImgPath_org;
  39. verifyFailCustomImgPath = mySettings.VerifyFailCustomImgPath;
  40. uxVerifyFailDefaultRadioButton.IsChecked = mySettings.IsVerifyFailDefault;
  41. uxVerifyFailCustomRadioButton.IsChecked = !mySettings.IsVerifyFailDefault;
  42. uxVerifyFailExploreTextBox.Text = mySettings.VerifyFailCustomImgPath_org;
  43. }
  44. private void uxDefaultRadioButton_Checked(object sender, RoutedEventArgs e)
  45. {
  46. Uri srcPath;
  47. RadioButton rb = e.Source as RadioButton;
  48. if (rb is null)
  49. {
  50. return;
  51. }
  52. switch (rb.Tag.ToString())
  53. {
  54. case "Verify":
  55. srcPath = new Uri(Utility.BG_PARENTFOLDER + Utility.BG_VERIFY, UriKind.Relative);
  56. uxVerifyImage.Source = new BitmapImage(srcPath);
  57. Utility.CopyFileFromResource(srcPath, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFY);
  58. mySettings.IsVerifyDefault = true;
  59. break;
  60. case "VerifyOk":
  61. srcPath = new Uri(Utility.BG_PARENTFOLDER + Utility.BG_VERIFYOK, UriKind.Relative);
  62. uxVerifyOkImage.Source = new BitmapImage(srcPath);
  63. Utility.CopyFileFromResource(srcPath, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFYOK);
  64. mySettings.IsVerifyOkDefault = true;
  65. break;
  66. case "VerifyFail":
  67. srcPath = new Uri(Utility.BG_PARENTFOLDER + Utility.BG_VERIFYFAIL, UriKind.Relative);
  68. uxVerifyFailImage.Source = new BitmapImage(srcPath);
  69. Utility.CopyFileFromResource(srcPath, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFYFAIL);
  70. mySettings.IsVerifyFailDefault = true;
  71. break;
  72. default:
  73. return;
  74. }
  75. mySettings.Save();
  76. }
  77. private void uxCustomRadioButton_Checked(object sender, RoutedEventArgs e)
  78. {
  79. RadioButton rb = e.Source as RadioButton;
  80. if (rb is null)
  81. {
  82. return;
  83. }
  84. switch (rb.Tag.ToString())
  85. {
  86. case "Verify":
  87. if (File.Exists(verifyCustomImgPath))
  88. {
  89. Utility.LoadImageFromPath(uxVerifyImage, verifyCustomImgPath);
  90. File.Copy(verifyCustomImgPath, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFY, true);
  91. mySettings.IsVerifyDefault = false;
  92. }
  93. break;
  94. case "VerifyOk":
  95. if (File.Exists(verifyOkCustomImgPath))
  96. {
  97. Utility.LoadImageFromPath(uxVerifyOkImage, verifyOkCustomImgPath);
  98. File.Copy(verifyOkCustomImgPath, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFYOK, true);
  99. mySettings.IsVerifyOkDefault = false;
  100. }
  101. break;
  102. case "VerifyFail":
  103. if (File.Exists(verifyFailCustomImgPath))
  104. {
  105. Utility.LoadImageFromPath(uxVerifyFailImage, verifyFailCustomImgPath);
  106. File.Copy(verifyFailCustomImgPath, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFYFAIL, true);
  107. mySettings.IsVerifyFailDefault = false;
  108. }
  109. break;
  110. default:
  111. return;
  112. }
  113. mySettings.Save();
  114. }
  115. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. Button btn = e.Source as Button;
  118. if (btn is null)
  119. {
  120. return;
  121. }
  122. var dlg = new CommonOpenFileDialog()
  123. {
  124. EnsureFileExists = true,
  125. Title = "Select an image file",
  126. };
  127. dlg.Filters.Add(new CommonFileDialogFilter("BMP images", "*.bmp"));
  128. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  129. {
  130. if (!Utility.IsBackgroundImageSizeQualified(dlg.FileName))
  131. {
  132. MessageBox.Show(dlg.FileName + "\r\n" + "The resolution of the image must be 800x480",
  133. "Incorrect Resolution", MessageBoxButton.OK, MessageBoxImage.Error);
  134. return;
  135. }
  136. switch (btn.Tag.ToString())
  137. {
  138. case "Verify":
  139. uxVerifyExploreTextBox.Text = dlg.FileName;
  140. verifyCustomImgPath = Utility.CUSTOM_DIRECTORY + Utility.BG_VERIFY;
  141. File.Copy(dlg.FileName, verifyCustomImgPath, true);
  142. File.Copy(dlg.FileName, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFY, true);
  143. Utility.LoadImageFromPath(uxVerifyImage, verifyCustomImgPath);
  144. mySettings.IsVerifyDefault = false;
  145. mySettings.VerifyCustomImgPath = verifyCustomImgPath;
  146. mySettings.VerifyCustomImgPath_org = uxVerifyExploreTextBox.Text;
  147. break;
  148. case "VerifyOk":
  149. uxVerifyOkExploreTextBox.Text = dlg.FileName;
  150. verifyOkCustomImgPath = Utility.CUSTOM_DIRECTORY + Utility.BG_VERIFYOK;
  151. File.Copy(dlg.FileName, verifyOkCustomImgPath, true);
  152. File.Copy(dlg.FileName, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFYOK, true);
  153. Utility.LoadImageFromPath(uxVerifyOkImage, verifyOkCustomImgPath);
  154. mySettings.IsVerifyOkDefault = false;
  155. mySettings.VerifyOkCustomImgPath = verifyOkCustomImgPath;
  156. mySettings.VerifyOkCustomImgPath_org = uxVerifyOkExploreTextBox.Text;
  157. break;
  158. case "VerifyFail":
  159. uxVerifyFailExploreTextBox.Text = dlg.FileName;
  160. verifyFailCustomImgPath = Utility.CUSTOM_DIRECTORY + Utility.BG_VERIFYFAIL;
  161. File.Copy(dlg.FileName, verifyFailCustomImgPath, true);
  162. File.Copy(dlg.FileName, Utility.OUTPUT_DIRECTORY + Utility.BG_VERIFYFAIL, true);
  163. Utility.LoadImageFromPath(uxVerifyFailImage, verifyFailCustomImgPath);
  164. mySettings.IsVerifyFailDefault = false;
  165. mySettings.VerifyFailCustomImgPath = verifyFailCustomImgPath;
  166. mySettings.VerifyFailCustomImgPath_org = uxVerifyFailExploreTextBox.Text;
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. mySettings.Save();
  173. }
  174. }
  175. }