From d12b1b5f2da82f01d2bbb0924938c0e80b7bb2de Mon Sep 17 00:00:00 2001 From: 17860779768 <3219079380@qq.com> Date: Fri, 24 Mar 2023 17:02:04 +0800 Subject: [PATCH] 202303241701Change --- CopyCode/MainFrm.Designer.cs | 8 +- CopyCode/MainFrm.cs | 254 +++++++++++++++--- CopyCode/Program.cs | 28 +- XKRS.Common.Model/Authority/AuthorityCheck.cs | 54 ++++ XKRS.Common.Model/Factory/FactoryHelper.cs | 24 +- XKRS.Common.Model/Factory/UIFactory.cs | 26 ++ XKRS.Common.Model/Helper/AttributeHelper.cs | 11 +- XKRS.Common.Model/Helper/EnumHelper.cs | 4 + XKRS.Common.Model/Helper/SettingHelper.cs | 26 +- XKRS.Common.Model/Interface/IDevice.cs | 2 + XKRS.Common.Model/Interface/IMenuNode.cs | 13 + XKRS.Common.Model/Interface/IRunCtrl.cs | 7 + XKRS.Common.Model/XKRS.Common.Model.csproj | 4 + .../Helper/AttributeHelper.cs | 23 ++ .../Helper/MenuFormFactory.cs | 106 ++++++++ .../UI/DockContent/DeviceRunFrmBase.cs | 15 +- .../UI/DockContent/MenuFrmBase.Designer.cs | 6 +- .../UI/DockContent/MenuFrmBase.cs | 34 ++- .../XKRS.UI.Model.Winform.csproj | 2 + 19 files changed, 592 insertions(+), 55 deletions(-) create mode 100644 XKRS.Common.Model/Authority/AuthorityCheck.cs create mode 100644 XKRS.Common.Model/Factory/UIFactory.cs create mode 100644 XKRS.Common.Model/Interface/IMenuNode.cs create mode 100644 XKRS.Common.Model/Interface/IRunCtrl.cs create mode 100644 XKRS.UI.Model.Winform/Helper/AttributeHelper.cs create mode 100644 XKRS.UI.Model.Winform/Helper/MenuFormFactory.cs diff --git a/CopyCode/MainFrm.Designer.cs b/CopyCode/MainFrm.Designer.cs index 25caff5..0e5f439 100644 --- a/CopyCode/MainFrm.Designer.cs +++ b/CopyCode/MainFrm.Designer.cs @@ -51,7 +51,7 @@ namespace XKRS.UI.Main // // menuMain // - this.menuMain.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + this.menuMain.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.menuMain.AutoSize = false; this.menuMain.Dock = System.Windows.Forms.DockStyle.None; @@ -64,7 +64,7 @@ namespace XKRS.UI.Main // // ststripDevices // - this.ststripDevices.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + this.ststripDevices.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.ststripDevices.AutoSize = false; this.ststripDevices.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(77)))), ((int)(((byte)(96)))), ((int)(((byte)(130))))); @@ -173,8 +173,8 @@ namespace XKRS.UI.Main // // dockPanelMain // - this.dockPanelMain.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) + this.dockPanelMain.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.dockPanelMain.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World); this.dockPanelMain.Location = new System.Drawing.Point(0, 24); diff --git a/CopyCode/MainFrm.cs b/CopyCode/MainFrm.cs index e611d68..3805ed0 100644 --- a/CopyCode/MainFrm.cs +++ b/CopyCode/MainFrm.cs @@ -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 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) @@ -136,7 +245,7 @@ namespace XKRS.UI.Main /// 是否为初始化流程 private void LoadProcess(bool isInitialProcess = true) { - if(isInitialProcess)//初次创建流程 + if (isInitialProcess)//初次创建流程 { var processCodes = LoadProcessCode(); var productionCodes = LoadProductionCode(); @@ -162,9 +271,9 @@ namespace XKRS.UI.Main { isLogin = value; tsslLoginStatus.Text = isLogin ? "已登录" : "未登录"; - foreach(var dock in dockPanelMain.Contents) + foreach (var dock in dockPanelMain.Contents) { - var menuFrm = dock as MenuFrmBase; + var menuFrm = dock as MenuFormBase; if (menuFrm != null) { menuFrm.SetLoginStatus(isLogin); @@ -179,42 +288,11 @@ 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) + private void DeviceDisplayFrm_FormClosed(object sender, FormClosedEventArgs e) { string id = (sender as DeviceRunFrmBase).Device.Id; - if(showedDeviceUIDict.ContainsKey(id)) + if (showedDeviceUIDict.ContainsKey(id)) { showedDeviceUIDict.Remove(id); } @@ -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 showedDeviceUIDict = new Dictionary(); diff --git a/CopyCode/Program.cs b/CopyCode/Program.cs index 7f7e8aa..49b7335 100644 --- a/CopyCode/Program.cs +++ b/CopyCode/Program.cs @@ -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(); + } + } } } diff --git a/XKRS.Common.Model/Authority/AuthorityCheck.cs b/XKRS.Common.Model/Authority/AuthorityCheck.cs new file mode 100644 index 0000000..fdbd240 --- /dev/null +++ b/XKRS.Common.Model/Authority/AuthorityCheck.cs @@ -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(); + } + } + + } +} diff --git a/XKRS.Common.Model/Factory/FactoryHelper.cs b/XKRS.Common.Model/Factory/FactoryHelper.cs index a8cb34b..af94a9b 100644 --- a/XKRS.Common.Model/Factory/FactoryHelper.cs +++ b/XKRS.Common.Model/Factory/FactoryHelper.cs @@ -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 types = new List(); + public static List TYPES + { + get + { + if (types.Count <= 0) + { + types = GetAttributeType().Values.ToList(); + } + return types; + } + } public static Dictionary GetAttributeType() 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(); + return attr != null && attr.TypeCode == attrTypeName && attr.AttrType == attrType; + }); + return type; + } } } diff --git a/XKRS.Common.Model/Factory/UIFactory.cs b/XKRS.Common.Model/Factory/UIFactory.cs new file mode 100644 index 0000000..ca9aa05 --- /dev/null +++ b/XKRS.Common.Model/Factory/UIFactory.cs @@ -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(); + 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; + } + } +} diff --git a/XKRS.Common.Model/Helper/AttributeHelper.cs b/XKRS.Common.Model/Helper/AttributeHelper.cs index fde5141..77f7e32 100644 --- a/XKRS.Common.Model/Helper/AttributeHelper.cs +++ b/XKRS.Common.Model/Helper/AttributeHelper.cs @@ -7,9 +7,18 @@ using static XKRS.Common.Model.Helper.EnumHelper; namespace XKRS.Common.Model.Helper { - public class AttributeHelper:Attribute + public class DeviceAttribute:Attribute { + /// + /// 设备类型 + /// + public string TypeCode { get; set; } + + /// + ///特性修饰类别 + /// + public DeviceAttributeType AttrType { get; set; } } public class ProcessAttribute : Attribute diff --git a/XKRS.Common.Model/Helper/EnumHelper.cs b/XKRS.Common.Model/Helper/EnumHelper.cs index 9968062..8910a59 100644 --- a/XKRS.Common.Model/Helper/EnumHelper.cs +++ b/XKRS.Common.Model/Helper/EnumHelper.cs @@ -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, } } } diff --git a/XKRS.Common.Model/Helper/SettingHelper.cs b/XKRS.Common.Model/Helper/SettingHelper.cs index 5d9e1d0..b974f61 100644 --- a/XKRS.Common.Model/Helper/SettingHelper.cs +++ b/XKRS.Common.Model/Helper/SettingHelper.cs @@ -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; } = ""; + } } diff --git a/XKRS.Common.Model/Interface/IDevice.cs b/XKRS.Common.Model/Interface/IDevice.cs index 52379ec..9f1a42c 100644 --- a/XKRS.Common.Model/Interface/IDevice.cs +++ b/XKRS.Common.Model/Interface/IDevice.cs @@ -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; } } } diff --git a/XKRS.Common.Model/Interface/IMenuNode.cs b/XKRS.Common.Model/Interface/IMenuNode.cs new file mode 100644 index 0000000..6b86bc4 --- /dev/null +++ b/XKRS.Common.Model/Interface/IMenuNode.cs @@ -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 + { + + } +} diff --git a/XKRS.Common.Model/Interface/IRunCtrl.cs b/XKRS.Common.Model/Interface/IRunCtrl.cs new file mode 100644 index 0000000..c918f48 --- /dev/null +++ b/XKRS.Common.Model/Interface/IRunCtrl.cs @@ -0,0 +1,7 @@ +namespace XKRS.Common.Interface +{ + public interface IRunCtrl + { + IDevice Device { get; set; } + } +} diff --git a/XKRS.Common.Model/XKRS.Common.Model.csproj b/XKRS.Common.Model/XKRS.Common.Model.csproj index cbf8627..ce7531c 100644 --- a/XKRS.Common.Model/XKRS.Common.Model.csproj +++ b/XKRS.Common.Model/XKRS.Common.Model.csproj @@ -39,16 +39,20 @@ + + + + diff --git a/XKRS.UI.Model.Winform/Helper/AttributeHelper.cs b/XKRS.UI.Model.Winform/Helper/AttributeHelper.cs new file mode 100644 index 0000000..5b7851c --- /dev/null +++ b/XKRS.UI.Model.Winform/Helper/AttributeHelper.cs @@ -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 +{ + /// + /// 菜单节点属性 + /// + public class MenuNodeAttribute:Attribute + { + public string MenuCode { get; set; } + public string MenuName { get; set; } + public int MenuOrder { get; set; } + public string ParentMenuCode { get; set; } + /// + /// 表示该窗体是实际显示窗体还是只是父节点 + /// + public bool IsActualForm { get; set; } + } +} diff --git a/XKRS.UI.Model.Winform/Helper/MenuFormFactory.cs b/XKRS.UI.Model.Winform/Helper/MenuFormFactory.cs new file mode 100644 index 0000000..5f804f4 --- /dev/null +++ b/XKRS.UI.Model.Winform/Helper/MenuFormFactory.cs @@ -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 menuFrmTypeDict = null; + public static Dictionary 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(); + return attr != null; + } + else + { + return false; + } + }).ToDictionary(t => t.GetCustomAttribute(), t => t); + } + catch(Exception ex) + { + throw ex; + } + } + return menuFrmTypeDict; + } + } + /// + /// 从Menu配置项转为窗口 + /// + /// + /// + 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 pair in MenuFrmTypeDict) + { + if (pair.Key.MenuCode == frmCode) + { + frmType = pair.Value; + break; + } + } + return frmType; + } + } +} diff --git a/XKRS.UI.Model.Winform/UI/DockContent/DeviceRunFrmBase.cs b/XKRS.UI.Model.Winform/UI/DockContent/DeviceRunFrmBase.cs index d7d2b2c..f2888bb 100644 --- a/XKRS.UI.Model.Winform/UI/DockContent/DeviceRunFrmBase.cs +++ b/XKRS.UI.Model.Winform/UI/DockContent/DeviceRunFrmBase.cs @@ -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); + } + + + } } diff --git a/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.Designer.cs b/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.Designer.cs index fe4eea8..a8f74fd 100644 --- a/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.Designer.cs +++ b/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.Designer.cs @@ -1,7 +1,7 @@  namespace XKRS.UI.Model.Winform { - partial class MenuFrmBase + partial class MenuFormBase { /// /// 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); diff --git a/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.cs b/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.cs index 24a1054..bb22738 100644 --- a/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.cs +++ b/XKRS.UI.Model.Winform/UI/DockContent/MenuFrmBase.cs @@ -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 OnUploadProcess { get; set; } public event Action 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 } diff --git a/XKRS.UI.Model.Winform/XKRS.UI.Model.Winform.csproj b/XKRS.UI.Model.Winform/XKRS.UI.Model.Winform.csproj index c38258b..8e3e1e1 100644 --- a/XKRS.UI.Model.Winform/XKRS.UI.Model.Winform.csproj +++ b/XKRS.UI.Model.Winform/XKRS.UI.Model.Winform.csproj @@ -60,6 +60,8 @@ + + Form