Form1.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. for (int i = 0; i < ShowMethods.Count; i++)
  45. {
  46. RadioButton btn = new RadioButton();
  47. btn.Name = method[i].Name;
  48. btn.Text = method[i].Name;
  49. btn.Anchor = AnchorStyles.Left;
  50. btn.Anchor = AnchorStyles.Top;
  51. btn.AutoSize = true;
  52. ControlHeight = btn.Height;
  53. //父控件三分之一的长度减去Radio的长度再除以2
  54. btn.Location = new Point((Methods.Width / 6 - btn.Width) / 2 + (i % RowItemCount) * (Methods.Width / RowItemCount), 25 + (i / RowItemCount) * (btn.Height));
  55. Methods.Controls.Add(btn);
  56. btn.CheckedChanged += new EventHandler(radioButton_checkChanged);
  57. if (i == 0)
  58. {
  59. btn.Checked = true;
  60. }
  61. }
  62. //是否剩下一行没装满的
  63. int LastRow = ShowMethods.Count % RowItemCount == 0 ? 0 : 1;
  64. Methods.Size = new Size(Methods.Width, ControlHeight * (ShowMethods.Count / RowItemCount + ShowMethods.Count % RowItemCount + 2));
  65. Parameters.Location = new Point(Methods.Location.X, Methods.Location.Y + Methods.Size.Height);
  66. Result.Location = new Point(Parameters.Location.X, Parameters.Location.Y + Parameters.Size.Height);
  67. Height = Methods.Height + Parameters.Height + Result.Height + 50;
  68. //使最大化窗口失效
  69. MaximizeBox = false;
  70. FormBorderStyle = FormBorderStyle.FixedSingle;
  71. }
  72. private void radioButton_checkChanged(object sender, EventArgs e)
  73. {
  74. removeControl();
  75. for (int i = 0; i < methodCollection.Count; i++)
  76. {
  77. if (((RadioButton)methodCollection[i]).Checked)
  78. {
  79. //显示当前选中的方法的所有参数
  80. //获取所有的参数
  81. icount = 0;
  82. param = helper.GetType().GetMethod(methodCollection[i].Name).GetParameters();
  83. for (int j = 1; j < param.Length + 1; j++)
  84. {
  85. //参数名i开头的才需要显示
  86. if (param[j - 1].Name.StartsWith("i"))
  87. {
  88. icount += 1;
  89. ParamControl paramControl = new ParamControl();
  90. paramControl.paramsName = param[j - 1].Name;
  91. paramControl.Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
  92. paramControl.paramsValueName = param[j - 1].Name;
  93. paramControl.Anchor = AnchorStyles.Left;
  94. paramControl.Anchor = AnchorStyles.Top;
  95. paramControl.Location = new Point((Parameters.Width / 3 - paramControl.Width) / 2 + ((j - 1) % 3) * (Parameters.Width / 3), 25 + ((j - 1) / 3) * (paramControl.Height));
  96. Parameters.Controls.Add(paramControl);
  97. }
  98. }
  99. allParams = new object[param.Length];
  100. }
  101. }
  102. //重新获取控件
  103. ParamCollection = Parameters.Controls;
  104. }
  105. private void confirm_Click(object sender, EventArgs e)
  106. {
  107. //拿到所有已经填写的参数
  108. for (int i = 0; i < ParamCollection.Count; i++)
  109. {
  110. for (int j = 1; j < allParams.Length + 1; j++)
  111. {
  112. if (j > icount)
  113. {
  114. allParams[j - 1] = new string(new char[] { });
  115. }
  116. else
  117. {
  118. if (ParamCollection[i] is ParamControl && ((ParamControl)ParamCollection[i]).paramsValueName == param[j - 1].Name)
  119. {
  120. if (param[j - 1].ParameterType.ToString() != "System.String[]")
  121. {
  122. allParams[j - 1] = ((ParamControl)ParamCollection[i]).paramsValue;
  123. }
  124. else
  125. {
  126. allParams[j - 1] = ((ParamControl)ParamCollection[i]).paramsValue.Split(',');
  127. }
  128. }
  129. }
  130. }
  131. }
  132. //将out类型的参数放进去
  133. string oResult = "";
  134. //拿到需要触发的方法名
  135. for (int i = 0; i < methodCollection.Count; i++)
  136. {
  137. if (((RadioButton)methodCollection[i]).Checked)
  138. {
  139. Type type = helper.GetType();
  140. MethodInfo method = type.GetMethod(((RadioButton)methodCollection[i]).Name);
  141. oResult = method.Invoke(helper, allParams).ToString();
  142. }
  143. }
  144. //输出out出的信息
  145. operateResult.AppendText("返回值:" + oResult + ",");
  146. for (int i = 0; i < param.Length; i++)
  147. {
  148. if (param[i].Name.StartsWith("o"))
  149. {
  150. operateResult.AppendText(param[i].Name + ":" + allParams[i] + ",");
  151. }
  152. }
  153. operateResult.AppendText("\n");
  154. }
  155. private void clear_Click(object sender, EventArgs e)
  156. {
  157. //清除信息
  158. if (clearPara.Checked)
  159. {
  160. for (int i = 0; i < ParamCollection.Count; i++)
  161. {
  162. if (ParamCollection[i] is ParamControl)
  163. {
  164. ((ParamControl)ParamCollection[i]).paramsValue = "";
  165. }
  166. }
  167. }
  168. //清除日志
  169. if (clearResult.Checked)
  170. {
  171. operateResult.Clear();
  172. }
  173. }
  174. /// <summary>
  175. /// 移除所有控件
  176. /// </summary>
  177. private void removeControl()
  178. {
  179. //移除所有ParamControl控件
  180. foreach (Control c in this.Parameters.Controls)
  181. {
  182. if (c is ParamControl)
  183. {
  184. query.Enqueue(c);
  185. }
  186. }
  187. while (query.Count != 0)
  188. {
  189. query.Dequeue().Dispose();
  190. }
  191. }
  192. }
  193. }