Form1.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using BenQGuru.eMES.DLLService;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Reflection;
  7. using System.Windows.Forms;
  8. namespace UAS_DLLTest
  9. {
  10. public partial class Form1 : Form
  11. {
  12. MESHelper helper;
  13. Control.ControlCollection methodCollection;
  14. Control.ControlCollection ParamCollection;
  15. ParameterInfo[] param;
  16. List<string> outParams = new List<string>();
  17. object[] allParams;
  18. int icount = 0;
  19. Queue<Control> query = new Queue<Control>();//临时存储获取到的control控件
  20. MethodInfo[] method;
  21. int RowItemCount = 3;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. helper = new MESHelper();
  26. methodCollection = Methods.Controls;
  27. ParamCollection = Parameters.Controls;
  28. }
  29. private void Form1_Load(object sender, EventArgs e)
  30. {
  31. //动态生成checkBox
  32. method = helper.GetType().GetMethods();
  33. List<MethodInfo> ShowMethods = new List<MethodInfo>();
  34. for (int i = 0; i < method.Length; i++)
  35. {
  36. if (method[i].GetCustomAttributes(typeof(DescriptionAttribute), true).Length != 0)
  37. {
  38. ShowMethods.Add(method[i]);
  39. }
  40. }
  41. //动态的计算方法框的高度
  42. //显示的控件才需要计数
  43. int ControlHeight = 0;
  44. Console.WriteLine(ShowMethods.Count);
  45. for (int i = 0; i < ShowMethods.Count; i++)
  46. {
  47. RadioButton btn = new RadioButton();
  48. btn.Name = method[i].Name;
  49. btn.Text = method[i].Name;
  50. btn.Anchor = AnchorStyles.Left;
  51. btn.Anchor = AnchorStyles.Top;
  52. btn.AutoSize = true;
  53. ControlHeight = btn.Height;
  54. //父控件三分之一的长度减去Radio的长度再除以2
  55. btn.Location = new Point((Methods.Width / 6 - btn.Width) / 2 + (i % RowItemCount) * (Methods.Width / RowItemCount), 25 + (i / RowItemCount) * (btn.Height));
  56. Methods.Controls.Add(btn);
  57. btn.CheckedChanged += new EventHandler(radioButton_checkChanged);
  58. }
  59. //是否剩下一行没装满的
  60. int LastRow = ShowMethods.Count % RowItemCount == 0 ? 0 : 1;
  61. Methods.Size = new Size(Methods.Width, ControlHeight * (ShowMethods.Count / RowItemCount + ShowMethods.Count % RowItemCount+2));
  62. Parameters.Location = new Point(Methods.Location.X, Methods.Location.Y + Methods.Size.Height);
  63. Result.Location = new Point(Parameters.Location.X, Parameters.Location.Y + Parameters.Size.Height);
  64. Height = Methods.Height + Parameters.Height + Result.Height + 50;
  65. //使最大化窗口失效
  66. MaximizeBox = false;
  67. FormBorderStyle = FormBorderStyle.FixedSingle;
  68. }
  69. private void radioButton_checkChanged(object sender, EventArgs e)
  70. {
  71. removeControl();
  72. for (int i = 0; i < methodCollection.Count; i++)
  73. {
  74. if (((RadioButton)methodCollection[i]).Checked)
  75. {
  76. //显示当前选中的方法的所有参数
  77. //获取所有的参数
  78. icount = 0;
  79. param = helper.GetType().GetMethod(methodCollection[i].Name).GetParameters();
  80. for (int j = 1; j < param.Length + 1; j++)
  81. {
  82. //参数名i开头的才需要显示
  83. if (param[j - 1].Name.StartsWith("i"))
  84. {
  85. icount += 1;
  86. ParamControl paramControl = new ParamControl();
  87. paramControl.paramsName = param[j - 1].Name;
  88. paramControl.Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
  89. paramControl.paramsValueName = param[j - 1].Name;
  90. paramControl.Anchor = AnchorStyles.Left;
  91. paramControl.Anchor = AnchorStyles.Top;
  92. paramControl.Location = new Point((Parameters.Width / 3 - paramControl.Width) / 2 + ((j - 1) % 3) * (Parameters.Width / 3), 25 + ((j - 1) / 3) * (paramControl.Height));
  93. Parameters.Controls.Add(paramControl);
  94. }
  95. }
  96. allParams = new object[param.Length];
  97. }
  98. }
  99. //重新获取控件
  100. ParamCollection = Parameters.Controls;
  101. }
  102. private void confirm_Click(object sender, EventArgs e)
  103. {
  104. //拿到所有已经填写的参数
  105. for (int i = 0; i < ParamCollection.Count; i++)
  106. {
  107. for (int j = 1; j < allParams.Length + 1; j++)
  108. {
  109. if (j > icount)
  110. {
  111. allParams[j - 1] = new string(new char[] { });
  112. }
  113. else
  114. {
  115. if (ParamCollection[i] is ParamControl && ((ParamControl)ParamCollection[i]).paramsValueName == param[j - 1].Name)
  116. {
  117. if (param[j - 1].ParameterType.ToString() != "System.String[]")
  118. {
  119. allParams[j - 1] = ((ParamControl)ParamCollection[i]).paramsValue;
  120. }
  121. else
  122. {
  123. allParams[j - 1] = ((ParamControl)ParamCollection[i]).paramsValue.Split(',');
  124. }
  125. }
  126. }
  127. }
  128. }
  129. //将out类型的参数放进去
  130. string oResult = "";
  131. //拿到需要触发的方法名
  132. for (int i = 0; i < methodCollection.Count; i++)
  133. {
  134. if (((RadioButton)methodCollection[i]).Checked)
  135. {
  136. Type type = helper.GetType();
  137. MethodInfo method = type.GetMethod(((RadioButton)methodCollection[i]).Name);
  138. oResult = method.Invoke(helper, allParams).ToString();
  139. }
  140. }
  141. //输出out出的信息
  142. operateResult.AppendText("返回值:" + oResult + ",");
  143. for (int i = 0; i < param.Length; i++)
  144. {
  145. if (param[i].Name.StartsWith("o"))
  146. {
  147. operateResult.AppendText(param[i].Name + ":" + allParams[i] + ",");
  148. }
  149. }
  150. operateResult.AppendText("\n");
  151. }
  152. private void clear_Click(object sender, EventArgs e)
  153. {
  154. //清除信息
  155. if (clearPara.Checked)
  156. {
  157. for (int i = 0; i < ParamCollection.Count; i++)
  158. {
  159. if (ParamCollection[i] is ParamControl)
  160. {
  161. ((ParamControl)ParamCollection[i]).paramsValue = "";
  162. }
  163. }
  164. }
  165. //清除日志
  166. if (clearResult.Checked)
  167. {
  168. operateResult.Clear();
  169. }
  170. }
  171. /// <summary>
  172. /// 移除所有控件
  173. /// </summary>
  174. private void removeControl()
  175. {
  176. //移除所有ParamControl控件
  177. foreach (Control c in this.Parameters.Controls)
  178. {
  179. if (c is ParamControl)
  180. {
  181. query.Enqueue(c);
  182. }
  183. }
  184. while (query.Count != 0)
  185. {
  186. query.Dequeue().Dispose();
  187. }
  188. }
  189. }
  190. }