391 lines
13 KiB
C#
391 lines
13 KiB
C#
using CleanMachineHMI.Domain.Enums;
|
|
using Krypton.Navigator;
|
|
using Krypton.Toolkit;
|
|
using LeadDataBaseUtility;
|
|
using Newtonsoft.Json;
|
|
using NX_LogUtility;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace CleanMachineHMI.Utils
|
|
{
|
|
/// <summary>
|
|
/// 窗体工具
|
|
/// </summary>
|
|
public static class frmUtil
|
|
{
|
|
public static List<ThemeLanguage> znCNLang = new List<ThemeLanguage>();
|
|
|
|
/// <summary>
|
|
/// 给一个字符串进行MD5加密
|
|
/// </summary>
|
|
/// <param name="strText">待加密字符串</param>
|
|
/// <returns>加密后的字符串</returns>
|
|
public static string MD5Encrypt(string strText)
|
|
{
|
|
try
|
|
{
|
|
MD5 md5 = MD5.Create();
|
|
byte[] buffer = Encoding.Default.GetBytes(strText);
|
|
byte[] md5buffer = md5.ComputeHash(buffer);
|
|
string str = null;
|
|
foreach (byte b in md5buffer)
|
|
{
|
|
str += b.ToString("x2");
|
|
}
|
|
return str;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置窗体容器
|
|
/// </summary>
|
|
/// <param name="objFrm"></param>
|
|
/// <param name="parePage"></param>
|
|
public static void SetPageForm(this KryptonForm objFrm, KryptonPage panelPage)
|
|
{
|
|
|
|
try
|
|
{
|
|
objFrm.TopLevel = false;
|
|
objFrm.FormBorderStyle = FormBorderStyle.None;
|
|
objFrm.Dock = DockStyle.Fill;
|
|
objFrm.MinimizeBox = false;
|
|
objFrm.Parent = panelPage;
|
|
objFrm.Show();
|
|
objFrm.Refresh();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 界面刷新
|
|
/// </summary>
|
|
/// <param name="kryptonNavigator"></param>
|
|
/// <param name="enumFormType"></param>
|
|
public static void SetNavPage(this KryptonNavigator kryptonNavigator, EnumFormType enumFormType)
|
|
{
|
|
|
|
try
|
|
{
|
|
Type frmType = Global.FrmModelList.Where(s => s.FormType == enumFormType).FirstOrDefault().Form;
|
|
KryptonPage newPage;
|
|
if (!kryptonNavigator.Pages.Where(s => s.Name == enumFormType.ToString()).Any())
|
|
{
|
|
newPage = new KryptonPage { Text = "", Name = enumFormType.ToString(), };
|
|
kryptonNavigator.Pages.Add(newPage);
|
|
}
|
|
else
|
|
{
|
|
newPage = kryptonNavigator.Pages.Where(s => s.Name == enumFormType.ToString()).FirstOrDefault();
|
|
}
|
|
|
|
kryptonNavigator.SelectedPage = newPage;
|
|
if (frmType.BaseType.Name == "UserControl")
|
|
{
|
|
var objFrm = (UserControl)Activator.CreateInstance(frmType);
|
|
objFrm.Dock = DockStyle.Fill;
|
|
objFrm.Parent = newPage;
|
|
objFrm.Show();
|
|
}
|
|
else
|
|
{
|
|
var objFrm = (KryptonForm)Activator.CreateInstance(frmType);
|
|
objFrm.TopLevel = false;
|
|
objFrm.FormBorderStyle = FormBorderStyle.None;
|
|
objFrm.Dock = DockStyle.Fill;
|
|
objFrm.Parent = newPage;
|
|
objFrm.Show();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 递归获取子控件
|
|
/// </summary>
|
|
/// <param name="control"></param>
|
|
/// <returns></returns>
|
|
public static List<Control> GetAllControls(Control control)
|
|
{
|
|
var list = new List<Control>();
|
|
foreach (Control con in control.Controls)
|
|
{
|
|
list.Add(con);
|
|
if (con.Controls.Count > 0)
|
|
{
|
|
list.AddRange(GetAllControls(con));
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设定窗体语言
|
|
/// </summary>
|
|
/// <param name="form">窗体对象</param>
|
|
/// <param name="languageCode">语言编码</param>
|
|
public static void SetFormControlText(this Control form)
|
|
{
|
|
try
|
|
{
|
|
var lstControls = GetAllControls(form);
|
|
lstControls.Add(form);
|
|
foreach (Control item in lstControls)
|
|
{
|
|
var key = form.Name + "|" + item.Name;
|
|
if (!Global.ThemeLanguage.ContainsKey(key))
|
|
{
|
|
continue;
|
|
}
|
|
#if DEBUG
|
|
AddzhCN(key, item.Text, "");
|
|
#endif
|
|
if (item is KryptonGroupBox)
|
|
{
|
|
((KryptonGroupBox)item).Values.Heading = Global.ThemeLanguage[key];
|
|
}
|
|
else
|
|
{
|
|
item.Text = Global.ThemeLanguage[key];
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
}
|
|
}
|
|
|
|
public static void SetSingleControlText(this Control form, Control control)
|
|
{
|
|
try
|
|
{
|
|
var key = form.Name + "|" + control.Name;
|
|
if (!Global.ThemeLanguage.ContainsKey(key))
|
|
{
|
|
control.Text = control.Name;
|
|
}
|
|
AddzhCN(key, control.Text, "");
|
|
|
|
control.Text = Global.ThemeLanguage[key];
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设定数据视图列文本
|
|
/// </summary>
|
|
/// <param name="dataGrid"></param>
|
|
/// <param name="formName"></param>
|
|
/// <param name="languageCode"></param>
|
|
public static void SetDataViewColumnText(this DataGridView dataGrid, string formName)
|
|
{
|
|
try
|
|
{
|
|
foreach (DataGridViewColumn item in dataGrid.Columns)
|
|
{
|
|
var key = formName + "|" + item.Name;
|
|
if (!Global.ThemeLanguage.ContainsKey(key))
|
|
{
|
|
continue;
|
|
}
|
|
AddzhCN(key, item.HeaderText, "");
|
|
item.HeaderText = Global.ThemeLanguage[key];
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取信息文本
|
|
/// </summary>
|
|
/// <param name="dataTable"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static string T(this Control form, string defaultValue, string infoCode)
|
|
{
|
|
try
|
|
{
|
|
var key = form.Name + "|" + infoCode;
|
|
if (!Global.ThemeLanguage.ContainsKey(key))
|
|
{
|
|
return defaultValue;
|
|
}
|
|
else
|
|
{
|
|
AddzhCN(key, defaultValue, "");
|
|
return Global.ThemeLanguage[key];
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static List<string> GetComBoboxItemArr(this Control form, string controlCode)
|
|
{
|
|
try
|
|
{
|
|
var key = form.Name + "|" + controlCode;
|
|
if (!Global.ThemeLanguage.ContainsKey(key))
|
|
{
|
|
return new List<string>();
|
|
}
|
|
else
|
|
{
|
|
var control = (KryptonComboBox)form.Controls.Find(controlCode, true)[0];
|
|
var str = "";
|
|
foreach (var item in control.Items)
|
|
{
|
|
str += $"{item},";
|
|
}
|
|
|
|
AddzhCN(key, "", string.IsNullOrEmpty(str) ? Global.ThemeLanguage[key] : str.TrimEnd(new char[] { ',' }));
|
|
return Global.ThemeLanguage[key].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message, LogTypeEum.Err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static void DB2Json()
|
|
{
|
|
var tmpThemeLanguage = new List<ThemeLanguage>();
|
|
var table = LeadDbHelperSQLite.Query("SELECT FORM_CODE,CONTROL_CODE,CONTROL_NAME FROM SYS_LANGUAGE_CONTROL WHERE FORM_CODE <> 'frmMain'").Tables[0];
|
|
foreach (DataRow row in table.Rows)
|
|
{
|
|
|
|
var part = tmpThemeLanguage.FirstOrDefault(x => x.formCode == row["FORM_CODE"].ToString() && x.controlCode == row["CONTROL_CODE"].ToString());
|
|
if (part == null)
|
|
{
|
|
tmpThemeLanguage.Add(new ThemeLanguage
|
|
{
|
|
formCode = row["FORM_CODE"].ToString(),
|
|
controlCode = row["CONTROL_CODE"].ToString(),
|
|
controlName = row["CONTROL_NAME"].ToString(),
|
|
});
|
|
}
|
|
else
|
|
{
|
|
if (part.options == null)
|
|
{
|
|
part.options = new List<string> { part.controlName, row["CONTROL_NAME"].ToString() };
|
|
part.controlName = null;
|
|
}
|
|
else
|
|
{
|
|
part.options.Add(row["CONTROL_NAME"].ToString());
|
|
}
|
|
}
|
|
}
|
|
var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(tmpThemeLanguage);
|
|
WriteLangFile("en.json", str1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 写文本文件
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="txt"></param>
|
|
private static void WriteLangFile(string fileName, string txt)
|
|
{
|
|
// var fileDir = System.Windows.Forms.Application.StartupPath.Split(new string[] { @"bin\Debug" }, StringSplitOptions.RemoveEmptyEntries)[0] + "Lang\\";
|
|
var fileDir = "Lang\\";
|
|
try
|
|
{
|
|
if (!Directory.Exists(fileDir))//如果文件夹不存在,就新建一个文件夹。
|
|
{
|
|
Directory.CreateDirectory(fileDir);
|
|
}
|
|
File.WriteAllText(fileDir + fileName, txt);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
private static void AddzhCN(string key, string name, string options)
|
|
{
|
|
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(options))
|
|
{
|
|
Console.WriteLine(key + "空");
|
|
return;
|
|
}
|
|
var codes = key.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (znCNLang.Any(x => x.formCode == codes[0] && x.controlCode == codes[1]))
|
|
{
|
|
return;
|
|
}
|
|
znCNLang.Add(new ThemeLanguage
|
|
{
|
|
formCode = codes[0],
|
|
controlCode = codes[1],
|
|
controlName = string.IsNullOrEmpty(options) ? name : null,
|
|
options = string.IsNullOrEmpty(options) ? null : options.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(),
|
|
});
|
|
var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(znCNLang);
|
|
WriteLangFile("zh-CN.json", str1);
|
|
}
|
|
}
|
|
|
|
public class ThemeLanguage
|
|
{
|
|
/// <summary>
|
|
/// 窗体编码
|
|
/// </summary>
|
|
public string formCode { get; set; }
|
|
/// <summary>
|
|
/// 控件编码
|
|
/// </summary>
|
|
public string controlCode { get; set; }
|
|
/// <summary>
|
|
/// 控件名称
|
|
/// </summary>
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
public string controlName { get; set; }
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
public List<string> options { get; set; }
|
|
}
|
|
}
|