using CowainHmi.S1; using PCHMI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using XPHMI; namespace CowainHmi { public partial class FrmMain : Form { int survey = 0; int A = 0; //RFID读 int B = 0; //RFI写 int B1 = 0; ////回原弹窗 int C = 0; //相机NG弹窗 public FrmMain() { InitializeComponent(); } private async void FrmMain_Load(object sender, EventArgs e) { config1.START(this); FrmProgress frm = new FrmProgress(); frm.TopMost = true; frm.Show(); frm.Activate(); await LoadDataAsync(); } private async Task LoadDataAsync() { PCHMI.RT.SEND_UINT16(0, "U200", 0); //窗体加载时把站名称都读上来 await ReadStationNames(); GlobalData.StationChanged += GlobalData_StationChanged; GlobalData.StationId = 0; this.timer1.Enabled = true; } private async Task ReadStationNames() { //PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.0", 6); //设备名称 //PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.8", 6); //设备名称 //PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.16", 6); //设备名称 //PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.24", 6); //设备名称 string[] names = this.Tag.ToString().Split(','); await Task.Delay(300); //List list = new List(); //list.Add(PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.0", 6)); //list.Add(PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.8", 6)); //list.Add(PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.16", 6)); //list.Add(PCHMI.VL.GET_SIEMENS_STRING(0, "DB999.24", 6)); foreach (var item in names) { if (!string.IsNullOrEmpty(item)) { GlobalData.StationNames.Add(item); } } } private void GlobalData_StationChanged(object sender, int e) { int pageId = (e + 1) * 100; bool ret = ShowStation(e); if (ret) { PCHMI.RT.SEND_UINT16(0, "U200", (ushort)pageId); } else { MessageBox.Show("画面为空,不能创建", "提示"); } } private ushort safeErrLast; private void timer1_Tick(object sender, EventArgs e) { ushort safeErr = PCHMI.RT.GET_BIT(0, "U:S1_SafeErr"); //出现安全信号故障,主动弹出窗口 if (safeErr > 0 && safeErr != safeErrLast) { DlgS1安全.GetInstance().Show(); DlgS1安全.GetInstance().Activate(); } safeErrLast = safeErr; survey = PCHMI.RT.GET_INT16(0, ""); if (survey == 1) { ShowPotho(); } if (survey == 2) { ShowPotho1(); } ////回原弹窗 B1 = PCHMI.RT.GET_INT16(0, "DB1651.20"); if (B1 == 1) { 弹窗1_回原选择 OBJ = new 弹窗1_回原选择(); //OBJ.TopMost = true; OBJ.Show(); PCHMI.RT.SEND_INT16(0, "DB1651.20", 2); } //RFID读 A = PCHMI.RT.GET_INT16(0, "DB1651.22"); if (A == 1) { DlgS1RFID1读超时 OBJ = new DlgS1RFID1读超时(); //OBJ.TopMost = true; OBJ.Show(); PCHMI.RT.SEND_INT16(0, "DB1651.22", 2); } //RFID写 B = PCHMI.RT.GET_INT16(0, "DB1651.24"); if (B == 1) { DlgS1RFID1写超时 OBJ = new DlgS1RFID1写超时(); //OBJ.TopMost = true; OBJ.Show(); PCHMI.RT.SEND_INT16(0, "DB1651.24", 2); } C = PCHMI.RT.GET_INT16(0, "DB1651.26"); if (C == 1) { 弹窗2_相机NG弹窗 OBJ = new 弹窗2_相机NG弹窗(); //OBJ.TopMost = true; OBJ.Show(); PCHMI.RT.SEND_INT16(0, "DB1651.26", 2); } } private void ShowPotho() //打开笑脸 { //DlgS1笑脸 OBJ = new DlgS1笑脸(); //OBJ.TopMost = true; //OBJ.Location = new Point(300, 161); //OBJ.Show(); //PCHMI.RT.SEND_INT16(0, "DB1651.20", 10); } private void ShowPotho1() //打开哭脸 { //DlgS1哭脸 C = new DlgS1哭脸(); ////C.TopMost = true; //C.Location = new Point(300, 161); //C.Show(); //PCHMI.RT.SEND_INT16(0, "DB1651.20", 10); } private bool ShowStation(int stationId) { RemoveControlByName(this, "顶部"); RemoveControlByName(this, "任务栏"); var top = CreateStationControl(stationId, "顶部"); if (top == null) { return false; } var bottom = CreateStationControl(stationId, "任务栏"); if (bottom == null) { return false; } // // 顶部 // top.Dock = System.Windows.Forms.DockStyle.Top; top.Location = new System.Drawing.Point(0, 0); top.Name = "顶部"; top.Size = new System.Drawing.Size(1366, 60); top.TabIndex = 0; // // 任务栏 // bottom.Dock = System.Windows.Forms.DockStyle.Bottom; bottom.Location = new System.Drawing.Point(0, 688); bottom.Name = "任务栏"; bottom.Size = new System.Drawing.Size(1366, 80); bottom.TabIndex = 1; this.Controls.Add(top); this.Controls.Add(bottom); return true; } private void RemoveControlByName(Control parent, string controlName) { // 遍历所有子控件 foreach (Control child in parent.Controls) { // 如果找到了指定名称的控件 if (child.Name == controlName) { // 从父控件的Controls集合中移除该控件 parent.Controls.Remove(child); // 如果需要,可以在这里添加更多逻辑,比如释放资源等 // 因为我们找到了并移除了控件,所以不需要继续搜索 return; } // 如果当前控件还包含子控件,则递归搜索 if (child.HasChildren) { RemoveControlByName(child, controlName); } } } public UserControl CreateStationControl(int stationId, string name) { string topName = $"S{stationId + 1}{name}"; var baseType = typeof(UserControl); Assembly asm = Assembly.GetExecutingAssembly(); var types = asm.GetTypes() .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray(); if (types != null && types.Length > 0) { var type = types.Where(x => x.IsClass && x.Name == topName).FirstOrDefault(); if (type != null && type.IsSubclassOf(typeof(UserControl))) { // 假设所有相关控件都有一个接受int类型ID的构造函数 return (UserControl)Activator.CreateInstance(type); } } return null; // 如果没有找到或类型不匹配,则返回null } } }