using BenQGuru.eMES.DLLService; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Reflection; using System.Windows.Forms; namespace UAS_DLLTest { public partial class Form1 : Form { MESHelper helper; Control.ControlCollection methodCollection; Control.ControlCollection ParamCollection; ParameterInfo[] param; List outParams = new List(); object[] allParams; int icount = 0; Queue query = new Queue();//临时存储获取到的control控件 MethodInfo[] method; int RowItemCount = 3; public Form1() { InitializeComponent(); helper = new MESHelper(); methodCollection = Methods.Controls; ParamCollection = Parameters.Controls; } private void Form1_Load(object sender, EventArgs e) { //动态生成checkBox method = helper.GetType().GetMethods(); List ShowMethods = new List(); for (int i = 0; i < method.Length; i++) { if (method[i].GetCustomAttributes(typeof(DescriptionAttribute), true).Length != 0) { ShowMethods.Add(method[i]); } } //动态的计算方法框的高度 //显示的控件才需要计数 int ControlHeight = 0; for (int i = 0; i < ShowMethods.Count; i++) { RadioButton btn = new RadioButton(); btn.Name = method[i].Name; btn.Text = method[i].Name; btn.Anchor = AnchorStyles.Left; btn.Anchor = AnchorStyles.Top; btn.AutoSize = true; ControlHeight = btn.Height; //父控件三分之一的长度减去Radio的长度再除以2 btn.Location = new Point((Methods.Width / 6 - btn.Width) / 2 + (i % RowItemCount) * (Methods.Width / RowItemCount), 25 + (i / RowItemCount) * (btn.Height)); Methods.Controls.Add(btn); btn.CheckedChanged += new EventHandler(radioButton_checkChanged); if (i == 0) { btn.Checked = true; } } //是否剩下一行没装满的 int LastRow = ShowMethods.Count % RowItemCount == 0 ? 0 : 1; Methods.Size = new Size(Methods.Width, ControlHeight * (ShowMethods.Count / RowItemCount + ShowMethods.Count % RowItemCount + 2)); Parameters.Location = new Point(Methods.Location.X, Methods.Location.Y + Methods.Size.Height); Result.Location = new Point(Parameters.Location.X, Parameters.Location.Y + Parameters.Size.Height); Height = Methods.Height + Parameters.Height + Result.Height + 50; //使最大化窗口失效 MaximizeBox = false; FormBorderStyle = FormBorderStyle.FixedSingle; } private void radioButton_checkChanged(object sender, EventArgs e) { removeControl(); for (int i = 0; i < methodCollection.Count; i++) { if (((RadioButton)methodCollection[i]).Checked) { //显示当前选中的方法的所有参数 //获取所有的参数 icount = 0; param = helper.GetType().GetMethod(methodCollection[i].Name).GetParameters(); for (int j = 1; j < param.Length + 1; j++) { //参数名i开头的才需要显示 if (param[j - 1].Name.StartsWith("i")) { icount += 1; ParamControl paramControl = new ParamControl(); paramControl.paramsName = param[j - 1].Name; paramControl.Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); paramControl.paramsValueName = param[j - 1].Name; paramControl.Anchor = AnchorStyles.Left; paramControl.Anchor = AnchorStyles.Top; paramControl.Location = new Point((Parameters.Width / 3 - paramControl.Width) / 2 + ((j - 1) % 3) * (Parameters.Width / 3), 25 + ((j - 1) / 3) * (paramControl.Height)); Parameters.Controls.Add(paramControl); } } allParams = new object[param.Length]; } } //重新获取控件 ParamCollection = Parameters.Controls; } private void confirm_Click(object sender, EventArgs e) { //拿到所有已经填写的参数 for (int i = 0; i < ParamCollection.Count; i++) { for (int j = 1; j < allParams.Length + 1; j++) { if (j > icount) { allParams[j - 1] = new string(new char[] { }); } else { if (ParamCollection[i] is ParamControl && ((ParamControl)ParamCollection[i]).paramsValueName == param[j - 1].Name) { if (param[j - 1].ParameterType.ToString() != "System.String[]") { allParams[j - 1] = ((ParamControl)ParamCollection[i]).paramsValue; } else { allParams[j - 1] = ((ParamControl)ParamCollection[i]).paramsValue.Split(','); } } } } } //将out类型的参数放进去 string oResult = ""; //拿到需要触发的方法名 for (int i = 0; i < methodCollection.Count; i++) { if (((RadioButton)methodCollection[i]).Checked) { Type type = helper.GetType(); MethodInfo method = type.GetMethod(((RadioButton)methodCollection[i]).Name); oResult = method.Invoke(helper, allParams).ToString(); } } //输出out出的信息 operateResult.AppendText("返回值:" + oResult + ","); for (int i = 0; i < param.Length; i++) { if (param[i].Name.StartsWith("o")) { operateResult.AppendText(param[i].Name + ":" + allParams[i] + ","); } } operateResult.AppendText("\n"); } private void clear_Click(object sender, EventArgs e) { //清除信息 if (clearPara.Checked) { for (int i = 0; i < ParamCollection.Count; i++) { if (ParamCollection[i] is ParamControl) { ((ParamControl)ParamCollection[i]).paramsValue = ""; } } } //清除日志 if (clearResult.Checked) { operateResult.Clear(); } } /// /// 移除所有控件 /// private void removeControl() { //移除所有ParamControl控件 foreach (Control c in this.Parameters.Controls) { if (c is ParamControl) { query.Enqueue(c); } } while (query.Count != 0) { query.Dequeue().Dispose(); } } } }