using Bro.Common.Factory; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.UI.Model.Winform; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; namespace Bro.UI.Config { public partial class MainFrm : Form { IProcess _process = null; public MainFrm() { InitializeComponent(); //var theme = new VS2015DarkTheme(); //stsStripLayout.BackColor = ststripDevices.BackColor = Color.FromArgb(64, 64, 64); var theme = new VS2015BlueTheme(); stsStripLayout.BackColor = ststripDevices.BackColor = Color.FromArgb(77, 96, 130); stsStripWarning.BackColor = Color.FromArgb(214, 219, 233); dockPanelMain.Theme = theme; VisualStudioToolStripExtender extender = new VisualStudioToolStripExtender(); extender.SetStyle(menuMain, VisualStudioToolStripExtender.VsVersion.Vs2015, theme); InitialMenu(MenuFormFactory.MenuFrmTypeDict, ""); //MenuFormFactory.MenuFrmTypeDict.ToList().ForEach(t => //{ // var dockOption = t.Value.GetCustomAttribute(); // if (dockOption != null) // { // MenuFrmBase dockFrm = MenuFormFactory.GetMenuFrm(t.Key.MenuCode); // dockFrm.Text = t.Key.MenuName; // dockFrm.MdiParent = this; // dockFrm.DockPanel = dockPanelMain; // dockFrm.DockState = dockOption.DockState; // switch (dockOption.DockState) // { // case DockState.DockBottom: // case DockState.DockTop: // if (dockOption.DefaultHeight > 0) // { // dockFrm.AutoHidePortion = dockOption.DefaultHeight; // dockPanelMain.DockTopPortion = dockPanelMain.DockBottomPortion = dockOption.DefaultHeight; // } // break; // case DockState.DockLeft: // case DockState.DockRight: // if (dockOption.DefaultWidth > 0) // { // dockFrm.AutoHidePortion = dockOption.DefaultWidth; // dockPanelMain.DockLeftPortion = dockPanelMain.DockRightPortion = dockOption.DefaultWidth; // } // break; // default: // break; // } // RegisterEvent(dockFrm); // } //}); m_deserializeMenuFrm = new DeserializeDockContent(GetMenuFromPersistString); LoadLayoutFromXML(m_deserializeMenuFrm); m_deserializeDeviceRunFrm = new DeserializeDockContent(GetAllFormPersistString); _allMenuLoadDoneHandle.Set(); Text = SettingHelper.GetProgramDescription(); string iconPath = SettingHelper.GetProgramIcon(); if (!string.IsNullOrWhiteSpace(iconPath)) { Icon = new Icon(iconPath); } } private void RegisterEvent(MenuFrmBase dockFrm) { dockFrm.OnUploadProcess = DockFrm_OnUploadProcess; dockFrm.OnLogMsgOutput = DockFrm_OnLogMsgOutput; //if (dockFrm is FrmDevices) //{ // (dockFrm as FrmDevices).OnCheckedDeviceListChanged += MainFrm_OnCheckedDeviceListChanged; // return; //} } #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; } if (parentMenuCode == "") { menuMain.Items.Add(item); } else { ToolStripMenuItem parentNode = GetMatchNode(menuMain.Items, parentMenuCode); if (parentNode != null) { parentNode.DropDownItems.Add(item); } } InitialMenu(menuFrmTypeDict, m.MenuCode); }); } private async void DockFrm_OnLogMsgOutput(LogMsg msg) { await Task.Run(() => { foreach (var dock in dockPanelMain.Contents) { (dock as MenuFrmBase)?.LogDisplay(msg); } }); } private async void DockFrm_OnUploadProcess(string frmId, IProcess process) { await Task.Run(() => { _allMenuLoadDoneHandle.WaitOne(); _process = process; _process.OnAlarmUpdate -= Process_OnAlarmUpdate; _process.OnAlarmUpdate += Process_OnAlarmUpdate; CloseAllDeviceFrm(); LoadDevices(); foreach (var dock in dockPanelMain.Contents) { MenuFrmBase m = dock as MenuFrmBase; if (m != null && m.Id != frmId) { m.DownloadProcess(process); } } }); } private void Process_OnAlarmUpdate(string alarmMsg) { tsslWarning.Text = alarmMsg; } private ToolStripMenuItem GetMatchNode(ToolStripItemCollection items, string parentMenuCode) { foreach (ToolStripMenuItem node in items) { if (node.Tag != null && node.Tag.ToString() == parentMenuCode) { return node; } else { if (node.DropDownItems.Count > 0) { var nextNode = GetMatchNode(node.DropDownItems, parentMenuCode); if (nextNode != null) { return nextNode; } } } } return null; } private void MenuFormItem_Click(object sender, EventArgs e) { SuspendLayout(); ToolStripMenuItem item = sender as ToolStripMenuItem; bool isExisted = false; foreach (var dock in dockPanelMain.Contents) { MenuFrmBase menu = dock as MenuFrmBase; if (menu != null && menu.Tag.ToString() == item.Tag.ToString()) { if (menu.IsHidden) { menu.Show(); } menu.BringToFront(); isExisted = true; break; } } if (isExisted) { ResumeLayout(); return; } MenuFrmBase dockFrm = MenuFormFactory.GetMenuFrm(item.Tag.ToString()); dockFrm.Text = item.Text; dockFrm.MdiParent = this; dockFrm.DockPanel = dockPanelMain; dockFrm.SetLoginStatus(IsLogin); dockFrm.Show(); RegisterEvent(dockFrm); ResumeLayout(); if (_process != null) { dockFrm.DownloadProcess(_process); } } #endregion private void MainFrm_Load(object sender, EventArgs e) { AdvancedPwdFrm.OnLoginOK = OnLoginOK; SpinWait wait = new SpinWait(); while (_process == null) { wait.SpinOnce(); } LoadLayoutFromXML(m_deserializeDeviceRunFrm); } private void CloseAllDocuments() { //if (dockPanelMain.DocumentStyle == DocumentStyle.SystemMdi) //{ // foreach (Form form in MdiChildren) // form.Close(); //} //else //{ // foreach (IDockContent document in dockPanelMain.DocumentsToArray()) // { // // IMPORANT: dispose all panes. // document.DockHandler.DockPanel = null; // document.DockHandler.Close(); // } //} } private void MainFrm_FormClosing(object sender, FormClosingEventArgs e) { CloseAllDocuments(); } #region Login bool isLogin = false; bool IsLogin { get => isLogin; set { isLogin = value; tsslLoginStatus.Text = isLogin ? "已登录" : "未登录"; foreach (var dock in dockPanelMain.Contents) { var menuFrm = dock as MenuFrmBase; if (menuFrm != null) { menuFrm.SetLoginStatus(isLogin); } } } } private void tsslLoginStatus_Click(object sender, EventArgs e) { AdvancedPwdFrm pwdFrm = new AdvancedPwdFrm(); pwdFrm.ShowDialog(); } private void OnLoginOK(bool isLogin) { IsLogin = true; } #endregion #region Device Display readonly Dictionary showedDeviceUIDict = new Dictionary(); //private void MainFrm_OnCheckedDeviceListChanged(List deviceIds) //{ // if (InvokeRequired) // { // Invoke(new Action(() => MainFrm_OnCheckedDeviceListChanged(deviceIds))); // } // else // { // List closedUI = new List(); // foreach (KeyValuePair pair in showedDeviceUIDict) // { // if (!deviceIds.Contains(pair.Key)) // { // pair.Value.FormClosed -= DeviceDisplayFrm_FormClosed; // pair.Value.DockHandler.DockPanel = null; // pair.Value.DockHandler.Close(); // closedUI.Add(pair.Key); // } // } // closedUI.ForEach(k => showedDeviceUIDict.Remove(k)); // deviceIds.ForEach(d => // { // if (!showedDeviceUIDict.ContainsKey(d)) // { // IRunCtrl runCtrl = null; // if (_process == null) // return; // IDevice device = _process.DeviceCollection.FirstOrDefault(u => u.Id == d); // if (device == null) // return; // var attr = device.GetType().GetCustomAttribute(); // if (attr == null) // return; // runCtrl = UIFactory.GetRunCtrl(device); // if (runCtrl == null) // return; // DockContent dockFrm = new DockContent(); // UserControl uCtrl = runCtrl as UserControl; // uCtrl.Dock = DockStyle.Fill; // dockFrm.Controls.Add(uCtrl); // dockFrm.Text = device.Name; // dockFrm.Tag = device.Id; // dockFrm.MdiParent = this; // dockFrm.DockHandler.DockPanel = dockPanelMain; // dockFrm.DockState = DockState.Document; // dockFrm.FormClosed += DeviceDisplayFrm_FormClosed; // showedDeviceUIDict[d] = dockFrm; // } // }); // } //} private void DeviceDisplayFrm_FormClosed(object sender, FormClosedEventArgs e) { string id = (sender as DeviceRunFrmBase).Device.Id; if (showedDeviceUIDict.ContainsKey(id)) { showedDeviceUIDict.Remove(id); } } private void CloseAllDeviceFrm() { this.Invoke(new Action(() => { this.SuspendLayout(); dockPanelMain.Contents.Select(u => { if (u is DeviceRunFrmBase runFrmBase) { return runFrmBase; } else { return null; } }).ToList().ForEach(u => { if (u == null) return; u.DockPanel = null; u.Close(); }); this.ResumeLayout(); })); } private void LoadDevices() { while (ststripDevices.Items.Count > 1) ststripDevices.Items.RemoveAt(1); ststripDevices.Invalidate(); _process.DeviceCollection.ForEach(d => { ToolStripStatusLabel tssl = new ToolStripStatusLabel(d.Name); tssl.ForeColor = System.Drawing.SystemColors.Control; tssl.DoubleClickEnabled = true; tssl.DoubleClick += Tssl_DoubleClick; tssl.MouseHover += Tssl_MouseHover; tssl.MouseLeave += Tssl_MouseLeave; d.OnDeviceStateChanged += Device_OnDeviceStateChanged; tssl.Tag = d; ststripDevices.Items.Add(tssl); }); } private void Tssl_MouseLeave(object sender, EventArgs e) { this.Cursor = Cursors.Default; } private void Tssl_MouseHover(object sender, EventArgs e) { if (sender is ToolStripStatusLabel lbl) { this.Cursor = Cursors.Hand; } } private void Tssl_DoubleClick(object sender, EventArgs e) { if ((sender as ToolStripStatusLabel).Tag is IDevice device) { string typeCode = device.GetType().GetCustomAttribute()?.TypeCode; if (!string.IsNullOrWhiteSpace(typeCode)) { if (UIFactory.IsDeviceCtrlExisted(typeCode, EnumHelper.DeviceAttributeType.RunCtrl)) { if (!showedDeviceUIDict.ContainsKey(device.Id)) { 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; showedDeviceUIDict[device.Id] = runFrm; } else { showedDeviceUIDict[device.Id].BringToFront(); } } } } } private void Device_OnDeviceStateChanged(IDevice device, EnumHelper.DeviceState currentState) { for (int i = 1; i < ststripDevices.Items.Count; i++) { if ((ststripDevices.Items[i].Tag as IDevice)?.Id == device.Id) { ststripDevices.Items[i].BackColor = currentState.GetEnumSelectedColor(); ststripDevices.Items[i].ForeColor = currentState.GetEnumSelectedFontColor(); break; } } } #endregion #region Layout List _dockContentList = new List(); string _layoutFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "layout.xml"); private DeserializeDockContent m_deserializeMenuFrm; private DeserializeDockContent m_deserializeDeviceRunFrm; private IDockContent GetMenuFromPersistString(string persistString) { if (persistString.StartsWith("MenuFrm")) { var desc = persistString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string menuCode = desc[1]; foreach (var dock in dockPanelMain.Contents) { MenuFrmBase menu = dock as MenuFrmBase; if (menu != null && menu.Tag.ToString() == menuCode) { return dock; } } MenuFrmBase dockFrm = MenuFormFactory.GetMenuFrm(menuCode); if (dockFrm == null) return null; dockFrm.Text = desc[2]; if (_process != null) { dockFrm.DownloadProcess(_process); } dockFrm.SetLoginStatus(IsLogin); RegisterEvent(dockFrm); return dockFrm; } 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) { MenuFrmBase menu = dock as MenuFrmBase; if (menu != null && menu.Tag.ToString() == menuCode) { return dock; } } MenuFrmBase dockFrm = MenuFormFactory.GetMenuFrm(menuCode); if (dockFrm == null) return null; dockFrm.Text = desc[2]; if (_process != null) { dockFrm.DownloadProcess(_process); } 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 (!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; showedDeviceUIDict[deviceId] = runFrm; } return showedDeviceUIDict[deviceId]; } return null; } private void tssBtnLayout_Click(object sender, EventArgs e) { tssBtnLayout.ShowDropDown(); } private void tsmiSaveLayout_Click(object sender, EventArgs e) { dockPanelMain.SaveAsXml(_layoutFile); MessageBox.Show("当前布局设置保存完成"); } private void tsmiResetLayout_Click(object sender, EventArgs e) { LoadLayoutFromXML(m_deserializeMenuFrm); SpinWait wait = new SpinWait(); while (_process == null) { wait.SpinOnce(); } LoadLayoutFromXML(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 } }