Files
lightsetup/LightSetupConfig/FrmLightSteupConfig.cs
2023-05-22 09:29:58 +08:00

255 lines
7.2 KiB
C#

using LightSetupBase;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using System.Web;
namespace LightSetupConfig
{
public partial class FrmLightSteupConfig : Form
{
private string path;
private Md5Config md5Config = new Md5Config();
public FrmLightSteupConfig()
{
InitializeComponent();
}
private void FrmLightSteupConfig_Load(object sender, EventArgs e)
{
this.initBind();
}
private void bt_new_Click(object sender, EventArgs e)
{
NewFile();
}
private void bt_open_Click(object sender, EventArgs e)
{
Open();
}
private void bt_save_Click(object sender, EventArgs e)
{
Save();
}
private void tb_save_other_Click(object sender, EventArgs e)
{
SaveOther();
}
private void lb_list_SelectedIndexChanged(object sender, EventArgs e)
{
readToContent();
}
private void tb_content_TextChanged(object sender, EventArgs e)
{
writeFromContent();
}
private void FrmLightSetupConfig_KeyDown(object sender, KeyEventArgs e)
{
// ×éºÏ¼ü
if (e.KeyCode == Keys.O && e.Modifiers == Keys.Control) //Ctrl+O
{
Open();
}
else if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control) //Ctrl+S
{
Save();
}
else if (e.KeyCode == Keys.S && (int)e.Modifiers == ((int)Keys.Control + (int)Keys.Alt)) //Ctrl+Alt+S
{
SaveOther();
}
}
private void bt_new_MouseEnter(object sender, EventArgs e)
{
showStatus("¿ì½Ý¼ü: Ctrl + N , Alt + N ");
}
private void bt_open_MouseEnter(object sender, EventArgs e)
{
showStatus("¿ì½Ý¼ü: Ctrl + O , Alt + O ");
}
private void bt_save_MouseEnter(object sender, EventArgs e)
{
showStatus("¿ì½Ý¼ü: Ctrl + S £¬ Alt + S ");
}
private void bt_save_other_MouseEnter(object sender, EventArgs e)
{
showStatus("¿ì½Ý¼ü: Ctrl + Alt + S , Alt + W");
}
private void bt_open_MouseLeave(object sender, EventArgs e)
{
this.showStatus(String.Empty);
}
private void NewFile()
{
this.md5Config = new Md5Config();
this.initBind();
}
private void Open()
{
try
{
OpenFileDialog fbd = new OpenFileDialog();
fbd.Filter = "Image Files (*.config)|*.config";
if (fbd.ShowDialog() == DialogResult.OK)
{
this.path = fbd.FileName;
string json = File.ReadAllText(this.path, Encoding.UTF8);
this.md5Config = JsonSerializer.Deserialize<Md5Config>(json, Md5Config.getOption());
this.initBind();
}
}
catch (Exception ex)
{
showError(ex);
}
}
private void Save()
{
try
{
if (String.IsNullOrEmpty(this.path))
{
this.SaveOther();
}
else
{
this.saveHandle();
}
}
catch (Exception ex)
{
showError(ex);
}
}
private void SaveOther()
{
try
{
if (this.showSaveDialog())
{
this.saveHandle();
}
}
catch (Exception ex)
{
showError(ex);
}
}
private void saveHandle()
{
string json = JsonSerializer.Serialize(this.md5Config, Md5Config.getOption());
File.WriteAllText(this.path, json, Encoding.UTF8);
}
private bool showSaveDialog()
{
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Filter = "Image Files (*.config)|*.config";
//openImageDialog.Multiselect = false;
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
this.path = saveImageDialog.FileName;
return true;
}
return false;
}
private void initBind()
{
// Áбí
List<Md5ConfigAttributeProperty> list = new List<Md5ConfigAttributeProperty>();
// ÀàÐÍ
Type info = typeof(Md5Config);
PropertyInfo[] propertyInfos = info.GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
Md5ConfigAttribute? md5ConfigAttribute = propertyInfo.GetCustomAttribute<Md5ConfigAttribute>();
Md5ConfigAttributeProperty item = new Md5ConfigAttributeProperty();
item.PropertyName = propertyInfo.Name;
item.PropertyInfo = propertyInfo;
if (md5ConfigAttribute == null)
{
item.Name = propertyInfo.Name;
item.Comment = "ÇëÊäÈë×Ö¶Î" + propertyInfo.Name;
}
else
{
item.Name = md5ConfigAttribute.Name;
item.Comment = md5ConfigAttribute.Comment;
}
list.Add(item);
}
this.lb_list.DataSource = null;
this.lb_list.DisplayMember = "Name";
this.lb_list.ValueMember = "PropertyName";
this.lb_list.DataSource = list;
}
private void readToContent()
{
Md5ConfigAttributeProperty selectedItem = (Md5ConfigAttributeProperty)this.lb_list.SelectedItem;
if (selectedItem == null)
{
return;
}
this.showStatus(selectedItem.Comment);
this.tb_content.Text = (string)selectedItem.PropertyInfo.GetValue(this.md5Config);
}
private void writeFromContent()
{
Md5ConfigAttributeProperty selectedItem = (Md5ConfigAttributeProperty)this.lb_list.SelectedItem;
if (selectedItem == null)
{
return;
}
selectedItem.PropertyInfo.SetValue(this.md5Config, this.tb_content.Text);
}
private void showError(Exception ex)
{
MessageBox.Show(ex.ToString(), "´íÎóÌáʾ");
}
private void showStatus(string msg)
{
if (String.IsNullOrEmpty(msg) && String.IsNullOrEmpty(this.path))
{
this.lb_status.Text = "״̬À¸,Çë¼ÇµÃ±£´æÎļþ!";
}
else if (String.IsNullOrEmpty(msg))
{
this.lb_status.Text = "״̬À¸,Îļþ±£´æÂ·¾¶Îª:" + this.path;
}
else
{
this.lb_status.Text = msg;
}
}
}
}