using LB_VisionProcesses.Forms;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Serialization;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Diagnostics;
|
using System.Drawing;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
|
namespace LB_VisionProcesses.Forms
|
{
|
public partial class GlobalVarForm : Form
|
{
|
public bool isSave = false;
|
private string fullPath = string.Empty;
|
public GlobalVarForm(string path = "")
|
{
|
InitializeComponent();
|
fullPath = path;
|
}
|
private void GlobalVarForm_Load(object sender, EventArgs e)
|
{
|
var sortedKeys = IProcess.dicGlobalVars.Keys
|
.OrderBy(k => k, new NaturalStringComparer())
|
.ToList();
|
int index = 0;
|
foreach (var key in sortedKeys)
|
{
|
GlobalVarControl control = new GlobalVarControl(key, IProcess.dicGlobalVars[key]);
|
control.Size = new Size(this.controlsPanel.Size.Width, control.Size.Height);
|
control.Location = new Point(0, control.Size.Height * index);
|
this.controlsPanel.Controls.Add(control);
|
index++;
|
}
|
}
|
private void controlsPanel_SizeChanged(object sender, EventArgs e)
|
{
|
int index = 0;
|
foreach (var control in controlsPanel.Controls)
|
{
|
if (control is GlobalVarControl globalVarControl)
|
{
|
globalVarControl.Size = new Size(this.controlsPanel.Size.Width, globalVarControl.Size.Height);
|
globalVarControl.Location = new Point(0, globalVarControl.Size.Height * index);
|
index++;
|
}
|
}
|
}
|
private void btnAdd_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
string name = "全局变量";
|
while (IProcess.dicGlobalVars.ContainsKey(name))
|
name += "(副本)";
|
GlobalVarControl control = new GlobalVarControl(name);
|
control.Size = new Size(this.controlsPanel.Size.Width, control.Size.Height);
|
control.Location = new Point(0, control.Size.Height * IProcess.dicGlobalVars.Count);
|
this.controlsPanel.Controls.Add(control);
|
IProcess.dicGlobalVars.TryAdd(name, "");
|
}
|
catch (Exception ex) { Debug.WriteLine($"btnAdd_Click失败:{ex.Message}【{ex.StackTrace}】"); }
|
}
|
private void btnDelete_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
List<GlobalVarControl> removeControls = new List<GlobalVarControl>();
|
foreach (var control in controlsPanel.Controls)
|
{
|
if (control is GlobalVarControl globalVarControl && globalVarControl.isSelected)
|
removeControls.Add(globalVarControl);
|
}
|
foreach (var control in removeControls)
|
controlsPanel.Controls.Remove(control);
|
}
|
catch (Exception ex) { Debug.WriteLine($"btnDelete_Click失败:{ex.Message}【{ex.StackTrace}】"); }
|
}
|
private void GlobalVarForm_FormClosing(object sender, FormClosingEventArgs e)
|
{
|
try
|
{
|
if (string.IsNullOrEmpty(fullPath))
|
{
|
e.Cancel = false; //确认关闭窗体
|
return;
|
}
|
DialogResult res = MessageBox.Show("是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); //保存结果信息
|
/// 参数1:显示文本,参数2:标题,参数3:按键类型,参数4:显示图标
|
if (res != DialogResult.Yes && res != DialogResult.No) //取消
|
{
|
e.Cancel = true; //取消关闭窗体
|
return;
|
}
|
if (res == DialogResult.Yes) //保存
|
{
|
IProcess.dicGlobalVars.Clear();
|
foreach (var control in controlsPanel.Controls)
|
{
|
if (control is GlobalVarControl globalVarControl)
|
IProcess.dicGlobalVars.AddOrUpdate(globalVarControl.Key, globalVarControl.Value
|
, (key, oldValue) => globalVarControl.Value);
|
}
|
string strJson = string.Empty;
|
var settings = new JsonSerializerSettings
|
{
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
// 自定义缩进(4空格)
|
ContractResolver = new DefaultContractResolver
|
{
|
NamingStrategy = new CamelCaseNamingStrategy()
|
}
|
};
|
strJson = JsonConvert.SerializeObject(IProcess.dicGlobalVars, settings);
|
string filePath = Path.GetDirectoryName(fullPath);
|
//判断文件夹是否存在,防呆输入为文件名称
|
if (!Directory.Exists(filePath))
|
{
|
try
|
{
|
Directory.CreateDirectory(filePath);
|
}
|
catch (Exception)
|
{ }
|
}
|
File.WriteAllText(fullPath, strJson, Encoding.UTF8);
|
e.Cancel = false; //确认关闭窗体
|
return;
|
}
|
e.Cancel = false; //确认关闭窗体
|
return;
|
}
|
catch { }
|
finally { }
|
}
|
}
|
}
|