Form1.cs 7.9 KB

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