FormHOZMain.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. using FileManager;
  2. using MeasureData;
  3. using MeasureThread;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using static MeasureThread.ThreadStatusEventArgs;
  17. namespace HOZProject
  18. {
  19. public partial class FormHOZMain : Form
  20. {
  21. #region 控制窗体可以调整大小用
  22. const int HTLEFT = 10;
  23. const int HTRIGHT = 11;
  24. const int HTTOP = 12;
  25. const int HTTOPLEFT = 13;
  26. const int HTTOPRIGHT = 14;
  27. const int HTBOTTOM = 15;
  28. const int HTBOTTOMLEFT = 0x10;
  29. const int HTBOTTOMRIGHT = 17;
  30. protected override void WndProc(ref Message m)
  31. {
  32. switch (m.Msg)
  33. {
  34. case 0x0084:
  35. base.WndProc(ref m);
  36. Point vPoint = new Point((int)m.LParam & 0xFFFF,
  37. (int)m.LParam >> 16 & 0xFFFF);
  38. vPoint = PointToClient(vPoint);
  39. if (vPoint.X <= 5)
  40. if (vPoint.Y <= 5)
  41. m.Result = (IntPtr)HTTOPLEFT;
  42. else if (vPoint.Y >= ClientSize.Height - 5)
  43. m.Result = (IntPtr)HTBOTTOMLEFT;
  44. else m.Result = (IntPtr)HTLEFT;
  45. else if (vPoint.X >= ClientSize.Width - 5)
  46. if (vPoint.Y <= 5)
  47. m.Result = (IntPtr)HTTOPRIGHT;
  48. else if (vPoint.Y >= ClientSize.Height - 5)
  49. m.Result = (IntPtr)HTBOTTOMRIGHT;
  50. else m.Result = (IntPtr)HTRIGHT;
  51. else if (vPoint.Y <= 5)
  52. m.Result = (IntPtr)HTTOP;
  53. else if (vPoint.Y >= ClientSize.Height - 5)
  54. m.Result = (IntPtr)HTBOTTOM;
  55. break;
  56. case 0x0201://鼠标左键按下的消息
  57. m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
  58. m.LParam = IntPtr.Zero;//默认值
  59. m.WParam = new IntPtr(2);//鼠标放在标题栏内
  60. base.WndProc(ref m);
  61. break;
  62. default:
  63. base.WndProc(ref m);
  64. break;
  65. }
  66. }
  67. #endregion
  68. #region 成员变量
  69. public BackgroundWorker m_BackgroundWorker;// 申明后台对象
  70. /// <summary>
  71. /// 测量文件
  72. /// </summary>
  73. public MeasureFile m_MeasureFile;
  74. /// 测量线程
  75. public Measure m_Ms;
  76. /// <summary>
  77. /// 显示日志窗体
  78. /// </summary>
  79. Form m_FormLog = null;
  80. /// <summary>
  81. /// 显示实例化窗体
  82. /// </summary>
  83. FormMove m_FormInit = null;
  84. /// <summary>
  85. /// 是否已保存
  86. /// </summary>
  87. public bool IsSave = false;
  88. /// <summary>
  89. /// 初始化用户控件
  90. /// </summary>
  91. UControl_Init uControl_Init = null;
  92. /// <summary>
  93. /// 日志用户控件
  94. /// </summary>
  95. UControl_Log ucLog = null;
  96. //流程控制
  97. public int m_MeasureType = (int)MeasureMsgManage.measureType.FIB;
  98. //是否关闭窗体标识
  99. public bool IsClose = false;
  100. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  101. #endregion
  102. #region 构造函数
  103. public FormHOZMain()
  104. {
  105. InitializeComponent();
  106. //在线程操作过程中,可以设置控件属性
  107. Control.CheckForIllegalCrossThreadCalls = false;
  108. m_BackgroundWorker = new BackgroundWorker(); // 实例化后台对象
  109. m_BackgroundWorker.WorkerReportsProgress = true; // 设置可以通告进度
  110. m_BackgroundWorker.WorkerSupportsCancellation = true; // 设置可以取消
  111. m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
  112. m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
  113. m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);
  114. LogManager.InitManulLog();
  115. }
  116. #endregion
  117. #region 测量线程
  118. void DoWork(object sender, DoWorkEventArgs e)
  119. {
  120. try
  121. {
  122. m_Ms = new Measure(ConfigurationManager.AppSettings["WebServerIP"].ToString(),
  123. ConfigurationManager.AppSettings["WebServerPort"].ToString(),
  124. ConfigurationManager.AppSettings["WebServerUrl"].ToString());
  125. m_MeasureFile.MParam.AutoFocus.UP = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_UP"].ToString());
  126. m_MeasureFile.MParam.AutoFocus.Down = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Down"].ToString());
  127. m_MeasureFile.MParam.AutoFocus.Step = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Step"].ToString());
  128. m_MeasureFile.MParam.AutoFocus.Range = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Range"].ToString());
  129. m_MeasureFile.MParam.AutoFocus.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_FStep"].ToString());
  130. m_MeasureFile.MParam.FIBFocus.UP = Convert.ToSingle(ConfigurationManager.AppSettings["FIB_UP"].ToString());
  131. m_MeasureFile.MParam.FIBFocus.Down = Convert.ToSingle(ConfigurationManager.AppSettings["FIB_Down"].ToString());
  132. m_MeasureFile.MParam.FIBFocus.Step = Convert.ToSingle(ConfigurationManager.AppSettings["FIB_Step"].ToString());
  133. m_MeasureFile.MParam.FIBFocus.Range = Convert.ToSingle(ConfigurationManager.AppSettings["FIB_Range"].ToString());
  134. m_MeasureFile.MParam.FIBFocus.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["FIB_FStep"].ToString());
  135. m_MeasureFile.MParam.AutoStigX.UP = Convert.ToSingle(ConfigurationManager.AppSettings["StigX_UP"].ToString());
  136. m_MeasureFile.MParam.AutoStigX.Down = Convert.ToSingle(ConfigurationManager.AppSettings["StigX_Down"].ToString());
  137. m_MeasureFile.MParam.AutoStigX.Step = Convert.ToSingle(ConfigurationManager.AppSettings["StigX_Step"].ToString());
  138. m_MeasureFile.MParam.AutoStigX.Range = Convert.ToSingle(ConfigurationManager.AppSettings["StigX_Range"].ToString());
  139. m_MeasureFile.MParam.AutoStigX.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["StigX_FStep"].ToString());
  140. m_MeasureFile.MParam.AutoStigY.UP = Convert.ToSingle(ConfigurationManager.AppSettings["StigY_UP"].ToString());
  141. m_MeasureFile.MParam.AutoStigY.Down = Convert.ToSingle(ConfigurationManager.AppSettings["StigY_Down"].ToString());
  142. m_MeasureFile.MParam.AutoStigY.Step = Convert.ToSingle(ConfigurationManager.AppSettings["StigY_Step"].ToString());
  143. m_MeasureFile.MParam.AutoStigY.Range = Convert.ToSingle(ConfigurationManager.AppSettings["StigY_Range"].ToString());
  144. m_MeasureFile.MParam.AutoStigY.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["StigY_FStep"].ToString());
  145. m_Ms.X_Min = Convert.ToSingle(ConfigurationManager.AppSettings["X_Min"]);
  146. m_Ms.X_Max = Convert.ToSingle(ConfigurationManager.AppSettings["X_Max"]);
  147. m_Ms.Y_Min = Convert.ToSingle(ConfigurationManager.AppSettings["Y_Min"]);
  148. m_Ms.Y_Max = Convert.ToSingle(ConfigurationManager.AppSettings["Y_Max"]);
  149. m_Ms.Z_Min = Convert.ToSingle(ConfigurationManager.AppSettings["Z_Min"]);
  150. m_Ms.Z_Max = Convert.ToSingle(ConfigurationManager.AppSettings["Z_Max"]);
  151. m_Ms.T_Min = Convert.ToSingle(ConfigurationManager.AppSettings["T_Min"]);
  152. m_Ms.T_Max = Convert.ToSingle(ConfigurationManager.AppSettings["T_Max"]);
  153. m_Ms.R_Min = Convert.ToSingle(ConfigurationManager.AppSettings["R_Min"]);
  154. m_Ms.R_Max = Convert.ToSingle(ConfigurationManager.AppSettings["R_Max"]);
  155. m_Ms.M_Min = Convert.ToSingle(ConfigurationManager.AppSettings["M_Min"]);
  156. m_Ms.M_Max = Convert.ToSingle(ConfigurationManager.AppSettings["M_Max"]);
  157. //人工干预
  158. m_Ms.hand_intervene = Convert.ToInt32(ConfigurationManager.AppSettings["Hand_Intervene"]);
  159. //SmartSEM远程路径
  160. m_Ms.RemoteELYPath = ConfigurationManager.AppSettings["RemoteELYPath"];
  161. m_Ms.RemoteMLFPath = ConfigurationManager.AppSettings["RemoteMLFPath"];
  162. //Z轴移动距离
  163. m_MeasureFile.MParam.ZDistance= Convert.ToSingle(ConfigurationManager.AppSettings["ZDistance"]);
  164. m_Ms.InitMeas(m_MeasureFile);
  165. //注册事件
  166. m_Ms.SendThreadStatus += new ThreadStatusHandler(displayMessage);
  167. //注册事件
  168. m_Ms.SendCutHolesStatus += new CutHolesStatusHandler(displayCutHoleMessage);
  169. //设置控件操作
  170. SetWinControlMeasureState(false);
  171. //自动测量的全过程
  172. m_Ms.DoMeasure();
  173. }
  174. catch (Exception ex)
  175. {
  176. LogManager.LogError(ex.ToString());
  177. }
  178. }
  179. public void displayMessage(object sender, ThreadStatusEventArgs e)
  180. {
  181. //主界面显示内容
  182. this.BeginInvoke((Action)delegate
  183. {
  184. this.listmsg.Items.Add(e.HoleName +"_" + e.Time.ToString() + "_"+ e.Message + "_" + e.State );
  185. //显示流程中对应编号的内容
  186. MeasureMsgManage.ShowMsgContent(this, e);
  187. AddLogListInfo(e);
  188. });
  189. }
  190. public void displayCutHoleMessage(object sender, CutHolesStatusEventArgs e)
  191. {
  192. //主界面显示内容
  193. this.BeginInvoke((Action)delegate
  194. {
  195. //修改切孔状态
  196. ChangeCutHoleState(e.HoleName.ToString(), Convert.ToInt32(e.State));
  197. });
  198. }
  199. void UpdateProgress(object sender, ProgressChangedEventArgs e)
  200. {
  201. }
  202. void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
  203. {
  204. //设置控件操作
  205. MessageBox.Show("测量已完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  206. SetWinControlMeasureState(true);
  207. }
  208. #endregion
  209. #region 窗体加载
  210. private void FormHOZMain_Load(object sender, EventArgs e)
  211. {
  212. //加载控件的点击事件
  213. this.Click += new EventHandler(FormHOZMain_Click);
  214. plFill.Click += new EventHandler(FormHOZMain_Click);
  215. plTop.Click += new EventHandler(FormHOZMain_Click);
  216. plLeft.Click += new EventHandler(FormHOZMain_Click);
  217. plLeftContent.Click += new EventHandler(FormHOZMain_Click);
  218. pbImage.Click += new EventHandler(FormHOZMain_Click);
  219. //加载窗口移动事件
  220. this.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
  221. //新建
  222. NewCreate();
  223. //实例初始化窗体对象
  224. if (uControl_Init == null)
  225. {
  226. uControl_Init = new UControl_Init(this);
  227. uControl_Init.ReloadConfig();
  228. }
  229. }
  230. #endregion
  231. #region 创建切孔列表信息
  232. /// <summary>
  233. /// 创建切孔列表信息
  234. /// </summary>
  235. /// <param name="ListCutHole"></param>
  236. public void CreateCutHoleList(List<CutHole> ListCutHole)
  237. {
  238. //清空左侧Panel中的切孔控件
  239. ClearPanelControls();
  240. for (int i = ListCutHole.Count - 1; i >= 0; i--)
  241. {
  242. UControl_CutHole ucCutHole = new UControl_CutHole(this);
  243. ucCutHole.Dock = DockStyle.Top;
  244. ucCutHole.CutHoleName = ListCutHole[i].HoleName;
  245. //显示切孔参数信息
  246. if (ucCutHole.UControl_ParaInfo == null)
  247. {
  248. ucCutHole.UControl_ParaInfo = new UControl_ParaInfo(this);
  249. }
  250. plPrarInfo.Width = ucCutHole.UControl_ParaInfo.Width;
  251. plPrarInfo.Height = ucCutHole.UControl_ParaInfo.Height;
  252. //设置当前样品的参数信息
  253. string CutHoleName = ListCutHole[i].HoleName;
  254. ucCutHole.UControl_ParaInfo.Name = CutHoleName;
  255. ucCutHole.UControl_ParaInfo.CutHoleName = CutHoleName;
  256. ucCutHole.UControl_ParaInfo.Position = ListCutHole[i].Position;
  257. ucCutHole.UControl_ParaInfo.StartTime = ListCutHole[i].START.ToString();
  258. ucCutHole.UControl_ParaInfo.EndTime = ListCutHole[i].END.ToString();
  259. int state = (int)ListCutHole[i].STATE;
  260. ucCutHole.UControl_ParaInfo.State = state;
  261. ucCutHole.UControl_ParaInfo.IsSwitch = ListCutHole[i].SWITCH;
  262. ucCutHole.UControl_ParaInfo.Dock = DockStyle.Fill;
  263. ucCutHole.UControl_ParaInfo.ShowParaInfo();
  264. plPrarInfo.Controls.Add(ucCutHole.UControl_ParaInfo);
  265. plLeftContent.Controls.Add(ucCutHole);
  266. ChangeCutHoleState(CutHoleName, state);
  267. }
  268. plPrarInfo.Visible = false;
  269. }
  270. #endregion
  271. #region 隐藏处理层
  272. //隐藏处理层
  273. private void FormHOZMain_Click(object sender, EventArgs e)
  274. {
  275. if (plPrarInfo.Visible)
  276. {
  277. plPrarInfo.Visible = false;
  278. }
  279. }
  280. #endregion
  281. #region 设置控件在线程过程中的编辑状态
  282. /// <summary>
  283. /// 设置控件在线程过程中的编辑状态
  284. /// </summary>
  285. /// <param name="cState"></param>
  286. public void SetWinControlMeasureState(bool cState)
  287. {
  288. pbNew.Enabled = cState;
  289. pbOpen.Enabled = cState;
  290. pbSave.Enabled = cState;
  291. pbInit.Enabled = cState;
  292. pbImportTemplateFile.Enabled = cState;
  293. //pbLog.Enabled = cState;
  294. pbStart.Enabled = cState;
  295. pbStop.Enabled = !cState;
  296. //设置切孔是否执行
  297. foreach (Control item in plPrarInfo.Controls)
  298. {
  299. if (item is UserControl)
  300. {
  301. UControl_ParaInfo ucParaInfo = (UControl_ParaInfo)item;
  302. ucParaInfo.CkIsSwitch.Enabled = cState;
  303. }
  304. }
  305. }
  306. #endregion
  307. #region 窗体中 线程开始 停止 最大化 最小化 关闭 按钮事件
  308. private void pbMin_MouseEnter(object sender, EventArgs e)
  309. {
  310. this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min;
  311. }
  312. private void pbMin_MouseLeave(object sender, EventArgs e)
  313. {
  314. this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min;
  315. }
  316. private void pbMax_MouseEnter(object sender, EventArgs e)
  317. {
  318. this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max;
  319. }
  320. private void pbMax_MouseLeave(object sender, EventArgs e)
  321. {
  322. this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max;
  323. }
  324. private void pbClose_MouseEnter(object sender, EventArgs e)
  325. {
  326. this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.Exit;
  327. }
  328. private void pbClose_MouseLeave(object sender, EventArgs e)
  329. {
  330. this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.Exit;
  331. }
  332. private void pbStart_MouseEnter(object sender, EventArgs e)
  333. {
  334. this.pbStart.BackgroundImage = global::HOZProject.Properties.Resources.StartLeave;
  335. }
  336. private void pbStart_MouseLeave(object sender, EventArgs e)
  337. {
  338. this.pbStart.BackgroundImage = global::HOZProject.Properties.Resources.StartLeave;
  339. }
  340. private void pbStop_MouseEnter(object sender, EventArgs e)
  341. {
  342. this.pbStop.BackgroundImage = global::HOZProject.Properties.Resources.StopLeave;
  343. }
  344. private void pbStop_MouseLeave(object sender, EventArgs e)
  345. {
  346. this.pbStop.BackgroundImage = global::HOZProject.Properties.Resources.StopLeave;
  347. }
  348. private void pbClose_Click(object sender, EventArgs e)
  349. {
  350. try
  351. {
  352. if (m_Ms != null)
  353. {
  354. if (m_BackgroundWorker.IsBusy)
  355. {
  356. if (MessageBox.Show("当前测量正在运行,是否关闭此窗体?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  357. {
  358. //线程停止变量
  359. m_Ms.key_stop = true;
  360. IsClose = true;
  361. lblStateMessage.Text = "正在关闭窗体...请等待";
  362. pbClose.Enabled = false;
  363. }
  364. return;
  365. }
  366. }
  367. if (MessageBox.Show("是否关闭此窗体?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  368. {
  369. m_MeasureFile.Save();
  370. this.Close();
  371. }
  372. }
  373. catch (Exception ex)
  374. {
  375. LogManager.LogError(ex.Message);
  376. }
  377. }
  378. private void pbMin_Click(object sender, EventArgs e)
  379. {
  380. this.WindowState = FormWindowState.Minimized;
  381. }
  382. private void pbMax_Click(object sender, EventArgs e)
  383. {
  384. if (this.WindowState == FormWindowState.Maximized)
  385. {
  386. this.WindowState = FormWindowState.Normal;
  387. }
  388. else
  389. {
  390. this.WindowState = FormWindowState.Maximized;
  391. }
  392. //窗体据中
  393. this.StartPosition = FormStartPosition.CenterScreen;
  394. }
  395. #endregion
  396. #region 新建、保存、打开、初始化、导入配置、查看日志
  397. private void pbNew_Click(object sender, EventArgs e)
  398. {
  399. NewCreate();
  400. }
  401. private void NewCreate()
  402. {
  403. m_MeasureFile = new MeasureFile();
  404. if (!m_MeasureFile.New())
  405. {
  406. return;
  407. }
  408. else
  409. {
  410. //清空内容容器中的控件
  411. ClearPanelControls();
  412. }
  413. //实例初始化窗体对象
  414. if (uControl_Init == null)
  415. {
  416. uControl_Init = new UControl_Init(this);
  417. uControl_Init.ReloadConfig();
  418. }
  419. m_MeasureFile.MParam = uControl_Init.GetMeasureParam();
  420. }
  421. private void pbSave_Click(object sender, EventArgs e)
  422. {
  423. if (m_MeasureFile == null)
  424. {
  425. MessageBox.Show("请新建一个测量文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  426. }
  427. else
  428. {
  429. //保存测量文件
  430. if (m_MeasureFile.SaveAs())
  431. {
  432. //获取测量文件所在路径
  433. string savePath = m_MeasureFile.FilePath;
  434. //数据库名称
  435. string dbFileName = "MeasureFile.db";
  436. //要复制的文件路径
  437. string pLocalFilePath = Application.StartupPath +"\\"+dbFileName;
  438. string pSaveFilePath = savePath + "\\" + dbFileName;//指定存储的路径
  439. if (File.Exists(pLocalFilePath))//必须判断要复制的文件是否存在
  440. {
  441. //三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
  442. File.Copy(pLocalFilePath, pSaveFilePath, true);
  443. }
  444. //将初始化中的参数,配置到测量参数中
  445. m_MeasureFile.MParam = uControl_Init.GetMeasureParamInfo();
  446. //设置已保存状态
  447. IsSave = true;
  448. }
  449. }
  450. }
  451. private void pbOpen_Click(object sender, EventArgs e)
  452. {
  453. try
  454. {
  455. //打开默认路径
  456. OpenFileDialog openFileDialog = new OpenFileDialog();
  457. //设置筛选文件格式
  458. openFileDialog.Filter = "测量文件(*.msf)|*.msf";
  459. if (openFileDialog.ShowDialog() == DialogResult.OK)
  460. {
  461. //读取文件
  462. string measureFileNamePath = openFileDialog.FileName;
  463. m_MeasureFile.FileName = measureFileNamePath;
  464. m_MeasureFile.Open();
  465. List<CutHole> ListCutHole = m_MeasureFile.ListCutHole;
  466. //文件路径
  467. string CutHoleFilePath = m_MeasureFile.CutHoleFilePath;
  468. CreateCutHoleList(ListCutHole);
  469. ////保存测量文件
  470. //m_MeasureFile.Save();
  471. //设置已保存状态
  472. IsSave = true;
  473. }
  474. }
  475. catch (Exception ex)
  476. {
  477. LogManager.LogError(ex.Message);
  478. }
  479. }
  480. private void pbInit_Click(object sender, EventArgs e)
  481. {
  482. if (uControl_Init == null)
  483. {
  484. uControl_Init = new UControl_Init(this);
  485. uControl_Init.ReloadConfig();
  486. }
  487. if (m_FormInit == null)
  488. {
  489. m_FormInit = new FormMove();
  490. }
  491. m_FormInit.StartPosition = FormStartPosition.CenterScreen;
  492. m_FormInit.FormBorderStyle = FormBorderStyle.None;
  493. m_FormInit.Width = uControl_Init.Width + 20;
  494. m_FormInit.Height = uControl_Init.Height + 20;
  495. if (m_FormInit.Controls.Count == 0)
  496. {
  497. uControl_Init.Location = new Point(10,10);
  498. m_FormInit.Controls.Add(uControl_Init);
  499. }
  500. m_FormInit.ShowDialog();
  501. }
  502. private void pbImportTemplateFile_Click(object sender, EventArgs e)
  503. {
  504. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  505. string m_TemplateFilePath = config.AppSettings.Settings["TemplateFilePath"].Value.ToString();
  506. //判断保存的路径是否存在
  507. if (!Directory.Exists(m_TemplateFilePath))
  508. {
  509. ////创建路径
  510. //Directory.CreateDirectory(m_TemplateFilePath);
  511. FolderBrowserDialog dialog = new FolderBrowserDialog();
  512. dialog.Description = "请选择文件路径";
  513. //dialog.RootFolder = Environment.SpecialFolder.Programs;
  514. if (dialog.ShowDialog() == DialogResult.OK)
  515. {
  516. m_TemplateFilePath = dialog.SelectedPath;
  517. config.AppSettings.Settings["TemplateFilePath"].Value = m_TemplateFilePath;
  518. config.Save(ConfigurationSaveMode.Modified);
  519. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  520. }
  521. }
  522. //打开默认路径
  523. OpenFileDialog openFileDialog = new OpenFileDialog();
  524. //设置默认打开路径(绝对路径)
  525. openFileDialog.InitialDirectory = m_TemplateFilePath;
  526. openFileDialog.Filter = "样品参数文件|*.cfg";
  527. if (openFileDialog.ShowDialog() == DialogResult.OK)
  528. {
  529. if (uControl_Init == null)
  530. {
  531. uControl_Init = new UControl_Init(this);
  532. }
  533. //设置选择的模板文件
  534. uControl_Init.ReadConfigPath = openFileDialog.FileName;
  535. //获取文件
  536. uControl_Init.ReadConfigInfo();
  537. }
  538. }
  539. private void pbLog_Click(object sender, EventArgs e)
  540. {
  541. m_FormLog = new Form();
  542. m_FormLog.StartPosition = FormStartPosition.CenterScreen;
  543. if (ucLog == null)
  544. {
  545. ucLog = new UControl_Log();
  546. }
  547. m_FormLog.FormBorderStyle = FormBorderStyle.None;
  548. m_FormLog.Width = ucLog.Width;
  549. m_FormLog.Height = ucLog.Height;
  550. ucLog.Name = "UControl_Log";
  551. //获取日志信息
  552. if (listmsg.Items.Count>0)
  553. {
  554. string[] strLog = new string[listmsg.Items.Count];
  555. for (int i = 0; i < listmsg.Items.Count; i++)
  556. {
  557. //赋值给数组
  558. strLog[i] = listmsg.Items[i].ToString();
  559. }
  560. ucLog.ShowProcessLogInfo(strLog);
  561. }
  562. m_FormLog.Controls.Add(ucLog);
  563. m_FormLog.ShowDialog();
  564. }
  565. //显示日志
  566. private void AddLogListInfo(ThreadStatusEventArgs e)
  567. {
  568. if (m_FormLog != null)
  569. {
  570. foreach (Control item in m_FormLog.Controls)
  571. {
  572. if (item is UserControl)
  573. {
  574. if (item.Name == "UControl_Log")
  575. {
  576. ucLog = (UControl_Log)item;
  577. //获取日志信息
  578. if (listmsg.Items.Count > 0)
  579. {
  580. string[] strLog = new string[listmsg.Items.Count];
  581. for (int i = 0; i < listmsg.Items.Count; i++)
  582. {
  583. //赋值给数组
  584. strLog[i] = listmsg.Items[i].ToString();
  585. }
  586. ucLog.ShowProcessLogInfo(strLog);
  587. }
  588. }
  589. }
  590. }
  591. }
  592. }
  593. #endregion
  594. #region 修改切孔状态
  595. /// <summary>
  596. /// 修改切孔状态
  597. /// </summary>
  598. /// <param name="cutHoleCode"></param>
  599. /// <param name="States"></param>
  600. public void ChangeCutHoleState(string CutHoleName, int States)
  601. {
  602. foreach (Control item in plLeftContent.Controls)
  603. {
  604. if (item is UserControl)
  605. {
  606. UControl_CutHole cutHole = (UControl_CutHole)item;
  607. if (cutHole.CutHoleName == CutHoleName)
  608. {
  609. Button btnCutHole = (Button)cutHole.Controls.Find("btnCutHole", false)[0];
  610. switch (States)
  611. {
  612. //运行中
  613. case (int)ThreadState.InProcess:
  614. //btnCutHole.BackColor = Color.Yellow;
  615. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Yellow;
  616. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  617. ChangeCutHoleMeasureState(CutHoleName, States);
  618. break;
  619. //等待
  620. case (int)ThreadState.Waiting:
  621. //btnCutHole.BackColor = Color.Yellow;
  622. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Yellow;
  623. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  624. ChangeCutHoleMeasureState(CutHoleName, States);
  625. break;
  626. //准备
  627. case (int)ThreadState.Ready:
  628. //btnCutHole.BackColor = Color.White;
  629. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Gray;
  630. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  631. ChangeCutHoleMeasureState(CutHoleName, States);
  632. break;
  633. //失败
  634. case (int)ThreadState.Failed:
  635. //btnCutHole.BackColor = Color.Red;
  636. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Red;
  637. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  638. ChangeCutHoleMeasureState(CutHoleName, States);
  639. break;
  640. //完成
  641. case (int)ThreadState.Success:
  642. //btnCutHole.BackColor = Color.GreenYellow;
  643. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Green;
  644. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  645. //修改切孔状态
  646. ChangeCutHoleMeasureState(CutHoleName, States);
  647. break;
  648. }
  649. break;
  650. }
  651. }
  652. }
  653. }
  654. #endregion
  655. #region 修改切孔状态
  656. public void ChangeCutHoleMeasureState(string CutHoleName, int States)
  657. {
  658. if (plPrarInfo.Controls.Count > 0)
  659. {
  660. foreach (Control item in plPrarInfo.Controls)
  661. {
  662. if (item is UserControl)
  663. {
  664. if (item.Name == CutHoleName)
  665. {
  666. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  667. //获取切孔列表
  668. List<CutHole> aCutHole = m_MeasureFile.ListCutHole;
  669. foreach (CutHole cutHoleItem in aCutHole)
  670. {
  671. if (cutHoleItem.HoleName == CutHoleName)
  672. {
  673. //设置开始时间与结束时间
  674. uControl_ParaInfo.StartTime = cutHoleItem.START.ToString();
  675. uControl_ParaInfo.EndTime = cutHoleItem.END.ToString();
  676. uControl_ParaInfo.ShowTime();
  677. break;
  678. }
  679. }
  680. switch (States)
  681. {
  682. //准备
  683. case (int)ThreadState.Ready:
  684. //修改切孔状态
  685. uControl_ParaInfo.lblShowState.Text = "准备";
  686. break;
  687. //等待
  688. case (int)ThreadState.Waiting:
  689. //修改切孔状态
  690. uControl_ParaInfo.lblShowState.Text = "等待";
  691. break;
  692. //进行中
  693. case (int)ThreadState.InProcess:
  694. //修改切孔状态
  695. uControl_ParaInfo.lblShowState.Text = "进行中";
  696. break;
  697. //完成
  698. case (int)ThreadState.Success:
  699. //修改切孔状态
  700. uControl_ParaInfo.lblShowState.Text = "完成";
  701. break;
  702. //失败
  703. case (int)ThreadState.Failed:
  704. uControl_ParaInfo.lblShowState.Text = "失败";
  705. uControl_ParaInfo.pbMeasure.Value = 100;
  706. uControl_ParaInfo.lblCompletedAmount.Text = "100%";
  707. break;
  708. }
  709. break;
  710. }
  711. }
  712. }
  713. }
  714. }
  715. #endregion
  716. #region 拖动无窗体的控件
  717. [DllImport("user32.dll")]//拖动无窗体的控件
  718. public static extern bool ReleaseCapture();
  719. [DllImport("user32.dll")]
  720. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  721. public const int WM_SYSCOMMAND = 0x0112;
  722. public const int SC_MOVE = 0xF010;
  723. public const int HTCAPTION = 0x0002;
  724. private void FormHOZMain_MouseDown(object sender, MouseEventArgs e)
  725. {
  726. if (this.WindowState == FormWindowState.Normal)
  727. {
  728. //拖动窗体
  729. ReleaseCapture();
  730. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  731. }
  732. }
  733. #endregion
  734. #region 开始、结束线程事件
  735. private void pbStart_Click(object sender, EventArgs e)
  736. {
  737. if (plLeftContent.Controls.Count==0)
  738. {
  739. MessageBox.Show("请添加切孔信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  740. return;
  741. }
  742. if (IsSave)
  743. {
  744. if (m_BackgroundWorker.IsBusy)
  745. {
  746. MessageBox.Show("线程已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  747. return;
  748. }
  749. if (MessageBox.Show("是否开始测量!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
  750. {
  751. m_BackgroundWorker.RunWorkerAsync(this);
  752. }
  753. }
  754. else
  755. {
  756. MessageBox.Show("请保存当前测量文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  757. return;
  758. }
  759. }
  760. private void pbStop_Click(object sender, EventArgs e)
  761. {
  762. if (m_Ms != null)
  763. {
  764. if (MessageBox.Show("是否停止当前测量?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
  765. {
  766. //线程停止变量
  767. m_Ms.key_stop = true;
  768. }
  769. }
  770. }
  771. #endregion
  772. #region 清空内容容器中的控件
  773. /// <summary>
  774. /// 清空内容容器中的控件
  775. /// </summary>
  776. private void ClearPanelControls()
  777. {
  778. //清空内容容器中的控件
  779. plLeftContent.Controls.Clear();
  780. plPrarInfo.Controls.Clear();
  781. }
  782. /// <summary>
  783. /// 清空日志控件内容
  784. /// </summary>
  785. private void ClearContrlsContent()
  786. {
  787. try
  788. {
  789. if (ucLog != null)
  790. {
  791. if (ucLog.dgvLog != null)
  792. {
  793. if (ucLog.dgvLog.Rows.Count > 0)
  794. {
  795. ucLog.dgvLog.Rows.Clear();
  796. }
  797. }
  798. }
  799. if (listmsg != null)
  800. {
  801. if (listmsg.Items.Count > 0)
  802. {
  803. listmsg.Items.Clear();
  804. }
  805. }
  806. }
  807. catch (Exception ex)
  808. {
  809. LogManager.LogError(ex.Message);
  810. }
  811. }
  812. #endregion
  813. #region 测试修改切孔中流程状态
  814. private void button1_Click(object sender, EventArgs e)
  815. {
  816. if (plPrarInfo.Controls.Count > 0)
  817. {
  818. string name = textBox1.Text;
  819. string code = textBox2.Text;
  820. bool state = Convert.ToBoolean(comboBox1.Text);
  821. foreach (Control item in plPrarInfo.Controls)
  822. {
  823. if (item is UserControl)
  824. {
  825. if (item.Name == name)
  826. {
  827. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  828. TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
  829. foreach (TimeLineItem tlItem in ParaItem)
  830. {
  831. if (tlItem.Code == code)
  832. {
  833. tlItem.State = Convert.ToInt32(state);
  834. break;
  835. }
  836. }
  837. uControl_ParaInfo.TimeLineInvalidate();
  838. break;
  839. }
  840. }
  841. }
  842. }
  843. }
  844. #endregion
  845. private void FormHOZMain_Resize(object sender, EventArgs e)
  846. {
  847. plMain.Left = 12;
  848. plMain.Top = 12;
  849. plMain.Width = this.Width - 24;
  850. plMain.Height = this.Height - 24;
  851. plFill.Width = plMain.Width - plLeft.Width - 4;
  852. plFill.Height = plMain.Height - plTop.Height - 4;
  853. plFill.Left = plLeft.Width + 4;
  854. plFill.Top = plTop.Height + 4;
  855. }
  856. }
  857. }