202303241701Change

This commit is contained in:
17860779768 2023-03-24 17:02:04 +08:00
parent 03e8e92c40
commit d12b1b5f2d
19 changed files with 592 additions and 55 deletions

View File

@ -14,6 +14,7 @@ using XKRS.Common.Factory;
using XKRS.Common.Interface;
using XKRS.Common.Model.Helper;
using XKRS.UI.Model.Winform;
using XKRS.UI.Model.Winform.Helper;
namespace XKRS.UI.Main
{
@ -32,13 +33,121 @@ namespace XKRS.UI.Main
dockPanelMain.Theme = theme;
VisualStudioToolStripExtender extender = new VisualStudioToolStripExtender();
extender.SetStyle(menuMain, VisualStudioToolStripExtender.VsVersion.Vs2015, theme);
InitialMenu()
InitialMenu(MenuFormFactory.MenuFrmTypeDict, "");
m_deserializeMenuFrm = new DeserializeDockContent(GetMenuFromPersistString);
m_deserializeDeviceRunFrm = new DeserializeDockContent(GetAllFormPersistString);
notifyIcon.Text = SettingHelper.GetProgramDescription();
string iconPath = SettingHelper.GetProgramIcon();
if (!string.IsNullOrWhiteSpace(iconPath))
{
notifyIcon.Icon = Icon = new Icon(iconPath);
}
}
private void RegisterEvent(MenuFormBase dockFrm)
{
dockFrm.OnUploadProcess = DockFrm_OnUploadProcess;
}
#region Menu
readonly ManualResetEvent _allMenuLoadDoneHandle = new ManualResetEvent(false);
private void InitialMenu(Dictionary<MenuNodeAttribute, Type> menuFrmTypeDict, string parentMenuCode)
{
menuFrmTypeDict.Keys.Where(u => u.ParentMenuCode == parentMenuCode).OrderBy(m => m.MenuOrder).ToList().ForEach(m =>
{
ToolStripMenuItem item = new ToolStripMenuItem(m.MenuName);
item.Tag = m.MenuCode;
if (m.IsActualForm)
{
item.Click += MenuFormItem_Click;
}
});
}
private async void DockFrm_OnUploadProcess(string frmId, IProcess process)
{
await Task.Run(() =>
{
_allMenuLoadDoneHandle.WaitOne();//阻止当前线程直到WaitHandle接收到信号
this.Invoke(new Action(() =>
{
_process = process;
CloseAllDeviceFrm();
try
{
foreach (var dock in dockPanelMain.Contents)
{
MenuFormBase m = dock as MenuFormBase;
if (m != null && m.Id != frmId)
{
m.DownloadProcess(process);
}
}
}
catch (Exception ex)
{
throw ex;
}
}));
});
}
private void MenuFormItem_Click(object sender, EventArgs e)
{
try
{
SuspendLayout();
ToolStripMenuItem item = sender as ToolStripMenuItem;
bool isExisted = false;
foreach (var dock in dockPanelMain.Contents)
{
MenuFormBase menu = dock as MenuFormBase;
if (menu != null && menu.Tag.ToString() == item.Tag.ToString())
{
if (menu.IsHidden)
{
menu.Show();
}
menu.BringToFront();
isExisted = true;
break;
}
}
if (isExisted)
{
ResumeLayout();
return;
}
MenuFormBase dockFrm = MenuFormFactory.GetMenuFrm(item.Tag.ToString());
if (dockFrm == null)
{
return;
}
dockFrm.Text = item.Text;
dockFrm.DockPanel = dockPanelMain;
dockFrm.SetLoginStatus(IsLogin);
dockFrm.DockStateChanged += DockStateChanged;
dockFrm.Show();
RegisterEvent(dockFrm);
ResumeLayout();
}
catch
{
}
}
#endregion
private void MainFrm_Load(object sender, EventArgs e)
@ -164,7 +273,7 @@ namespace XKRS.UI.Main
tsslLoginStatus.Text = isLogin ? "已登录" : "未登录";
foreach (var dock in dockPanelMain.Contents)
{
var menuFrm = dock as MenuFrmBase;
var menuFrm = dock as MenuFormBase;
if (menuFrm != null)
{
menuFrm.SetLoginStatus(isLogin);
@ -179,37 +288,6 @@ namespace XKRS.UI.Main
}
#endregion
#region Layout布局
string _layoutFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{SettingHelper.GetSelectLayout()}.layout");//路径
private DeserializeDockContent m_deserializeMenuFrm;
private DeserializeDockContent m_deserializeDeviceRunFrm;
private void LoadLayoutFromXML(DeserializeDockContent dContent)
{
if (!File.Exists(_layoutFile))
return;
dockPanelMain.SuspendLayout(true);//悬挂式布局
CloseAllDeviceFrm();
while (dockPanelMain.Contents.Count > 0)
{
dockPanelMain.Contents[0].DockHandler.DockPanel = null;
}
using (FileStream stream = new FileStream(_layoutFile, FileMode.Open))
{
dockPanelMain.LoadFromXml(stream, dContent);
stream.Close();
}
dockPanelMain.ResumeLayout(true, true);
}
#endregion
#region CloseForm
private void DeviceDisplayFrm_FormClosed(object sender, FormClosedEventArgs e)
{
@ -250,6 +328,106 @@ namespace XKRS.UI.Main
#endregion
#region Layout布局
string _layoutFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{SettingHelper.GetSelectLayout()}.layout");//路径
private DeserializeDockContent m_deserializeMenuFrm;
private DeserializeDockContent m_deserializeDeviceRunFrm;
private IDockContent GetMenuFromPersistString(string persistString)
{
return null;
}
private IDockContent GetAllFormPersistString(string persistString)
{
if (persistString.StartsWith("MenuFrm"))
{
var desc = persistString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string menuCode = desc[1];
foreach(var dock in dockPanelMain.Contents)
{
MenuFormBase menu = dock as MenuFormBase;
if (menu != null && menu.Tag.ToString() == menuCode)
{
return dock;
}
}
MenuFormBase dockFrm = MenuFormFactory.GetMenuFrm(menuCode);
if (dockFrm == null)
{
return null;
}
dockFrm.Text = desc[2];
dockFrm.DockStateChanged += DockStateChanged;
dockFrm.SetLoginStatus(IsLogin);
RegisterEvent(dockFrm);
return dockFrm;
}
if (persistString.StartsWith("DeviceRunFrm"))
{
string deviceId = persistString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[1];
var device = _process.DeviceCollection.FirstOrDefault(u => u.Id == deviceId);
if (null == device || null == deviceId)
{
return null;
}
if (!showedDeviceUIDict.ContainsKey(deviceId))
{
var runCtrl = UIFactory.GetRunCtrl(device);
DeviceRunFrmBase runFrm = new DeviceRunFrmBase(device, runCtrl);
runFrm.Text = device.Name;
runFrm.MdiParent = this;
runFrm.DockPanel = dockPanelMain;
runFrm.DockState = DockState.Document;
runFrm.FormClosed += DeviceDisplayFrm_FormClosed;
runFrm.DockStateChanged += DockStateChanged;
showedDeviceUIDict[deviceId] = runFrm;
}
return showedDeviceUIDict[deviceId];
}
return null;
}
private void DockStateChanged(object sender, EventArgs e)
{
if (sender is DockContent dc)
{
if (dc.DockState == DockState.Float)
{
dc.Parent.FindForm().FormBorderStyle = FormBorderStyle.Sizable;
}
}
}
private void LoadLayoutFromXML(DeserializeDockContent dContent)
{
if (!File.Exists(_layoutFile))
return;
dockPanelMain.SuspendLayout(true);//悬挂式布局
CloseAllDeviceFrm();
while (dockPanelMain.Contents.Count > 0)
{
dockPanelMain.Contents[0].DockHandler.DockPanel = null;
}
using (FileStream stream = new FileStream(_layoutFile, FileMode.Open))
{
dockPanelMain.LoadFromXml(stream, dContent);
stream.Close();
}
dockPanelMain.ResumeLayout(true, true);
}
#endregion
#region Device Display
readonly Dictionary<string, DeviceRunFrmBase> showedDeviceUIDict = new Dictionary<string, DeviceRunFrmBase>();

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -14,9 +15,34 @@ namespace XKRS.UI.Main
[STAThread]
static void Main()
{
AutoStart(true);
if(AuthorityCheck)
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainFrm());
}
private static void AutoStart(bool isAuto = true)
{
if (isAuto == true)
{
RegistryKey R_local = Registry.CurrentUser;//包含有关当前用户首选项的信息
//创建一个新子项或打开一个现有子项进行写访问
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.SetValue("ProjectName", Application.ExecutablePath);//获取启动应用程序的可执行文件的路径
R_run.Close();
R_local.Close();
}
else
{
RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.DeleteValue("ProjectName", false);
R_run.Close();
R_local.Close();
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace XKRS.Common.Model.Authority
{
public static class AuthorityCheck
{
public static Timer AuthorityCheckTimer = null;
public static UdpClient RemoteCheckClient = null;//提供用户数据报协议Udp网络服务
static byte[] CHECKBUFFER = null;
static IPEndPoint AuthorityServer = null;//将网络终结点表示为IP地址和端口号
public static bool IsAuthorityOK { get; set; } = false;//是否批准静态属性 默认初值为false
static bool _isCheckAuthorityNecessary = true;
static AuthorityCheck()
{
//AuthorityCheckTimer = new Timer(OnAuthorityCheck, null, 0, 1000 * 60);
RemoteCheckClient = new UdpClient();
//获取或设置 System.Boolean 值,该值指定是否 System.Net.Sockets.UdpClient 可能会发送或接收广播的数据包。
RemoteCheckClient.EnableBroadcast = true;
//获取或设置一个值,指定的后的时间量同步 Overload:System.Net.Sockets.Socket.Receive 调用将会超时。
RemoteCheckClient.Client.ReceiveTimeout = 1000;
CHECKBUFFER = Encoding.UTF8.GetBytes("AuthorityCheck");
//string checkStr = ConfigurationManger.AppSettings["AuthorityCheck"];
}
private static void OnAuthorityCheck(object state)
{
AuthorityCheckOperation();
}
public static void AuthorityCheckOperartion()
{
if (!_isCheckAuthorityNecessary)
{
IsAuthorityOK = true;
}
else
{
DogFeature feature = new DogFeature();
}
}
}
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using XKRS.Common.Model.Helper;
namespace XKRS.Common.Factory
{
@ -16,7 +17,18 @@ namespace XKRS.Common.Factory
//构造函数退出后,不能分配 readonly 字段。
private static readonly string[] DLLPATTERNS = { "XKRS." };
private static List<Type> types = new List<Type>();
public static List<Type> TYPES
{
get
{
if (types.Count <= 0)
{
types = GetAttributeType<DeviceAttribute>().Values.ToList();
}
return types;
}
}
public static Dictionary<T, Type> GetAttributeType<T>() where T : Attribute
{
@ -59,5 +71,15 @@ namespace XKRS.Common.Factory
});
return attrTyprDict;
}
public static Type GetTypeByAttributeTypeName(string attrTypeName, EnumHelper.DeviceAttributeType attrType)
{
Type type = TYPES.FirstOrDefault(t =>
{
DeviceAttribute attr = t.GetCustomAttribute<DeviceAttribute>();
return attr != null && attr.TypeCode == attrTypeName && attr.AttrType == attrType;
});
return type;
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using XKRS.Common.Interface;
using XKRS.Common.Model.Helper;
namespace XKRS.Common.Factory
{
public class UIFactory
{
public static IRunCtrl GetRunCtrl(IDevice device)
{
var attr = device.GetType().GetCustomAttribute<DeviceAttribute>();
if (attr == null)
return null;
var type = FactoryHelper.GetTypeByAttributeTypeName(attr.TypeCode, EnumHelper.DeviceAttributeType.RunCtrl);
if (type == null)
return null;
IRunCtrl runCtrl = Activator.CreateInstance(type, new object[] { device }) as IRunCtrl;
return runCtrl;
}
}
}

View File

@ -7,9 +7,18 @@ using static XKRS.Common.Model.Helper.EnumHelper;
namespace XKRS.Common.Model.Helper
{
public class AttributeHelper:Attribute
public class DeviceAttribute:Attribute
{
/// <summary>
/// 设备类型
/// </summary>
public string TypeCode { get; set; }
/// <summary>
///特性修饰类别
/// </summary>
public DeviceAttributeType AttrType { get; set; }
}
public class ProcessAttribute : Attribute

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -17,6 +18,9 @@ namespace XKRS.Common.Model.Helper
public enum DeviceAttributeType
{
[Description("运行控件")]
RunCtrl=512,
}
}
}

View File

@ -67,9 +67,28 @@ namespace XKRS.Common.Model.Helper
}
public static string GetProgramDescription()
{
if (string.IsNullOrWhiteSpace(SettingInfo.Description))
{
SettingInfo.Description = "";
}
return SettingInfo.Description;
}
public static string GetProgramIcon()
{
string iconPath = SettingInfo.IconPath;
if (string.IsNullOrWhiteSpace(iconPath) || !File.Exists(iconPath))
{
iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
}
if (!File.Exists(iconPath))
{
iconPath = "";
}
return iconPath;
}
@ -85,8 +104,11 @@ namespace XKRS.Common.Model.Helper
public string DefaultProcess { get; set; }
public string DefaultProduction { get; set; }
public string IconPath { get; set; }
public string Description { get; set; }
public string CurrLayout { get; set; } = "";
}
}

View File

@ -18,10 +18,12 @@ namespace XKRS.Common.Interface
public interface ISimpleDevice
{
string Id { get; set; }
string Name { get; set; }
}
public class SimpleDevice : ISimpleDevice
{
public string Id { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XKRS.Common.Interface
{
public interface IMenuNode
{
}
}

View File

@ -0,0 +1,7 @@
namespace XKRS.Common.Interface
{
public interface IRunCtrl
{
IDevice Device { get; set; }
}
}

View File

@ -39,16 +39,20 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Authority\AuthorityCheck.cs" />
<Compile Include="Exception\ExceptionHelper.cs" />
<Compile Include="Factory\FactoryHelper.cs" />
<Compile Include="Factory\ProcessFactory.cs" />
<Compile Include="Factory\UIFactory.cs" />
<Compile Include="Helper\AttributeHelper.cs" />
<Compile Include="Helper\EnumHelper.cs" />
<Compile Include="Interface\IDevice.cs" />
<Compile Include="Interface\IExceptionHandler.cs" />
<Compile Include="Helper\SettingHelper.cs" />
<Compile Include="Interface\ILogger.cs" />
<Compile Include="Interface\IMenuNode.cs" />
<Compile Include="Interface\IProcess.cs" />
<Compile Include="Interface\IRunCtrl.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XKRS.UI.Model.Winform
{
/// <summary>
/// 菜单节点属性
/// </summary>
public class MenuNodeAttribute:Attribute
{
public string MenuCode { get; set; }
public string MenuName { get; set; }
public int MenuOrder { get; set; }
public string ParentMenuCode { get; set; }
/// <summary>
/// 表示该窗体是实际显示窗体还是只是父节点
/// </summary>
public bool IsActualForm { get; set; }
}
}

View File

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using XKRS.Common.Interface;
namespace XKRS.UI.Model.Winform.Helper
{
public static class MenuFormFactory
{
public static Dictionary<MenuNodeAttribute, Type> menuFrmTypeDict = null;
public static Dictionary<MenuNodeAttribute,Type> MenuFrmTypeDict
{
get
{
if (menuFrmTypeDict == null)
{
var assm = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)
.GetFiles("*.dll")
.Where(fi =>
{
return fi.Name.StartsWith("XKRS");
})
.Select(u =>
{
try
{
return Assembly.LoadFrom(u.FullName);
}
catch { }
return null;
})
.Where(a => a != null)
.ToList();
assm.AddRange(AppDomain.CurrentDomain.GetAssemblies());
assm = assm.Distinct().ToList();
try
{
menuFrmTypeDict = assm.SelectMany(a => a.GetTypes())
.Where(t =>
{
if (t.GetInterfaces().Contains(typeof(IMenuNode)))
{
var attr = t.GetCustomAttribute<MenuNodeAttribute>();
return attr != null;
}
else
{
return false;
}
}).ToDictionary(t => t.GetCustomAttribute<MenuNodeAttribute>(), t => t);
}
catch(Exception ex)
{
throw ex;
}
}
return menuFrmTypeDict;
}
}
/// <summary>
/// 从Menu配置项转为窗口
/// </summary>
/// <param name="frmCode"></param>
/// <returns></returns>
public static MenuFormBase GetMenuFrm(string frmCode)
{
Type frmType = GetMenuFrmType(frmCode);
if (frmType == null)
{
return null;
}
var menuItem = Activator.CreateInstance(frmType);
if (menuItem == null)
{
return null;
}
var menu = menuItem as MenuFormBase;
if (menu == null)
{
return null;
}
menu.Tag = frmCode;
return menu;
}
public static Type GetMenuFrmType(string frmCode)
{
Type frmType = null;
foreach (KeyValuePair<MenuNodeAttribute,Type> pair in MenuFrmTypeDict)
{
if (pair.Key.MenuCode == frmCode)
{
frmType = pair.Value;
break;
}
}
return frmType;
}
}
}

View File

@ -15,10 +15,21 @@ namespace XKRS.UI.Model.Winform
public partial class DeviceRunFrmBase : DockContent
{
public IDevice Device { get; set; }
public IRunCtrl RunCtrl { get; set; }
public DeviceRunFrmBase()
public DeviceRunFrmBase() : base()
{
InitializeComponent();
}
}
public DeviceRunFrmBase(IDevice device,IRunCtrl runCtrl) : base()
{
Device = device;
RunCtrl = runCtrl;
UserControl uc = RunCtrl as UserControl;
uc.Dock = DockStyle.Fill;
this.Controls.Add(uc);
}
}
}

View File

@ -1,7 +1,7 @@

namespace XKRS.UI.Model.Winform
{
partial class MenuFrmBase
partial class MenuFormBase
{
/// <summary>
/// Required designer variable.
@ -31,13 +31,13 @@ namespace XKRS.UI.Model.Winform
{
this.SuspendLayout();
//
// MenuFrmBase
// MenuFormBase
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.ClientSize = new System.Drawing.Size(474, 344);
this.DoubleBuffered = true;
this.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
this.Name = "MenuFrmBase";
this.Name = "MenuFormBase";
this.Text = "MenuFrmBase";
this.ResumeLayout(false);

View File

@ -12,24 +12,52 @@ using XKRS.Common.Interface;
namespace XKRS.UI.Model.Winform
{
public partial class MenuFrmBase : DockContent
public partial class MenuFormBase : DockContent
{
public Action<string,IProcess> OnUploadProcess { get; set; }
public event Action<bool> OnIsLoginChanged;
public string Id { get; set; } = Guid.NewGuid().ToString();
public MenuFrmBase()
private IProcess process = null;
public IProcess Process
{
get => process;
set
{
if (process != value)
{
process = value;
OnProcessUpdated();
}
}
}
public MenuFormBase()
{
InitializeComponent();
}
#region IProcessObserver
public virtual void OnProcessUpdated() { }
public virtual void DownloadProcess(IProcess process)
{
Process = process;
}
#endregion
#region Login
protected virtual bool IsLogin { get; set; }
public virtual void SetLoginStatus(bool isLogin)
{
IsLogin = isLogin;
OnIsLoginChanged?.Invoke(IsLogin);
}
#endregion
}

View File

@ -60,6 +60,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Helper\AttributeHelper.cs" />
<Compile Include="Helper\MenuFormFactory.cs" />
<Compile Include="UI\DockContent\DeviceRunFrmBase.cs">
<SubType>Form</SubType>
</Compile>