MainWindow.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using System.IO;
  2. using System.Reflection;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media.Imaging;
  6. using Microsoft.Win32;
  7. namespace ArchivesCenter3
  8. {
  9. /// <summary>
  10. /// Interaction logic for MainWindow.xaml
  11. /// </summary>
  12. public partial class MainWindow : Window
  13. {
  14. public MainWindow()
  15. {
  16. InitializeComponent();
  17. updateinfo();
  18. LoadFileContent();
  19. Version version = Assembly.GetExecutingAssembly().GetName().Version;
  20. versionTextBlock.Text = $"版本号:{version.Major}.{version.Minor}.{version.Build}";
  21. }
  22. private List<string> lines; // 存储文件内容的列表
  23. private int currentIndex = -1; // 当前行索引
  24. private bool isNewRecord = false; // 标记是否是新建记录
  25. private void updateinfo()
  26. {
  27. welcomeText.Text = "欢迎!" + Settings1.Default.Username + " ";
  28. WelcomeTitle.Text = Settings1.Default.WelcomeTitle;
  29. DatabaseName.Text = Settings1.Default.DatabaseName;
  30. DatabaseSubtitle.Text = Settings1.Default.DatabaseSubtitle;
  31. UsernameCu.Text = Settings1.Default.Username;
  32. WelcomeTitleCu.Text = Settings1.Default.WelcomeTitle;
  33. DatabaseNameCu.Text = Settings1.Default.DatabaseName;
  34. DatabaseSubtitleCu.Text = Settings1.Default.DatabaseSubtitle;
  35. PasswordCu.Text = Settings1.Default.Password;
  36. DatabaseNameBox.Text = null;
  37. DatabaseSubtitleBox.Text = null;
  38. WelcomeTitleBox.Text = null;
  39. UsernameBox.Text = null;
  40. PasswordBox.Text = null;
  41. }
  42. private void Button_Click(object sender, RoutedEventArgs e)
  43. {
  44. if (!string.IsNullOrEmpty(DatabaseNameBox.Text))
  45. Settings1.Default.DatabaseName = DatabaseNameBox.Text;
  46. if (!string.IsNullOrEmpty(DatabaseSubtitleBox.Text))
  47. Settings1.Default.DatabaseSubtitle = DatabaseSubtitleBox.Text;
  48. if (!string.IsNullOrEmpty(WelcomeTitleBox.Text))
  49. Settings1.Default.WelcomeTitle = WelcomeTitleBox.Text;
  50. if (!string.IsNullOrEmpty(UsernameBox.Text))
  51. Settings1.Default.Username = UsernameBox.Text;
  52. if (!string.IsNullOrEmpty(PasswordBox.Text))
  53. Settings1.Default.Password = PasswordBox.Text;
  54. Settings1.Default.Save();
  55. updateinfo();
  56. }
  57. private void TextChanged(object sender, TextChangedEventArgs e)
  58. {
  59. bool allTextBoxesEmpty = string.IsNullOrWhiteSpace(DatabaseNameBox.Text) &&
  60. string.IsNullOrWhiteSpace(DatabaseSubtitleBox.Text) &&
  61. string.IsNullOrWhiteSpace(WelcomeTitleBox.Text) &&
  62. string.IsNullOrWhiteSpace(UsernameBox.Text) &&
  63. string.IsNullOrWhiteSpace(PasswordBox.Text);
  64. settingSaveButton0.IsEnabled = !allTextBoxesEmpty;
  65. settingSaveButton1.IsEnabled = !allTextBoxesEmpty;
  66. }
  67. private void LoadFileContent()
  68. {
  69. // 获取用户文档文件夹路径
  70. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  71. // 构建目标文件路径
  72. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.txt");
  73. // 检查文件是否存在
  74. if (File.Exists(filePath))
  75. {
  76. // 读取文件内容
  77. lines = File.ReadAllLines(filePath).ToList();
  78. }
  79. else
  80. {
  81. lines = new List<string>();
  82. }
  83. }
  84. private void DisplayCurrentLine()
  85. {
  86. if (currentIndex >= 0 && currentIndex < lines.Count)
  87. {
  88. string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
  89. txtName.Text = parts.Length > 0 ? parts[0] : string.Empty;
  90. cmbGender.SelectedItem = GetComboBoxItem(cmbGender, parts.Length > 1 ? parts[1] : null);
  91. dpBirthDate.SelectedDate = parts.Length > 2 ? DateTime.Parse(parts[2]) : (DateTime?)null;
  92. cmbLevel.SelectedItem = GetComboBoxItem(cmbLevel, parts.Length > 3 ? parts[3] : null);
  93. cmbTag.SelectedItem = GetComboBoxItem(cmbTag, parts.Length > 4 ? parts[4] : null);
  94. cmbIDType.SelectedItem = GetComboBoxItem(cmbIDType, parts.Length > 5 ? parts[5] : null);
  95. txtIDNumber.Text = parts.Length > 6 ? parts[6] : string.Empty;
  96. cmbBirthplace.SelectedItem = GetComboBoxItem(cmbBirthplace, parts.Length > 7 ? parts[7] : null);
  97. cmbPoliticalStatus.SelectedItem = GetComboBoxItem(cmbPoliticalStatus, parts.Length > 8 ? parts[8] : null);
  98. cmbMaritalStatus.SelectedItem = GetComboBoxItem(cmbMaritalStatus, parts.Length > 9 ? parts[9] : null);
  99. txtAddress.Text = parts.Length > 10 ? parts[10] : string.Empty;
  100. txtFamilySituation.Text = parts.Length > 11 ? parts[11] : string.Empty;
  101. txtAcquaintancePlace.Text = parts.Length > 12 ? parts[12] : string.Empty;
  102. cmbRelativeStatus.SelectedItem = GetComboBoxItem(cmbRelativeStatus, parts.Length > 13 ? parts[13] : null);
  103. cmbEducation.SelectedItem = GetComboBoxItem(cmbEducation, parts.Length > 14 ? parts[14] : null);
  104. txtPrimarySchool.Text = parts.Length > 15 ? parts[15] : string.Empty;
  105. txtJuniorHighSchool.Text = parts.Length > 16 ? parts[16] : string.Empty;
  106. txtHighSchool.Text = parts.Length > 17 ? parts[17] : string.Empty;
  107. txtUniversity.Text = parts.Length > 18 ? parts[18] : string.Empty;
  108. txtGraduateSchool.Text = parts.Length > 19 ? parts[19] : string.Empty;
  109. txtWorkUnit.Text = parts.Length > 20 ? parts[20] : string.Empty;
  110. txtPhoneNumber.Text = parts.Length > 21 ? parts[21] : string.Empty;
  111. txtEmail.Text = parts.Length > 22 ? parts[22] : string.Empty;
  112. txtWeChat.Text = parts.Length > 23 ? parts[23] : string.Empty;
  113. txtQQ.Text = parts.Length > 24 ? parts[24] : string.Empty;
  114. txtOtherContact.Text = parts.Length > 25 ? parts[25] : string.Empty;
  115. // 更新图片显示控件
  116. if (parts.Length > 26 && !string.IsNullOrEmpty(parts[26]))
  117. {
  118. try
  119. {
  120. BitmapImage bitmap = new BitmapImage();
  121. bitmap.BeginInit();
  122. bitmap.UriSource = new Uri(parts[26], UriKind.Absolute);
  123. bitmap.EndInit();
  124. imgPhoto.Source = bitmap;
  125. }
  126. catch
  127. {
  128. // 如果图片路径无效,清空图片控件
  129. imgPhoto.Source = null;
  130. }
  131. }
  132. else
  133. {
  134. // 如果没有图片路径,清空图片控件
  135. imgPhoto.Source = null;
  136. }
  137. }
  138. else
  139. {
  140. ClearControls();
  141. }
  142. }
  143. private void ClearControls()
  144. {
  145. txtName.Clear();
  146. cmbGender.SelectedItem = null;
  147. dpBirthDate.SelectedDate = new DateTime(1900, 1, 1); // 设置默认日期为 1900-01-01
  148. dpBirthDate.Text = "1900/1/1";
  149. cmbLevel.SelectedItem = null;
  150. cmbTag.SelectedItem = null;
  151. cmbIDType.SelectedItem = null;
  152. txtIDNumber.Clear();
  153. cmbBirthplace.SelectedItem = null;
  154. cmbPoliticalStatus.SelectedItem = null;
  155. cmbMaritalStatus.SelectedItem = null;
  156. txtAddress.Clear();
  157. txtFamilySituation.Clear();
  158. txtAcquaintancePlace.Clear();
  159. cmbRelativeStatus.SelectedItem = null;
  160. cmbEducation.SelectedItem = null;
  161. txtPrimarySchool.Clear();
  162. txtJuniorHighSchool.Clear();
  163. txtHighSchool.Clear();
  164. txtUniversity.Clear();
  165. txtGraduateSchool.Clear();
  166. txtWorkUnit.Clear();
  167. txtPhoneNumber.Clear();
  168. txtEmail.Clear();
  169. txtWeChat.Clear();
  170. txtQQ.Clear();
  171. txtOtherContact.Clear();
  172. imgPhoto.Source = null;
  173. }
  174. private object GetComboBoxItem(ComboBox comboBox, string content)
  175. {
  176. if (string.IsNullOrEmpty(content)) return null;
  177. foreach (var item in comboBox.Items)
  178. {
  179. if (item.ToString() == content)
  180. {
  181. return item;
  182. }
  183. }
  184. return null;
  185. }
  186. private void PreviousButton_Click(object sender, RoutedEventArgs e)
  187. {
  188. if (currentIndex > 0)
  189. {
  190. currentIndex--;
  191. DisplayCurrentLine();
  192. }
  193. }
  194. private void NextButton_Click(object sender, RoutedEventArgs e)
  195. {
  196. if (currentIndex < lines.Count - 1)
  197. {
  198. currentIndex++;
  199. DisplayCurrentLine();
  200. }
  201. }
  202. private void SaveButton_Click(object sender, RoutedEventArgs e)
  203. {
  204. // 获取所有控件的值
  205. string name = txtName.Text;
  206. string gender = cmbGender.SelectedItem?.ToString();
  207. string birthDate = dpBirthDate.SelectedDate?.ToString("yyyy-MM-dd") ?? string.Empty;
  208. string level = cmbLevel.SelectedItem?.ToString();
  209. string tag = cmbTag.SelectedItem?.ToString();
  210. string idType = cmbIDType.SelectedItem?.ToString();
  211. string idNumber = txtIDNumber.Text;
  212. string birthplace = cmbBirthplace.SelectedItem?.ToString();
  213. string politicalStatus = cmbPoliticalStatus.SelectedItem?.ToString();
  214. string maritalStatus = cmbMaritalStatus.SelectedItem?.ToString();
  215. string address = txtAddress.Text;
  216. string familySituation = txtFamilySituation.Text;
  217. string acquaintancePlace = txtAcquaintancePlace.Text;
  218. string relativeStatus = cmbRelativeStatus.SelectedItem?.ToString();
  219. string education = cmbEducation.SelectedItem?.ToString();
  220. string primarySchool = txtPrimarySchool.Text;
  221. string juniorHighSchool = txtJuniorHighSchool.Text;
  222. string highSchool = txtHighSchool.Text;
  223. string university = txtUniversity.Text;
  224. string graduateSchool = txtGraduateSchool.Text;
  225. string workUnit = txtWorkUnit.Text;
  226. string phoneNumber = txtPhoneNumber.Text;
  227. string email = txtEmail.Text;
  228. string weChat = txtWeChat.Text;
  229. string qq = txtQQ.Text;
  230. string otherContact = txtOtherContact.Text;
  231. string photoPath = imgPhoto.Source?.ToString() ?? string.Empty;
  232. // 构建保存的内容,使用两个星号 ** 分割每一列
  233. string content = string.Join("**", new string[]
  234. {
  235. name, gender, birthDate, level, tag, idType, idNumber, birthplace,
  236. politicalStatus, maritalStatus, address, familySituation, acquaintancePlace,
  237. relativeStatus, education, primarySchool, juniorHighSchool, highSchool,
  238. university, graduateSchool, workUnit, phoneNumber, email, weChat, qq, otherContact, photoPath
  239. });
  240. // 获取用户文档文件夹路径
  241. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  242. // 构建目标文件路径
  243. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.txt");
  244. // 确保目标文件夹存在
  245. string directoryPath = Path.GetDirectoryName(filePath);
  246. if (!Directory.Exists(directoryPath))
  247. {
  248. Directory.CreateDirectory(directoryPath);
  249. }
  250. if (currentIndex >= 0 && currentIndex < lines.Count)
  251. {
  252. // 修改当前行的内容
  253. lines[currentIndex] = content;
  254. isNewRecord = false; // 重置新建记录标志
  255. }
  256. else
  257. {
  258. // 新增一行
  259. lines.Add(content);
  260. }
  261. // 保存文件内容
  262. File.WriteAllLines(filePath, lines);
  263. MessageBox.Show("保存成功!");
  264. }
  265. private void NewButton_Click(object sender, RoutedEventArgs e)
  266. {
  267. // 清空所有控件的内容
  268. ClearControls();
  269. // 如果是第一次点击新建按钮,添加一个空行
  270. if (!isNewRecord)
  271. {
  272. lines.Add(string.Empty);
  273. isNewRecord = true;
  274. }
  275. // 设置当前索引为最后一行
  276. currentIndex = lines.Count - 1;
  277. // 显示新行
  278. DisplayCurrentLine();
  279. }
  280. private void ChangePhotoButton_Click(object sender, RoutedEventArgs e)
  281. {
  282. OpenFileDialog openFileDialog = new OpenFileDialog
  283. {
  284. Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png"
  285. };
  286. if (openFileDialog.ShowDialog() == true)
  287. {
  288. // 获取用户文档文件夹路径
  289. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  290. // 构建目标文件夹路径
  291. string targetDirectory = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people");
  292. // 确保目标文件夹存在
  293. if (!Directory.Exists(targetDirectory))
  294. {
  295. Directory.CreateDirectory(targetDirectory);
  296. }
  297. // 生成随机文件名
  298. string randomFileName = Guid.NewGuid().ToString() + Path.GetExtension(openFileDialog.FileName);
  299. string targetFilePath = Path.Combine(targetDirectory, randomFileName);
  300. // 复制文件到目标路径
  301. File.Copy(openFileDialog.FileName, targetFilePath, true);
  302. // 更新图片显示控件
  303. BitmapImage bitmap = new BitmapImage();
  304. bitmap.BeginInit();
  305. bitmap.UriSource = new Uri(targetFilePath, UriKind.Absolute);
  306. bitmap.EndInit();
  307. imgPhoto.Source = bitmap;
  308. // 如果当前行存在,更新文件路径到 content.txt 文件
  309. if (currentIndex >= 0 && currentIndex < lines.Count)
  310. {
  311. string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
  312. if (parts.Length > 26)
  313. {
  314. parts[26] = targetFilePath;
  315. }
  316. else
  317. {
  318. parts = parts.Concat(new string[] { targetFilePath }).ToArray();
  319. }
  320. lines[currentIndex] = string.Join("**", parts);
  321. SaveFileContent();
  322. }
  323. }
  324. }
  325. private void SaveFileContent()
  326. {
  327. // 获取用户文档文件夹路径
  328. string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  329. // 构建目标文件路径
  330. string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.txt");
  331. // 确保目标文件夹存在
  332. string directoryPath = Path.GetDirectoryName(filePath);
  333. if (!Directory.Exists(directoryPath))
  334. {
  335. Directory.CreateDirectory(directoryPath);
  336. }
  337. // 保存文件内容
  338. File.WriteAllLines(filePath, lines);
  339. }
  340. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  341. {
  342. if (currentIndex >= 0 && currentIndex < lines.Count)
  343. {
  344. // 如果当前处于编辑状态,删除当前行
  345. lines.RemoveAt(currentIndex);
  346. // 保存文件内容
  347. SaveFileContent();
  348. // 如果删除后还有行,显示上一行的内容
  349. if (currentIndex > 0)
  350. {
  351. currentIndex--;
  352. DisplayCurrentLine();
  353. }
  354. else
  355. {
  356. ClearControls();
  357. }
  358. }
  359. else
  360. {
  361. // 如果当前处于新建状态,清空控件内容
  362. ClearControls();
  363. }
  364. }
  365. }
  366. }