123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- using System.IO;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using Microsoft.Win32;
- namespace ArchivesCenter3
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- updateinfo();
- LoadFileContent();
- Version version = Assembly.GetExecutingAssembly().GetName().Version;
- versionTextBlock.Text = $"版本号:{version.Major}.{version.Minor}.{version.Build}";
- }
- private List<string> lines; // 存储文件内容的列表
- private int currentIndex = -1; // 当前行索引
- private bool isNewRecord = false; // 标记是否是新建记录
- private void updateinfo()
- {
- welcomeText.Text = "欢迎!" + Settings1.Default.Username + " ";
- WelcomeTitle.Text = Settings1.Default.WelcomeTitle;
- DatabaseName.Text = Settings1.Default.DatabaseName;
- DatabaseSubtitle.Text = Settings1.Default.DatabaseSubtitle;
- UsernameCu.Text = Settings1.Default.Username;
- WelcomeTitleCu.Text = Settings1.Default.WelcomeTitle;
- DatabaseNameCu.Text = Settings1.Default.DatabaseName;
- DatabaseSubtitleCu.Text = Settings1.Default.DatabaseSubtitle;
- PasswordCu.Text = Settings1.Default.Password;
- DatabaseNameBox.Text = null;
- DatabaseSubtitleBox.Text = null;
- WelcomeTitleBox.Text = null;
- UsernameBox.Text = null;
- PasswordBox.Text = null;
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- if (!string.IsNullOrEmpty(DatabaseNameBox.Text))
- Settings1.Default.DatabaseName = DatabaseNameBox.Text;
- if (!string.IsNullOrEmpty(DatabaseSubtitleBox.Text))
- Settings1.Default.DatabaseSubtitle = DatabaseSubtitleBox.Text;
- if (!string.IsNullOrEmpty(WelcomeTitleBox.Text))
- Settings1.Default.WelcomeTitle = WelcomeTitleBox.Text;
- if (!string.IsNullOrEmpty(UsernameBox.Text))
- Settings1.Default.Username = UsernameBox.Text;
- if (!string.IsNullOrEmpty(PasswordBox.Text))
- Settings1.Default.Password = PasswordBox.Text;
- Settings1.Default.Save();
- updateinfo();
- }
- private void TextChanged(object sender, TextChangedEventArgs e)
- {
- bool allTextBoxesEmpty = string.IsNullOrWhiteSpace(DatabaseNameBox.Text) &&
- string.IsNullOrWhiteSpace(DatabaseSubtitleBox.Text) &&
- string.IsNullOrWhiteSpace(WelcomeTitleBox.Text) &&
- string.IsNullOrWhiteSpace(UsernameBox.Text) &&
- string.IsNullOrWhiteSpace(PasswordBox.Text);
- settingSaveButton0.IsEnabled = !allTextBoxesEmpty;
- settingSaveButton1.IsEnabled = !allTextBoxesEmpty;
- }
- private void LoadFileContent()
- {
- // 获取用户文档文件夹路径
- string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- // 构建目标文件路径
- string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.txt");
- // 检查文件是否存在
- if (File.Exists(filePath))
- {
- // 读取文件内容
- lines = File.ReadAllLines(filePath).ToList();
- }
- else
- {
- lines = new List<string>();
- }
- }
- private void DisplayCurrentLine()
- {
- if (currentIndex >= 0 && currentIndex < lines.Count)
- {
- string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
- txtName.Text = parts.Length > 0 ? parts[0] : string.Empty;
- cmbGender.SelectedItem = GetComboBoxItem(cmbGender, parts.Length > 1 ? parts[1] : null);
- dpBirthDate.SelectedDate = parts.Length > 2 ? DateTime.Parse(parts[2]) : (DateTime?)null;
- cmbLevel.SelectedItem = GetComboBoxItem(cmbLevel, parts.Length > 3 ? parts[3] : null);
- cmbTag.SelectedItem = GetComboBoxItem(cmbTag, parts.Length > 4 ? parts[4] : null);
- cmbIDType.SelectedItem = GetComboBoxItem(cmbIDType, parts.Length > 5 ? parts[5] : null);
- txtIDNumber.Text = parts.Length > 6 ? parts[6] : string.Empty;
- cmbBirthplace.SelectedItem = GetComboBoxItem(cmbBirthplace, parts.Length > 7 ? parts[7] : null);
- cmbPoliticalStatus.SelectedItem = GetComboBoxItem(cmbPoliticalStatus, parts.Length > 8 ? parts[8] : null);
- cmbMaritalStatus.SelectedItem = GetComboBoxItem(cmbMaritalStatus, parts.Length > 9 ? parts[9] : null);
- txtAddress.Text = parts.Length > 10 ? parts[10] : string.Empty;
- txtFamilySituation.Text = parts.Length > 11 ? parts[11] : string.Empty;
- txtAcquaintancePlace.Text = parts.Length > 12 ? parts[12] : string.Empty;
- cmbRelativeStatus.SelectedItem = GetComboBoxItem(cmbRelativeStatus, parts.Length > 13 ? parts[13] : null);
- cmbEducation.SelectedItem = GetComboBoxItem(cmbEducation, parts.Length > 14 ? parts[14] : null);
- txtPrimarySchool.Text = parts.Length > 15 ? parts[15] : string.Empty;
- txtJuniorHighSchool.Text = parts.Length > 16 ? parts[16] : string.Empty;
- txtHighSchool.Text = parts.Length > 17 ? parts[17] : string.Empty;
- txtUniversity.Text = parts.Length > 18 ? parts[18] : string.Empty;
- txtGraduateSchool.Text = parts.Length > 19 ? parts[19] : string.Empty;
- txtWorkUnit.Text = parts.Length > 20 ? parts[20] : string.Empty;
- txtPhoneNumber.Text = parts.Length > 21 ? parts[21] : string.Empty;
- txtEmail.Text = parts.Length > 22 ? parts[22] : string.Empty;
- txtWeChat.Text = parts.Length > 23 ? parts[23] : string.Empty;
- txtQQ.Text = parts.Length > 24 ? parts[24] : string.Empty;
- txtOtherContact.Text = parts.Length > 25 ? parts[25] : string.Empty;
- // 更新图片显示控件
- if (parts.Length > 26 && !string.IsNullOrEmpty(parts[26]))
- {
- try
- {
- BitmapImage bitmap = new BitmapImage();
- bitmap.BeginInit();
- bitmap.UriSource = new Uri(parts[26], UriKind.Absolute);
- bitmap.EndInit();
- imgPhoto.Source = bitmap;
- }
- catch
- {
- // 如果图片路径无效,清空图片控件
- imgPhoto.Source = null;
- }
- }
- else
- {
- // 如果没有图片路径,清空图片控件
- imgPhoto.Source = null;
- }
- }
- else
- {
- ClearControls();
- }
- }
- private void ClearControls()
- {
- txtName.Clear();
- cmbGender.SelectedItem = null;
- dpBirthDate.SelectedDate = new DateTime(1900, 1, 1); // 设置默认日期为 1900-01-01
- dpBirthDate.Text = "1900/1/1";
- cmbLevel.SelectedItem = null;
- cmbTag.SelectedItem = null;
- cmbIDType.SelectedItem = null;
- txtIDNumber.Clear();
- cmbBirthplace.SelectedItem = null;
- cmbPoliticalStatus.SelectedItem = null;
- cmbMaritalStatus.SelectedItem = null;
- txtAddress.Clear();
- txtFamilySituation.Clear();
- txtAcquaintancePlace.Clear();
- cmbRelativeStatus.SelectedItem = null;
- cmbEducation.SelectedItem = null;
- txtPrimarySchool.Clear();
- txtJuniorHighSchool.Clear();
- txtHighSchool.Clear();
- txtUniversity.Clear();
- txtGraduateSchool.Clear();
- txtWorkUnit.Clear();
- txtPhoneNumber.Clear();
- txtEmail.Clear();
- txtWeChat.Clear();
- txtQQ.Clear();
- txtOtherContact.Clear();
- imgPhoto.Source = null;
- }
- private object GetComboBoxItem(ComboBox comboBox, string content)
- {
- if (string.IsNullOrEmpty(content)) return null;
- foreach (var item in comboBox.Items)
- {
- if (item.ToString() == content)
- {
- return item;
- }
- }
- return null;
- }
- private void PreviousButton_Click(object sender, RoutedEventArgs e)
- {
- if (currentIndex > 0)
- {
- currentIndex--;
- DisplayCurrentLine();
- }
- }
- private void NextButton_Click(object sender, RoutedEventArgs e)
- {
- if (currentIndex < lines.Count - 1)
- {
- currentIndex++;
- DisplayCurrentLine();
- }
- }
- private void SaveButton_Click(object sender, RoutedEventArgs e)
- {
- // 获取所有控件的值
- string name = txtName.Text;
- string gender = cmbGender.SelectedItem?.ToString();
- string birthDate = dpBirthDate.SelectedDate?.ToString("yyyy-MM-dd") ?? string.Empty;
- string level = cmbLevel.SelectedItem?.ToString();
- string tag = cmbTag.SelectedItem?.ToString();
- string idType = cmbIDType.SelectedItem?.ToString();
- string idNumber = txtIDNumber.Text;
- string birthplace = cmbBirthplace.SelectedItem?.ToString();
- string politicalStatus = cmbPoliticalStatus.SelectedItem?.ToString();
- string maritalStatus = cmbMaritalStatus.SelectedItem?.ToString();
- string address = txtAddress.Text;
- string familySituation = txtFamilySituation.Text;
- string acquaintancePlace = txtAcquaintancePlace.Text;
- string relativeStatus = cmbRelativeStatus.SelectedItem?.ToString();
- string education = cmbEducation.SelectedItem?.ToString();
- string primarySchool = txtPrimarySchool.Text;
- string juniorHighSchool = txtJuniorHighSchool.Text;
- string highSchool = txtHighSchool.Text;
- string university = txtUniversity.Text;
- string graduateSchool = txtGraduateSchool.Text;
- string workUnit = txtWorkUnit.Text;
- string phoneNumber = txtPhoneNumber.Text;
- string email = txtEmail.Text;
- string weChat = txtWeChat.Text;
- string qq = txtQQ.Text;
- string otherContact = txtOtherContact.Text;
- string photoPath = imgPhoto.Source?.ToString() ?? string.Empty;
- // 构建保存的内容,使用两个星号 ** 分割每一列
- string content = string.Join("**", new string[]
- {
- name, gender, birthDate, level, tag, idType, idNumber, birthplace,
- politicalStatus, maritalStatus, address, familySituation, acquaintancePlace,
- relativeStatus, education, primarySchool, juniorHighSchool, highSchool,
- university, graduateSchool, workUnit, phoneNumber, email, weChat, qq, otherContact, photoPath
- });
- // 获取用户文档文件夹路径
- string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- // 构建目标文件路径
- string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.txt");
- // 确保目标文件夹存在
- string directoryPath = Path.GetDirectoryName(filePath);
- if (!Directory.Exists(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- if (currentIndex >= 0 && currentIndex < lines.Count)
- {
- // 修改当前行的内容
- lines[currentIndex] = content;
- isNewRecord = false; // 重置新建记录标志
- }
- else
- {
- // 新增一行
- lines.Add(content);
- }
- // 保存文件内容
- File.WriteAllLines(filePath, lines);
- MessageBox.Show("保存成功!");
- }
- private void NewButton_Click(object sender, RoutedEventArgs e)
- {
- // 清空所有控件的内容
- ClearControls();
- // 如果是第一次点击新建按钮,添加一个空行
- if (!isNewRecord)
- {
- lines.Add(string.Empty);
- isNewRecord = true;
- }
- // 设置当前索引为最后一行
- currentIndex = lines.Count - 1;
- // 显示新行
- DisplayCurrentLine();
- }
- private void ChangePhotoButton_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog
- {
- Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png"
- };
- if (openFileDialog.ShowDialog() == true)
- {
- // 获取用户文档文件夹路径
- string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- // 构建目标文件夹路径
- string targetDirectory = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people");
- // 确保目标文件夹存在
- if (!Directory.Exists(targetDirectory))
- {
- Directory.CreateDirectory(targetDirectory);
- }
- // 生成随机文件名
- string randomFileName = Guid.NewGuid().ToString() + Path.GetExtension(openFileDialog.FileName);
- string targetFilePath = Path.Combine(targetDirectory, randomFileName);
- // 复制文件到目标路径
- File.Copy(openFileDialog.FileName, targetFilePath, true);
- // 更新图片显示控件
- BitmapImage bitmap = new BitmapImage();
- bitmap.BeginInit();
- bitmap.UriSource = new Uri(targetFilePath, UriKind.Absolute);
- bitmap.EndInit();
- imgPhoto.Source = bitmap;
- // 如果当前行存在,更新文件路径到 content.txt 文件
- if (currentIndex >= 0 && currentIndex < lines.Count)
- {
- string[] parts = lines[currentIndex].Split(new string[] { "**" }, StringSplitOptions.None);
- if (parts.Length > 26)
- {
- parts[26] = targetFilePath;
- }
- else
- {
- parts = parts.Concat(new string[] { targetFilePath }).ToArray();
- }
- lines[currentIndex] = string.Join("**", parts);
- SaveFileContent();
- }
- }
- }
- private void SaveFileContent()
- {
- // 获取用户文档文件夹路径
- string userDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- // 构建目标文件路径
- string filePath = Path.Combine(userDocumentsPath, "ArchivesCenter", "data", "people", "content.txt");
- // 确保目标文件夹存在
- string directoryPath = Path.GetDirectoryName(filePath);
- if (!Directory.Exists(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- // 保存文件内容
- File.WriteAllLines(filePath, lines);
- }
- private void DeleteButton_Click(object sender, RoutedEventArgs e)
- {
- if (currentIndex >= 0 && currentIndex < lines.Count)
- {
- // 如果当前处于编辑状态,删除当前行
- lines.RemoveAt(currentIndex);
- // 保存文件内容
- SaveFileContent();
- // 如果删除后还有行,显示上一行的内容
- if (currentIndex > 0)
- {
- currentIndex--;
- DisplayCurrentLine();
- }
- else
- {
- ClearControls();
- }
- }
- else
- {
- // 如果当前处于新建状态,清空控件内容
- ClearControls();
- }
- }
- }
- }
|