using Autofac;
|
using Bro.Common.Factory;
|
using Bro.Common.Interface;
|
using Bro.Common.Model;
|
using Bro.Common.Model.Forms;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.ComponentModel.Design;
|
using System.Drawing.Design;
|
using System.Drawing.Printing;
|
using System.Globalization;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using System.Windows.Forms.Design;
|
|
namespace Bro.Common.Helper
|
{
|
#region TypeConvert
|
public class SerialPortConverter : StringConverter
|
{
|
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
{
|
List<string> portNames = SerialPort.GetPortNames().ToList();
|
//if (portNames.Count == 0)
|
//{
|
// portNames.Add("当前无可用串口");
|
//}
|
|
return new StandardValuesCollection(portNames);
|
}
|
}
|
|
public class ExcelTypeConverter : StringConverter
|
{
|
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
{
|
List<string> portNames = new List<string>() { "Excel文件", "CSV文件" };
|
//if (portNames.Count == 0)
|
//{
|
// portNames.Add("当前无可用串口");
|
//}
|
|
return new StandardValuesCollection(portNames);
|
}
|
}
|
|
public class BaudRateConverter : StringConverter
|
{
|
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
{
|
List<string> bauds = new List<string>()
|
{
|
"9600",
|
"19200",
|
"38400",
|
"43000",
|
"56000",
|
"57600",
|
"115200",
|
};
|
|
return new StandardValuesCollection(bauds);
|
}
|
}
|
|
public abstract class ComboBoxItemTypeConvert : TypeConverter
|
{
|
public Hashtable _hash = null;
|
|
public ComboBoxItemTypeConvert()
|
{
|
_hash = new Hashtable();
|
|
GetConvertHash();
|
}
|
|
public abstract void GetConvertHash();
|
|
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
{
|
string[] ids = new string[_hash.Values.Count];
|
int i = 0;
|
|
foreach (DictionaryEntry myDE in _hash)
|
{
|
ids[i++] = (myDE.Key).ToString();
|
}
|
|
return new StandardValuesCollection(ids);
|
}
|
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
{
|
if (sourceType == typeof(string))
|
{
|
return true;
|
}
|
|
return base.CanConvertFrom(context, sourceType);
|
}
|
|
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object v)
|
{
|
if (v is string)
|
{
|
foreach (DictionaryEntry myDE in _hash)
|
{
|
if (myDE.Value.Equals((v.ToString())))
|
return myDE.Key;
|
}
|
}
|
|
return base.ConvertFrom(context, culture, v);
|
}
|
|
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object v, Type destinationType)
|
{
|
if (destinationType == typeof(string))
|
{
|
foreach (DictionaryEntry myDE in _hash)
|
{
|
if (myDE.Key.Equals(v))
|
return myDE.Value.ToString();
|
}
|
return "";
|
}
|
|
return base.ConvertTo(context, culture, v, destinationType);
|
}
|
}
|
|
public class NoEditStringConvert : StringConverter
|
{
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
}
|
|
public class ComplexObjectConvert : StringConverter
|
{
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
{
|
//return base.ConvertTo(context, culture, value, destinationType);
|
if (value != null)
|
{
|
if (value is IComplexDisplay)
|
{
|
return (value as IComplexDisplay).GetDisplayText();
|
}
|
else
|
{
|
return JsonConvert.SerializeObject(value);
|
}
|
}
|
else
|
{
|
return "NULL";
|
}
|
}
|
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
{
|
if (value.ToString() != "NULL")
|
{
|
return JsonConvert.DeserializeObject(value.ToString());
|
}
|
else
|
{
|
return null;
|
}
|
}
|
}
|
|
public class CollectionCountConvert : TypeConverter
|
{
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
{
|
if (value is ICollection)
|
{
|
return (value as ICollection).Count.ToString();
|
}
|
else
|
{
|
return base.ConvertTo(context, culture, value, destinationType);
|
}
|
}
|
}
|
|
public class SimpleCollectionConvert<T> : TypeConverter
|
{
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
{
|
if (value is IEnumerable<T>)
|
{
|
return String.Join(",", (value as IEnumerable<T>));
|
}
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
}
|
}
|
|
public class ShortTimeConverter : StringConverter
|
{
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
{
|
if (value is DateTime?)
|
{
|
if (value == null)
|
{
|
return "";
|
}
|
else
|
{
|
return ((DateTime)value).ToString("mm:ss.fff");
|
}
|
}
|
else
|
{
|
return base.ConvertTo(context, culture, value, destinationType);
|
}
|
}
|
}
|
|
public class ShortTimeHHmmConverter : StringConverter
|
{
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
{
|
return DateTime.Parse(value.ToString());
|
}
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
{
|
if (value is DateTime?)
|
{
|
if (value == null)
|
{
|
return "";
|
}
|
else
|
{
|
return ((DateTime)value).ToString("HH:mm");
|
}
|
}
|
else
|
{
|
return base.ConvertTo(context, culture, value, destinationType);
|
}
|
}
|
}
|
|
public class PrinterConverter : StringConverter
|
{
|
public string Filter { get; set; }
|
|
public PrinterConverter() : base() { }
|
|
public PrinterConverter(string filter) : base()
|
{
|
Filter = filter;
|
}
|
|
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
{
|
List<string> printerList = new List<string>();
|
foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
|
{
|
if (!string.IsNullOrWhiteSpace(Filter))
|
{
|
if (sPrint.Contains("Deli"))
|
{
|
printerList.Add(sPrint);
|
}
|
}
|
else
|
{
|
printerList.Add(sPrint);
|
}
|
}
|
|
return new StandardValuesCollection(printerList);
|
}
|
|
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
}
|
#endregion
|
|
#region UITypeEditor
|
public class FoldDialogEditor : UITypeEditor
|
{
|
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.Modal;
|
}
|
|
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
|
{
|
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
|
|
if (edSvc != null)
|
{
|
// 可以打开任何特定的对话框
|
FolderBrowserDialog dialog = new FolderBrowserDialog();
|
|
if (dialog.ShowDialog().Equals(DialogResult.OK))
|
{
|
return dialog.SelectedPath;
|
}
|
}
|
return value;
|
}
|
}
|
|
public class FileDialogEditor : UITypeEditor
|
{
|
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.Modal;
|
}
|
|
AutoResetEvent _dialogCloseHandle = new AutoResetEvent(false);
|
string _fileName = "";
|
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
|
{
|
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
|
|
if (edSvc != null)
|
{
|
// 可以打开任何特定的对话框
|
//OpenFileDialog dialog = new OpenFileDialog();
|
//dialog.AddExtension = false;
|
|
//if (dialog.ShowDialog().Equals(DialogResult.OK))
|
//{
|
// return dialog.FileName;
|
//}
|
|
Thread InvokeThread = new Thread(new ThreadStart(OpenFile));
|
InvokeThread.SetApartmentState(ApartmentState.STA);
|
InvokeThread.Start();
|
InvokeThread.Join();
|
|
_dialogCloseHandle.WaitOne();
|
|
return _fileName;
|
}
|
return value;
|
}
|
|
private void OpenFile()
|
{
|
OpenFileDialog dialog = new OpenFileDialog();
|
dialog.AddExtension = false;
|
|
if (dialog.ShowDialog().Equals(DialogResult.OK))
|
{
|
_fileName = dialog.FileName;
|
_dialogCloseHandle.Set();
|
}
|
}
|
}
|
|
public class PropertyObjectEditor : UITypeEditor
|
{
|
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.Modal;
|
}
|
|
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
|
{
|
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
|
|
if (edSvc != null)
|
{
|
Form form = new Form();
|
form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
|
form.StartPosition = FormStartPosition.CenterParent;
|
form.Text = context.PropertyDescriptor.DisplayName + "--" + context.PropertyDescriptor.Description;
|
|
PropertyGrid pg = new PropertyGrid();
|
pg.Dock = DockStyle.Fill;
|
pg.ToolbarVisible = false;
|
|
pg.SelectedObject = value;
|
|
form.Controls.Add(pg);
|
|
form.ShowDialog();
|
|
value = pg.SelectedObject;
|
return value;
|
}
|
|
return base.EditValue(context, provider, value);
|
}
|
}
|
|
public class IOperationConfigEditor : UITypeEditor
|
{
|
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.Modal;
|
}
|
|
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
|
{
|
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
|
|
if (edSvc != null)
|
{
|
if (value == null)
|
{
|
string methodCode = ((MonitorSet)context.Instance).MethodCode;
|
using (var scope = GlobalVar.Container.BeginLifetimeScope())
|
{
|
List<ProcessMethodAttribute> methodList = scope.Resolve<List<ProcessMethodAttribute>>();
|
var method = methodList.FirstOrDefault(u => u.MethodCode == methodCode);
|
|
if (method != null)
|
{
|
value = ConfigFactory.GetOperationConfig(method.DeviceType);
|
}
|
}
|
}
|
|
FrmOpConfigEdit frm = new FrmOpConfigEdit(((MonitorSet)context.Instance).MethodCode, value as IOperationConfig);
|
if (frm.ShowDialog() == DialogResult.OK)
|
{
|
return frm.OpConfig;
|
}
|
else
|
{
|
return frm.BackupConfig;
|
}
|
|
//Form form = new Form();
|
//form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
|
//form.StartPosition = FormStartPosition.CenterParent;
|
//form.Text = context.PropertyDescriptor.DisplayName + "--" + context.PropertyDescriptor.Description;
|
|
//PropertyGrid pg = new PropertyGrid();
|
//pg.Dock = DockStyle.Fill;
|
//pg.ToolbarVisible = false;
|
|
//pg.SelectedObject = value;
|
|
//form.Controls.Add(pg);
|
|
//form.ShowDialog();
|
|
//value = pg.SelectedObject;
|
//return value;
|
}
|
|
return base.EditValue(context, provider, value);
|
}
|
}
|
|
public class PropertyObjectDropDownEditor : UITypeEditor
|
{
|
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.DropDown;
|
}
|
|
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
|
{
|
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
|
|
if (edSvc != null)
|
{
|
UserControl form = new UserControl();
|
//form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
|
//form.StartPosition = FormStartPosition.CenterParent;
|
form.Text = context.PropertyDescriptor.DisplayName + "--" + context.PropertyDescriptor.Description;
|
|
PropertyGrid pg = new PropertyGrid();
|
pg.Dock = DockStyle.Fill;
|
pg.ToolbarVisible = false;
|
|
pg.SelectedObject = value;
|
|
form.Controls.Add(pg);
|
|
//form.ShowDialog();
|
edSvc.DropDownControl(form);
|
|
value = pg.SelectedObject;
|
return value;
|
}
|
|
return base.EditValue(context, provider, value);
|
}
|
}
|
|
//public class DropDownEditor : UITypeEditor
|
//{
|
// public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
// {
|
// return UITypeEditorEditStyle.DropDown;
|
// }
|
//}
|
|
public class SimpleCollectionEditor<T> : UITypeEditor
|
{
|
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.Modal;
|
}
|
|
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
|
{
|
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
|
|
if (edSvc != null)
|
{
|
Form form = new Form();
|
form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
|
form.StartPosition = FormStartPosition.CenterParent;
|
form.Text = context.PropertyDescriptor.DisplayName + "--" + context.PropertyDescriptor.Description;
|
|
TextBox tbox = new TextBox();
|
tbox.Dock = DockStyle.Fill;
|
tbox.Multiline = true;
|
form.Controls.Add(tbox);
|
|
List<T> tList = value as List<T>;
|
if (tList != null)
|
{
|
tList.ForEach(t =>
|
{
|
tbox.AppendText(t.ToString());
|
tbox.AppendText("\r\n");
|
});
|
}
|
|
form.ShowDialog();
|
List<string> returnStrs = tbox.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
switch (typeof(T).Name)
|
{
|
case "Int32":
|
return returnStrs.ConvertAll<int>(u => int.Parse(u));
|
case "Int16":
|
return returnStrs.ConvertAll<short>(u => short.Parse(u));
|
case "Int64":
|
return returnStrs.ConvertAll<long>(u => long.Parse(u));
|
case "Single":
|
return returnStrs.ConvertAll<float>(u => float.Parse(u));
|
case "Double":
|
return returnStrs.ConvertAll<double>(u => double.Parse(u));
|
case "String":
|
return returnStrs;
|
case "DateTime":
|
return returnStrs.ConvertAll<DateTime>(u => DateTime.Parse(u));
|
}
|
}
|
|
return base.EditValue(context, provider, value);
|
}
|
}
|
|
/// <summary>
|
/// 用于对象的显示
|
/// </summary>
|
public interface IComplexDisplay
|
{
|
string GetDisplayText();
|
}
|
|
public class ComplexCollectionEditor<T> : CollectionEditor where T : new()
|
{
|
protected override CollectionForm CreateCollectionForm()
|
{
|
var form = base.CreateCollectionForm();
|
|
var prop = form.GetType().GetField("propertyBrowser", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
if (prop != null)
|
{
|
var grid = prop.GetValue(form) as PropertyGrid;
|
if (grid != null)
|
{
|
grid.HelpVisible = true;
|
grid.ToolbarVisible = false;
|
}
|
}
|
|
return form;
|
}
|
|
public ComplexCollectionEditor(Type type) : base(type)
|
{
|
}
|
|
/// <summary>
|
/// 限制一次选一个实例
|
/// </summary>
|
/// <returns></returns>
|
protected override bool CanSelectMultipleInstances()
|
{
|
return false;
|
}
|
|
/// <summary>
|
/// 指定创建的对象类型
|
/// </summary>
|
/// <returns></returns>
|
protected override Type CreateCollectionItemType()
|
{
|
return typeof(T);
|
}
|
|
protected override string GetDisplayText(object value)
|
{
|
if (value is IComplexDisplay)
|
{
|
return (value as IComplexDisplay).GetDisplayText();
|
}
|
|
return base.GetDisplayText(value);
|
}
|
|
protected override void DestroyInstance(object instance)
|
{
|
base.DestroyInstance(instance);//重要!自动删除组件的设计时代码!
|
}
|
}
|
|
public class TimePickerEditor : UITypeEditor
|
{
|
IWindowsFormsEditorService editorService;
|
DateTimePicker picker = new DateTimePicker();
|
|
public TimePickerEditor()
|
{
|
picker.Format = DateTimePickerFormat.Custom;
|
picker.CustomFormat = "HH:mm";
|
picker.ShowUpDown = true;
|
}
|
|
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
|
{
|
return UITypeEditorEditStyle.DropDown;//显示下拉按钮
|
}
|
|
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
|
{
|
if (provider != null)
|
{
|
this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
|
}
|
if (this.editorService != null)
|
{
|
try
|
{
|
//if (value == null)
|
//{
|
// picker.Value = DateTime.Parse("00:00" as string);
|
//}
|
//else
|
//{
|
// picker.Value = DateTime.Parse(value.ToString());
|
//}
|
|
picker.Value = Convert.ToDateTime(value);
|
}
|
catch (Exception)
|
{
|
picker.Value = DateTime.Now;
|
}
|
|
this.editorService.DropDownControl(picker);
|
}
|
return picker.Value;
|
}
|
}
|
#endregion
|
|
}
|