1081 lines
34 KiB
C#
1081 lines
34 KiB
C#
using BRS.Common.Factory;
|
|
using BRS.Common.Model.Helper;
|
|
using BRS.Common.Interface;
|
|
using BRS.Common.Model;
|
|
using BRS.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 BRS.UI.Main
|
|
{
|
|
public partial class MainFrm : Form
|
|
{
|
|
IProcess _process = null;
|
|
|
|
|
|
public MainFrm()
|
|
{
|
|
InitializeComponent();
|
|
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime;
|
|
////数据库迁移检查
|
|
//DatabaseInitialize.Initialize();
|
|
|
|
var theme = new VS2015LightTheme();
|
|
stsStripLayout.BackColor = ststripDevices.BackColor = Color.FromArgb(64, 64, 64);
|
|
|
|
dockPanelMain.Theme = theme;
|
|
|
|
VisualStudioToolStripExtender extender = new VisualStudioToolStripExtender();
|
|
extender.SetStyle(menuMain, VisualStudioToolStripExtender.VsVersion.Vs2015, theme);
|
|
|
|
InitialMenu(MenuFormFactory.MenuFrmTypeDict, "");
|
|
|
|
m_deserializeMenuFrm = new DeserializeDockContent(GetMenuFromPersistString);
|
|
m_deserializeDeviceRunFrm = new DeserializeDockContent(GetAllFormPersistString);
|
|
|
|
|
|
notifyIcon.Text = 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;
|
|
|
|
dockFrm.OnLogMsgOutput -= DockFrm_OnLogMsgOutput;
|
|
dockFrm.OnLogMsgOutput += DockFrm_OnLogMsgOutput;
|
|
|
|
dockFrm.OnShowWaitingBar += DockFrm_OnShowWaitingBar;
|
|
|
|
//if (dockFrm is FrmDevices)
|
|
//{
|
|
// (dockFrm as FrmDevices).OnCheckedDeviceListChanged += MainFrm_OnCheckedDeviceListChanged;
|
|
// return;
|
|
//}
|
|
}
|
|
|
|
private void DockFrm_OnShowWaitingBar(bool isShowWaitingBar)
|
|
{
|
|
//waitBar.Visible = isShowWaitingBar;
|
|
}
|
|
|
|
#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;
|
|
}
|
|
else
|
|
{
|
|
item.Click += MenuItem_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 MenuFormBase)?.LogDisplay(msg);
|
|
// }
|
|
// });
|
|
//}
|
|
private void LogDisplay(LogMsg msg)
|
|
{
|
|
foreach (var dock in dockPanelMain.Contents)
|
|
{
|
|
(dock as MenuFormBase)?.LogDisplay(msg);
|
|
}
|
|
}
|
|
|
|
internal void DockFrm_OnLogMsgOutput(LogMsg msg)
|
|
{
|
|
//LogDisplay(msg);
|
|
_process?.LogAsync(msg);
|
|
}
|
|
|
|
private async void DockFrm_OnUploadProcess(string frmId, IProcess process)
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
_allMenuLoadDoneHandle.WaitOne();
|
|
this.Invoke(new Action(() =>
|
|
{
|
|
_process = process;
|
|
_process.OnLog -= LogDisplay;
|
|
_process.OnLog += LogDisplay;
|
|
_process.OnAlarmRaised -= Process_OnAlarmUpdate;
|
|
_process.OnAlarmRaised += Process_OnAlarmUpdate;
|
|
|
|
CloseAllDeviceFrm();
|
|
LoadDevices();
|
|
|
|
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;
|
|
}
|
|
}));
|
|
});
|
|
}
|
|
|
|
static object _alarmMsgLock = new object();
|
|
|
|
List<IWarningSet> _wsList = new List<IWarningSet>();
|
|
private async void Process_OnAlarmUpdate(IWarningSet warningSet)
|
|
{
|
|
this.Invoke(new Action(() =>
|
|
{
|
|
lock (_alarmMsgLock)
|
|
{
|
|
if (warningSet is ResetWaringSet)
|
|
{
|
|
_wsList.Clear();
|
|
}
|
|
else
|
|
{
|
|
var existed = _wsList.FirstOrDefault(u => u.SourceDevice == warningSet.SourceDevice && u.WarningDescription == warningSet.WarningDescription);
|
|
|
|
if (warningSet.CurrentStatus)
|
|
{
|
|
if (existed == null)
|
|
{
|
|
_wsList.Add(warningSet);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (existed != null)
|
|
{
|
|
_wsList.Remove(existed);
|
|
}
|
|
}
|
|
}
|
|
|
|
tsslWarning.Text = string.Join("\r\n", _wsList.Select(u => u.WarningDescription));
|
|
}
|
|
}));
|
|
|
|
await Task.Delay(100);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 不带窗口的菜单项点击事件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void MenuItem_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.DockState = DockState.Document;
|
|
// }
|
|
// 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.MdiParent = this;
|
|
//dockFrm.DockPanel = dockPanelMain;
|
|
//dockFrm.SetLoginStatus(IsLogin);
|
|
//dockFrm.DockStateChanged += DockStateChanged;
|
|
////dockFrm.DockState = DockState.Document;
|
|
//dockFrm.Show();
|
|
|
|
//RegisterEvent(dockFrm);
|
|
//ResumeLayout();
|
|
|
|
//if (_process != null)
|
|
//{
|
|
// dockFrm.DownloadProcess(_process);
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogDisplay(new LogMsg(DateTime.Now, EnumHelper.LogLevel.Warning, ex.Message));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 带窗口的菜单项点击事件
|
|
/// </summary>
|
|
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.DockState = DockState.Document;
|
|
}
|
|
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.MdiParent = this;
|
|
dockFrm.DockPanel = dockPanelMain;
|
|
dockFrm.SetLoginStatus(IsLogin);
|
|
dockFrm.DockStateChanged += DockStateChanged;
|
|
//dockFrm.DockState = DockState.Document;
|
|
dockFrm.Show();
|
|
|
|
RegisterEvent(dockFrm);
|
|
ResumeLayout();
|
|
|
|
if (_process != null)
|
|
{
|
|
dockFrm.DownloadProcess(_process);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogDisplay(new LogMsg(DateTime.Now, EnumHelper.LogLevel.Warning, ex.Message));
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
private void MainFrm_Load(object sender, EventArgs e)
|
|
{
|
|
LoadLayoutFromXML(m_deserializeMenuFrm);
|
|
_allMenuLoadDoneHandle.Set();
|
|
|
|
AdvancedPwdFrm.OnLoginOK = OnLoginOK;
|
|
|
|
LoadProcess();
|
|
LoadLayoutFromXML(m_deserializeDeviceRunFrm);
|
|
LoadProcess(false);
|
|
}
|
|
|
|
|
|
|
|
private List<string> LoadProcessCode()
|
|
{
|
|
var systemProcessCodes = ProcessFactory.GetProcessCodes();
|
|
var avaiableProcessCodes = SettingHelper.GetProcessCodes();
|
|
|
|
List<string> pCodes = new List<string>();
|
|
|
|
if (avaiableProcessCodes.Count > 0)
|
|
{
|
|
pCodes = avaiableProcessCodes.Intersect(systemProcessCodes).ToList();
|
|
}
|
|
else
|
|
{
|
|
pCodes = systemProcessCodes;
|
|
}
|
|
|
|
if (pCodes.Count > 1)
|
|
{
|
|
pCodes.RemoveAll(u => u == "");
|
|
}
|
|
|
|
return pCodes;
|
|
}
|
|
|
|
private List<string> LoadProductionCode()
|
|
{
|
|
return SettingHelper.GetProductionCodes();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 载入流程
|
|
/// </summary>
|
|
/// <param name="isInitialProcess">是否为初始化流程</param>
|
|
private void LoadProcess(bool isInitialProcess = true)
|
|
{
|
|
if (isInitialProcess) // 初次创建流程
|
|
{
|
|
var processCodes = LoadProcessCode();
|
|
var productionCodes = LoadProductionCode();
|
|
|
|
_process = ProcessFactory.CreateStationProcess(processCodes[0], productionCodes[0], out string msg);
|
|
if (!string.IsNullOrWhiteSpace(msg))
|
|
{
|
|
_process = null;
|
|
|
|
throw new ProcessException($"Process创建失败,{msg}", null, ExceptionLevel.Fatal);
|
|
}
|
|
|
|
_process.InitialProcess("");
|
|
}
|
|
else // 后续加载流程
|
|
{
|
|
_process.OnLog -= LogDisplay;
|
|
_process.OnLog += LogDisplay;
|
|
_process.OnAlarmRaised -= Process_OnAlarmUpdate;
|
|
_process.OnAlarmRaised += Process_OnAlarmUpdate;
|
|
|
|
_process.OnProcessStateChanged -= _process_OnProcessStateChanged;
|
|
_process.OnProcessStateChanged += _process_OnProcessStateChanged;
|
|
|
|
LoadDevices();
|
|
|
|
try
|
|
{
|
|
foreach (var dock in dockPanelMain.Contents)
|
|
{
|
|
//不要在初次载入时下载流程,等界面都载入后再下载流程
|
|
// 上述“下载流程”,即为将流程加载到继承了 MenuFormBase 类的所有窗口中
|
|
if (dock is MenuFormBase menuFrm)
|
|
{
|
|
menuFrm.DownloadProcess(_process);
|
|
}
|
|
|
|
if (dock is DeviceRunFrmBase deviceFrm)
|
|
{
|
|
var device = _process.DeviceCollection.FirstOrDefault(u => u.Id == deviceFrm.Device.Id);
|
|
if (device != null)
|
|
{
|
|
deviceFrm.UpdateDevice(device);
|
|
}
|
|
else
|
|
{
|
|
deviceFrm.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
private void _process_OnProcessStateChanged(EnumHelper.DeviceState obj)
|
|
{
|
|
this.Invoke(new Action(() =>
|
|
{
|
|
notifyIcon.Text = Text = $"{SettingHelper.GetProgramDescription()} {obj.GetEnumDescription()}";
|
|
}));
|
|
}
|
|
|
|
|
|
#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 MenuFormBase;
|
|
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 = isLogin;
|
|
}
|
|
#endregion
|
|
|
|
#region CloseForm
|
|
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;
|
|
|
|
if (_process.DeviceCollection.Any(d => d.Id == u.Device.Id))
|
|
return;
|
|
|
|
u.DockPanel = null;
|
|
u.Close();
|
|
});
|
|
|
|
this.ResumeLayout();
|
|
}));
|
|
}
|
|
|
|
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();
|
|
// }
|
|
//}
|
|
|
|
try
|
|
{
|
|
_process?.Close();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
|
|
foreach (var dock in dockPanelMain.Contents.ToList())
|
|
{
|
|
(dock as Form).Close();
|
|
}
|
|
}
|
|
|
|
private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (!SettingHelper.SettingInfo.IsMinimumWhenClose || _isExit)
|
|
{
|
|
if (MessageBox.Show("是否确认关闭当前程序?", "关闭提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
|
|
{
|
|
e.Cancel = true;
|
|
return;
|
|
}
|
|
|
|
notifyIcon.Dispose();
|
|
|
|
CloseAllDocuments();
|
|
|
|
//System.Environment.Exit(0);
|
|
}
|
|
else
|
|
{
|
|
e.Cancel = true;
|
|
|
|
notifyIcon.ShowBalloonTip(5000, "后台消息", "程序转换到后台运行,双击图标再次打开程序界面!", ToolTipIcon.Info);
|
|
|
|
this.WindowState = FormWindowState.Minimized;
|
|
this.Visible = false;
|
|
}
|
|
}
|
|
|
|
bool _isExit = false;
|
|
private void tsmiExitProgram_Click(object sender, EventArgs e)
|
|
{
|
|
notifyIcon.Dispose();
|
|
|
|
CloseAllDocuments();
|
|
|
|
_isExit = true;
|
|
//System.Environment.Exit(0);
|
|
this.Close();
|
|
}
|
|
|
|
private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
|
|
{
|
|
//System.Environment.Exit(0);
|
|
}
|
|
|
|
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
|
|
{
|
|
this.Visible = true;
|
|
this.WindowState = FormWindowState.Maximized;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region Device Display 设备展示到底部状态栏
|
|
readonly Dictionary<string, DeviceRunFrmBase> showedDeviceUIDict = new Dictionary<string, DeviceRunFrmBase>();
|
|
|
|
//private void MainFrm_OnCheckedDeviceListChanged(List<string> deviceIds)
|
|
//{
|
|
// if (InvokeRequired)
|
|
// {
|
|
// Invoke(new Action(() => MainFrm_OnCheckedDeviceListChanged(deviceIds)));
|
|
// }
|
|
// else
|
|
// {
|
|
// List<string> closedUI = new List<string>();
|
|
// foreach (KeyValuePair<string, DockContent> 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<DeviceAttribute>();
|
|
// 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;
|
|
// }
|
|
// });
|
|
// }
|
|
//}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 加载设备到底部状态栏
|
|
/// </summary>
|
|
private void LoadDevices()
|
|
{
|
|
while (ststripDevices.Items.Count > 1)
|
|
{
|
|
ststripDevices.Items.RemoveAt(1);
|
|
}
|
|
|
|
ststripDevices.Invalidate();
|
|
|
|
_process.DeviceCollection.ForEach(dev =>
|
|
{
|
|
ToolStripStatusLabel tssl = new ToolStripStatusLabel(dev.Name);
|
|
tssl.ForeColor = System.Drawing.SystemColors.Control;
|
|
tssl.DoubleClickEnabled = true;
|
|
tssl.DoubleClick += Tssl_DoubleClick;
|
|
tssl.MouseHover += Tssl_MouseHover;
|
|
tssl.MouseLeave += Tssl_MouseLeave;
|
|
dev.OnDeviceStateChanged += Device_OnDeviceStateChanged;
|
|
tssl.Tag = dev;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 底部状态栏图标双击事件
|
|
/// </summary>
|
|
private void Tssl_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
if ((sender as ToolStripStatusLabel).Tag is IDevice device)
|
|
{
|
|
string typeCode = device.GetType().GetCustomAttribute<DeviceAttribute>()?.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;
|
|
runFrm.DockStateChanged += DockStateChanged;
|
|
showedDeviceUIDict[device.Id] = runFrm;
|
|
}
|
|
else
|
|
{
|
|
showedDeviceUIDict[device.Id].BringToFront();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 设备状态改变事件
|
|
/// </summary>
|
|
/// <param name="device">指定的设备</param>
|
|
/// <param name="currentState">当前状态</param>
|
|
private void Device_OnDeviceStateChanged(IDevice device, EnumHelper.DeviceState currentState)
|
|
{
|
|
ststripDevices.BeginInvoke(new Action(() =>
|
|
{
|
|
Console.WriteLine();
|
|
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<IDockContent> _dockContentList = new List<IDockContent>();
|
|
string _layoutFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{SettingHelper.GetSelectedLayout()}.layout");
|
|
private DeserializeDockContent m_deserializeMenuFrm;
|
|
private DeserializeDockContent m_deserializeDeviceRunFrm;
|
|
|
|
private void LoadLayoutMenu()
|
|
{
|
|
var checkedLayout = SettingHelper.GetSelectedLayout();
|
|
//1.获取所有layout文件
|
|
var fileNameList = StaticHelper.GetAllFilesByFolder(Path.Combine(AppDomain.CurrentDomain.BaseDirectory), "*.layout").Select(u => u.Name).ToList();
|
|
var clearCount = tssBtnLayout.DropDownItems.Count;
|
|
for (int i = 0; i < clearCount - 4; i++)
|
|
{
|
|
tssBtnLayout.DropDownItems.RemoveAt(0);
|
|
}
|
|
|
|
//2.加载到主menu
|
|
fileNameList.ForEach(fileName =>
|
|
{
|
|
ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
|
|
toolStripMenuItem.Text = Path.GetFileNameWithoutExtension(fileName);
|
|
toolStripMenuItem.Name = fileName;
|
|
toolStripMenuItem.Checked = toolStripMenuItem.Text == checkedLayout.ToLower();
|
|
toolStripMenuItem.Click += ToolStripMenuItem_Click;
|
|
tssBtnLayout.DropDownItems.Insert(0, toolStripMenuItem);
|
|
});
|
|
}
|
|
|
|
private void ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
LoadLayoutFromXML(m_deserializeMenuFrm);
|
|
var menuItem = sender as ToolStripMenuItem;
|
|
foreach (var menu in tssBtnLayout.DropDownItems)
|
|
{
|
|
if (menu is ToolStripMenuItem item && item.Name.Contains(".layout"))
|
|
{
|
|
item.Checked = false;
|
|
if (item.Text.ToLower() == menuItem.Text.ToLower())
|
|
{
|
|
item.Checked = true;
|
|
SettingHelper.SetCurrLayout(item.Text);
|
|
_layoutFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{item.Text}.layout");
|
|
}
|
|
}
|
|
}
|
|
LoadLayoutFromXML(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)
|
|
{
|
|
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];
|
|
|
|
//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)
|
|
{
|
|
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;
|
|
//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 (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 tssBtnLayout_Click(object sender, EventArgs e)
|
|
{
|
|
LoadLayoutMenu();
|
|
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);
|
|
LoadProcess(false);
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
private void MainFrm_KeyUp(object sender, KeyEventArgs e)
|
|
{
|
|
//if (e.KeyData == Keys.F11)
|
|
//{
|
|
// menuMain.Visible = !menuMain.Visible;
|
|
//}
|
|
}
|
|
|
|
private void tsmiSaveLayoutAs_Click(object sender, EventArgs e)
|
|
{
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
|
sfd.Title = "保存布局文件";
|
|
sfd.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
sfd.Filter = "布局文件| *.layout";
|
|
sfd.ShowDialog();
|
|
|
|
string path = sfd.FileName;
|
|
if (path == "")
|
|
{
|
|
return;
|
|
}
|
|
dockPanelMain.SaveAsXml(path);
|
|
MessageBox.Show("当前布局设置另存完成");
|
|
}
|
|
}
|
|
}
|