StationOpMainForm.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using PlcUiControl;
  2. using Sunny.UI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Xml.Linq;
  15. using PlcUiForm;
  16. namespace YangjieTester.工站操作
  17. {
  18. [FormDescriptionAttribute("工站操作主画面")]
  19. public partial class StationOpMainForm : PlcBaseForm
  20. {
  21. public StationOpMainForm()
  22. {
  23. InitializeComponent();
  24. uiFlowLayoutPanel1.Height = btn0主站马达.Height * 3 + 25;
  25. }
  26. private void uiFlowLayoutPanel1_SizeChanged(object sender, EventArgs e)
  27. {
  28. foreach (Control control in uiFlowLayoutPanel1.AllControls)
  29. {
  30. control.Size = new Size((int)(uiFlowLayoutPanel1.Width *0.094), control.Height);
  31. }
  32. }
  33. static object preClickedBtn = null;
  34. private void btnStationForm_Click(object sender, EventArgs e)
  35. {
  36. var formType = GetFormTypeByButtonName(((UISymbolButton)sender).Name);
  37. if (formType != null)
  38. {
  39. FormEx.SetButtonSelected(sender);
  40. preClickedBtn = sender;
  41. UIForm form = (UIForm)Activator.CreateInstance(formType);
  42. FormEx.ShowSubForm(uiPanel1, form);
  43. }
  44. }
  45. private Type GetFormTypeByButtonName(string buttonName)
  46. {
  47. // 1. 从按钮名提取开头的数字(btn 后连续数字)
  48. var match = Regex.Match(buttonName, @"^btn(\d+)", RegexOptions.IgnoreCase);
  49. if (!match.Success)
  50. return null;
  51. string number = match.Groups[1].Value; // 如 "1", "12"
  52. string namespaceName = typeof(StationOpMainForm).Namespace;
  53. Assembly assembly = Assembly.GetExecutingAssembly();
  54. // 2. 构造正则:匹配 Form{number} + (结尾 或 下划线开头的后缀)
  55. // 例如 number="1" → ^Form1(_|$)
  56. string pattern = $@"^Form{Regex.Escape(number)}(?:_|$)";
  57. var formNameRegex = new Regex(pattern, RegexOptions.IgnoreCase);
  58. var formType = assembly.GetTypes()
  59. .FirstOrDefault(t =>
  60. t.IsClass &&
  61. !t.IsAbstract &&
  62. typeof(UIForm).IsAssignableFrom(t) &&
  63. t.Namespace == namespaceName &&
  64. formNameRegex.IsMatch(t.Name));
  65. return formType;
  66. }
  67. private void StationOpMainForm_Load(object sender, EventArgs e)
  68. {
  69. if (preClickedBtn != null)
  70. {
  71. btnStationForm_Click(preClickedBtn, null);
  72. var btnIndex= uiFlowLayoutPanel1.AllControls.IndexOfKey(((UIButton)preClickedBtn).Name);
  73. if(btnIndex>0)
  74. {
  75. FormEx.SetButtonSelected(uiFlowLayoutPanel1.AllControls[btnIndex]);
  76. }
  77. }
  78. else
  79. btnStationForm_Click(btn0主站马达, null);
  80. }
  81. }
  82. }