FormHOZMain.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. Form 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["Focus_UP"].ToString());
  131. m_MeasureFile.MParam.FIBFocus.Down = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Down"].ToString());
  132. m_MeasureFile.MParam.FIBFocus.Step = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Step"].ToString());
  133. m_MeasureFile.MParam.FIBFocus.Range = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Range"].ToString());
  134. m_MeasureFile.MParam.FIBFocus.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_FStep"].ToString());
  135. m_MeasureFile.MParam.AutoStigX.UP = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_UP"].ToString());
  136. m_MeasureFile.MParam.AutoStigX.Down = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Down"].ToString());
  137. m_MeasureFile.MParam.AutoStigX.Step = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Step"].ToString());
  138. m_MeasureFile.MParam.AutoStigX.Range = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Range"].ToString());
  139. m_MeasureFile.MParam.AutoStigX.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_FStep"].ToString());
  140. m_MeasureFile.MParam.AutoStigY.UP = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_UP"].ToString());
  141. m_MeasureFile.MParam.AutoStigY.Down = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Down"].ToString());
  142. m_MeasureFile.MParam.AutoStigY.Step = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Step"].ToString());
  143. m_MeasureFile.MParam.AutoStigY.Range = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Range"].ToString());
  144. m_MeasureFile.MParam.AutoStigY.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_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. m_Ms.InitMeas(m_MeasureFile);
  160. //注册事件
  161. m_Ms.SendThreadStatus += new ThreadStatusHandler(displayMessage);
  162. //注册事件
  163. m_Ms.SendCutHolesStatus += new CutHolesStatusHandler(displayCutHoleMessage);
  164. //设置控件操作
  165. SetWinControlMeasureState(false);
  166. //自动测量的全过程
  167. m_Ms.DoMeasure();
  168. }
  169. catch (Exception ex)
  170. {
  171. LogManager.LogError(ex.ToString());
  172. }
  173. }
  174. public void displayMessage(object sender, ThreadStatusEventArgs e)
  175. {
  176. //主界面显示内容
  177. this.BeginInvoke((Action)delegate
  178. {
  179. this.listmsg.Items.Add(e.HoleName +"_" + e.Time.ToString() + "_"+ e.Message + "_" + e.State );
  180. //显示流程中对应编号的内容
  181. MeasureMsgManage.ShowMsgContent(this, e);
  182. AddLogListInfo(e);
  183. });
  184. }
  185. public void displayCutHoleMessage(object sender, CutHolesStatusEventArgs e)
  186. {
  187. //主界面显示内容
  188. this.BeginInvoke((Action)delegate
  189. {
  190. //修改切孔状态
  191. ChangeCutHoleState(e.HoleName.ToString(), Convert.ToInt32(e.State));
  192. });
  193. }
  194. void UpdateProgress(object sender, ProgressChangedEventArgs e)
  195. {
  196. }
  197. void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
  198. {
  199. //设置控件操作
  200. MessageBox.Show("测量已完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  201. SetWinControlMeasureState(true);
  202. }
  203. #endregion
  204. #region 窗体加载
  205. private void FormHOZMain_Load(object sender, EventArgs e)
  206. {
  207. //加载控件的点击事件
  208. this.Click += new EventHandler(FormHOZMain_Click);
  209. plFill.Click += new EventHandler(FormHOZMain_Click);
  210. plTop.Click += new EventHandler(FormHOZMain_Click);
  211. plLeft.Click += new EventHandler(FormHOZMain_Click);
  212. plLeftContent.Click += new EventHandler(FormHOZMain_Click);
  213. pbImage.Click += new EventHandler(FormHOZMain_Click);
  214. //加载窗口移动事件
  215. this.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
  216. //新建
  217. NewCreate();
  218. //实例初始化窗体对象
  219. if (uControl_Init == null)
  220. {
  221. uControl_Init = new UControl_Init(this);
  222. uControl_Init.ReloadConfig();
  223. }
  224. }
  225. #endregion
  226. #region 创建切孔列表信息
  227. /// <summary>
  228. /// 创建切孔列表信息
  229. /// </summary>
  230. /// <param name="ListCutHole"></param>
  231. public void CreateCutHoleList(List<CutHole> ListCutHole)
  232. {
  233. //清空左侧Panel中的切孔控件
  234. ClearPanelControls();
  235. for (int i = ListCutHole.Count - 1; i >= 0; i--)
  236. {
  237. UControl_CutHole ucCutHole = new UControl_CutHole(this);
  238. ucCutHole.Dock = DockStyle.Top;
  239. ucCutHole.CutHoleName = ListCutHole[i].HoleName;
  240. //显示切孔参数信息
  241. if (ucCutHole.UControl_ParaInfo == null)
  242. {
  243. ucCutHole.UControl_ParaInfo = new UControl_ParaInfo(this);
  244. }
  245. plPrarInfo.Width = ucCutHole.UControl_ParaInfo.Width;
  246. plPrarInfo.Height = ucCutHole.UControl_ParaInfo.Height;
  247. //设置当前样品的参数信息
  248. string CutHoleName = ListCutHole[i].HoleName;
  249. ucCutHole.UControl_ParaInfo.Name = CutHoleName;
  250. ucCutHole.UControl_ParaInfo.CutHoleName = CutHoleName;
  251. ucCutHole.UControl_ParaInfo.Position = ListCutHole[i].Position;
  252. ucCutHole.UControl_ParaInfo.StartTime = ListCutHole[i].START.ToString();
  253. ucCutHole.UControl_ParaInfo.EndTime = ListCutHole[i].END.ToString();
  254. int state = (int)ListCutHole[i].STATE;
  255. ucCutHole.UControl_ParaInfo.State = state;
  256. ucCutHole.UControl_ParaInfo.IsSwitch = ListCutHole[i].SWITCH;
  257. ucCutHole.UControl_ParaInfo.Dock = DockStyle.Fill;
  258. ucCutHole.UControl_ParaInfo.ShowParaInfo();
  259. plPrarInfo.Controls.Add(ucCutHole.UControl_ParaInfo);
  260. plLeftContent.Controls.Add(ucCutHole);
  261. ChangeCutHoleState(CutHoleName, state);
  262. }
  263. plPrarInfo.Visible = false;
  264. }
  265. #endregion
  266. #region 隐藏处理层
  267. //隐藏处理层
  268. private void FormHOZMain_Click(object sender, EventArgs e)
  269. {
  270. if (plPrarInfo.Visible)
  271. {
  272. plPrarInfo.Visible = false;
  273. }
  274. }
  275. #endregion
  276. #region 设置控件在线程过程中的编辑状态
  277. /// <summary>
  278. /// 设置控件在线程过程中的编辑状态
  279. /// </summary>
  280. /// <param name="cState"></param>
  281. public void SetWinControlMeasureState(bool cState)
  282. {
  283. pbNew.Enabled = cState;
  284. pbOpen.Enabled = cState;
  285. pbSave.Enabled = cState;
  286. pbInit.Enabled = cState;
  287. pbImportTemplateFile.Enabled = cState;
  288. //pbLog.Enabled = cState;
  289. pbStart.Enabled = cState;
  290. pbStop.Enabled = !cState;
  291. //设置切孔是否执行
  292. foreach (Control item in plPrarInfo.Controls)
  293. {
  294. if (item is UserControl)
  295. {
  296. UControl_ParaInfo ucParaInfo = (UControl_ParaInfo)item;
  297. ucParaInfo.CkIsSwitch.Enabled = cState;
  298. }
  299. }
  300. }
  301. #endregion
  302. #region 窗体中 线程开始 停止 最大化 最小化 关闭 按钮事件
  303. private void pbMin_MouseEnter(object sender, EventArgs e)
  304. {
  305. this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min_Blue;
  306. }
  307. private void pbMin_MouseLeave(object sender, EventArgs e)
  308. {
  309. this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min_Gray;
  310. }
  311. private void pbMax_MouseEnter(object sender, EventArgs e)
  312. {
  313. this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max_Blue;
  314. }
  315. private void pbMax_MouseLeave(object sender, EventArgs e)
  316. {
  317. this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max_Gray;
  318. }
  319. private void pbClose_MouseEnter(object sender, EventArgs e)
  320. {
  321. this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_Red;
  322. }
  323. private void pbClose_MouseLeave(object sender, EventArgs e)
  324. {
  325. this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_Gray;
  326. }
  327. private void pbStart_MouseEnter(object sender, EventArgs e)
  328. {
  329. this.pbStart.BackgroundImage = global::HOZProject.Properties.Resources.Start_Green;
  330. }
  331. private void pbStart_MouseLeave(object sender, EventArgs e)
  332. {
  333. this.pbStart.BackgroundImage = global::HOZProject.Properties.Resources.Start;
  334. }
  335. private void pbStop_MouseEnter(object sender, EventArgs e)
  336. {
  337. this.pbStop.BackgroundImage = global::HOZProject.Properties.Resources.Stop_Red;
  338. }
  339. private void pbStop_MouseLeave(object sender, EventArgs e)
  340. {
  341. this.pbStop.BackgroundImage = global::HOZProject.Properties.Resources.Stop;
  342. }
  343. private void pbClose_Click(object sender, EventArgs e)
  344. {
  345. try
  346. {
  347. if (m_Ms != null)
  348. {
  349. if (m_BackgroundWorker.IsBusy)
  350. {
  351. if (MessageBox.Show("当前测量正在运行,是否关闭此窗体?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  352. {
  353. //线程停止变量
  354. m_Ms.key_stop = true;
  355. IsClose = true;
  356. lblStateMessage.Text = "正在关闭窗体...请等待";
  357. pbClose.Enabled = false;
  358. }
  359. return;
  360. }
  361. }
  362. if (MessageBox.Show("是否关闭此窗体?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  363. {
  364. m_MeasureFile.Save();
  365. this.Close();
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. LogManager.LogError(ex.Message);
  371. }
  372. }
  373. private void pbMin_Click(object sender, EventArgs e)
  374. {
  375. this.WindowState = FormWindowState.Minimized;
  376. }
  377. private void pbMax_Click(object sender, EventArgs e)
  378. {
  379. if (this.WindowState == FormWindowState.Maximized)
  380. {
  381. this.WindowState = FormWindowState.Normal;
  382. }
  383. else
  384. {
  385. this.WindowState = FormWindowState.Maximized;
  386. }
  387. //窗体据中
  388. this.StartPosition = FormStartPosition.CenterScreen;
  389. }
  390. #endregion
  391. #region 新建、保存、打开、初始化、导入配置、查看日志
  392. private void pbNew_Click(object sender, EventArgs e)
  393. {
  394. NewCreate();
  395. }
  396. private void NewCreate()
  397. {
  398. m_MeasureFile = new MeasureFile();
  399. if (!m_MeasureFile.New())
  400. {
  401. return;
  402. }
  403. else
  404. {
  405. //清空内容容器中的控件
  406. ClearPanelControls();
  407. }
  408. //实例初始化窗体对象
  409. if (uControl_Init == null)
  410. {
  411. uControl_Init = new UControl_Init(this);
  412. uControl_Init.ReloadConfig();
  413. }
  414. m_MeasureFile.MParam = uControl_Init.GetMeasureParam();
  415. }
  416. private void pbSave_Click(object sender, EventArgs e)
  417. {
  418. if (m_MeasureFile == null)
  419. {
  420. MessageBox.Show("请新建一个测量文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  421. }
  422. else
  423. {
  424. //保存测量文件
  425. if (m_MeasureFile.SaveAs())
  426. {
  427. //获取测量文件所在路径
  428. string savePath = m_MeasureFile.FilePath;
  429. //数据库名称
  430. string dbFileName = "MeasureFile.db";
  431. //要复制的文件路径
  432. string pLocalFilePath = Application.StartupPath +"\\"+dbFileName;
  433. string pSaveFilePath = savePath + "\\" + dbFileName;//指定存储的路径
  434. if (File.Exists(pLocalFilePath))//必须判断要复制的文件是否存在
  435. {
  436. //三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
  437. File.Copy(pLocalFilePath, pSaveFilePath, true);
  438. }
  439. //将初始化中的参数,配置到测量参数中
  440. m_MeasureFile.MParam = uControl_Init.GetMeasureParamInfo();
  441. //设置已保存状态
  442. IsSave = true;
  443. }
  444. }
  445. }
  446. private void pbOpen_Click(object sender, EventArgs e)
  447. {
  448. try
  449. {
  450. //打开默认路径
  451. OpenFileDialog openFileDialog = new OpenFileDialog();
  452. //设置筛选文件格式
  453. openFileDialog.Filter = "测量文件(*.msf)|*.msf";
  454. if (openFileDialog.ShowDialog() == DialogResult.OK)
  455. {
  456. //读取文件
  457. string measureFileNamePath = openFileDialog.FileName;
  458. m_MeasureFile.FileName = measureFileNamePath;
  459. m_MeasureFile.Open();
  460. List<CutHole> ListCutHole = m_MeasureFile.ListCutHole;
  461. //文件路径
  462. string CutHoleFilePath = m_MeasureFile.CutHoleFilePath;
  463. CreateCutHoleList(ListCutHole);
  464. ////保存测量文件
  465. //m_MeasureFile.Save();
  466. //设置已保存状态
  467. IsSave = true;
  468. }
  469. }
  470. catch (Exception ex)
  471. {
  472. LogManager.LogError(ex.Message);
  473. }
  474. }
  475. private void pbInit_Click(object sender, EventArgs e)
  476. {
  477. if (uControl_Init == null)
  478. {
  479. uControl_Init = new UControl_Init(this);
  480. uControl_Init.ReloadConfig();
  481. }
  482. if (m_FormInit == null)
  483. {
  484. m_FormInit = new Form();
  485. }
  486. m_FormInit.StartPosition = FormStartPosition.CenterScreen;
  487. m_FormInit.FormBorderStyle = FormBorderStyle.None;
  488. m_FormInit.Width = uControl_Init.Width;
  489. m_FormInit.Height = uControl_Init.Height;
  490. if (m_FormInit.Controls.Count == 0)
  491. {
  492. m_FormInit.Controls.Add(uControl_Init);
  493. }
  494. m_FormInit.ShowDialog();
  495. }
  496. private void pbImportTemplateFile_Click(object sender, EventArgs e)
  497. {
  498. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  499. string m_TemplateFilePath = config.AppSettings.Settings["TemplateFilePath"].Value.ToString();
  500. //判断保存的路径是否存在
  501. if (!Directory.Exists(m_TemplateFilePath))
  502. {
  503. ////创建路径
  504. //Directory.CreateDirectory(m_TemplateFilePath);
  505. FolderBrowserDialog dialog = new FolderBrowserDialog();
  506. dialog.Description = "请选择文件路径";
  507. //dialog.RootFolder = Environment.SpecialFolder.Programs;
  508. if (dialog.ShowDialog() == DialogResult.OK)
  509. {
  510. m_TemplateFilePath = dialog.SelectedPath;
  511. config.AppSettings.Settings["TemplateFilePath"].Value = m_TemplateFilePath;
  512. config.Save(ConfigurationSaveMode.Modified);
  513. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  514. }
  515. }
  516. //打开默认路径
  517. OpenFileDialog openFileDialog = new OpenFileDialog();
  518. //设置默认打开路径(绝对路径)
  519. openFileDialog.InitialDirectory = m_TemplateFilePath;
  520. openFileDialog.Filter = "样品参数文件|*.cfg";
  521. if (openFileDialog.ShowDialog() == DialogResult.OK)
  522. {
  523. if (uControl_Init == null)
  524. {
  525. uControl_Init = new UControl_Init(this);
  526. }
  527. //设置选择的模板文件
  528. uControl_Init.ReadConfigPath = openFileDialog.FileName;
  529. //获取文件
  530. uControl_Init.ReadConfigInfo();
  531. }
  532. }
  533. private void pbLog_Click(object sender, EventArgs e)
  534. {
  535. m_FormLog = new Form();
  536. m_FormLog.StartPosition = FormStartPosition.CenterScreen;
  537. if (ucLog == null)
  538. {
  539. ucLog = new UControl_Log();
  540. }
  541. m_FormLog.FormBorderStyle = FormBorderStyle.None;
  542. m_FormLog.Width = ucLog.Width;
  543. m_FormLog.Height = ucLog.Height;
  544. ucLog.Name = "UControl_Log";
  545. //获取日志信息
  546. if (listmsg.Items.Count>0)
  547. {
  548. string[] strLog = new string[listmsg.Items.Count];
  549. for (int i = 0; i < listmsg.Items.Count; i++)
  550. {
  551. //赋值给数组
  552. strLog[i] = listmsg.Items[i].ToString();
  553. }
  554. ucLog.ShowProcessLogInfo(strLog);
  555. }
  556. m_FormLog.Controls.Add(ucLog);
  557. m_FormLog.ShowDialog();
  558. }
  559. //显示日志
  560. private void AddLogListInfo(ThreadStatusEventArgs e)
  561. {
  562. if (m_FormLog != null)
  563. {
  564. foreach (Control item in m_FormLog.Controls)
  565. {
  566. if (item is UserControl)
  567. {
  568. if (item.Name == "UControl_Log")
  569. {
  570. ucLog = (UControl_Log)item;
  571. //获取日志信息
  572. if (listmsg.Items.Count > 0)
  573. {
  574. string[] strLog = new string[listmsg.Items.Count];
  575. for (int i = 0; i < listmsg.Items.Count; i++)
  576. {
  577. //赋值给数组
  578. strLog[i] = listmsg.Items[i].ToString();
  579. }
  580. ucLog.ShowProcessLogInfo(strLog);
  581. }
  582. }
  583. }
  584. }
  585. }
  586. }
  587. #endregion
  588. #region 修改切孔状态
  589. /// <summary>
  590. /// 修改切孔状态
  591. /// </summary>
  592. /// <param name="cutHoleCode"></param>
  593. /// <param name="States"></param>
  594. public void ChangeCutHoleState(string CutHoleName, int States)
  595. {
  596. foreach (Control item in plLeftContent.Controls)
  597. {
  598. if (item is UserControl)
  599. {
  600. UControl_CutHole cutHole = (UControl_CutHole)item;
  601. if (cutHole.CutHoleName == CutHoleName)
  602. {
  603. Button btnCutHole = (Button)cutHole.Controls.Find("btnCutHole", false)[0];
  604. switch (States)
  605. {
  606. //运行中
  607. case (int)ThreadState.InProcess:
  608. //btnCutHole.BackColor = Color.Yellow;
  609. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Yellow;
  610. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  611. ChangeCutHoleMeasureState(CutHoleName, States);
  612. break;
  613. //等待
  614. case (int)ThreadState.Waiting:
  615. //btnCutHole.BackColor = Color.Yellow;
  616. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Yellow;
  617. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  618. ChangeCutHoleMeasureState(CutHoleName, States);
  619. break;
  620. //准备
  621. case (int)ThreadState.Ready:
  622. //btnCutHole.BackColor = Color.White;
  623. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Gray;
  624. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  625. ChangeCutHoleMeasureState(CutHoleName, States);
  626. break;
  627. //失败
  628. case (int)ThreadState.Failed:
  629. //btnCutHole.BackColor = Color.Red;
  630. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Red;
  631. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  632. ChangeCutHoleMeasureState(CutHoleName, States);
  633. break;
  634. //完成
  635. case (int)ThreadState.Success:
  636. //btnCutHole.BackColor = Color.GreenYellow;
  637. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Green;
  638. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  639. //修改切孔状态
  640. ChangeCutHoleMeasureState(CutHoleName, States);
  641. break;
  642. }
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. #endregion
  649. #region 修改切孔状态
  650. public void ChangeCutHoleMeasureState(string CutHoleName, int States)
  651. {
  652. if (plPrarInfo.Controls.Count > 0)
  653. {
  654. foreach (Control item in plPrarInfo.Controls)
  655. {
  656. if (item is UserControl)
  657. {
  658. if (item.Name == CutHoleName)
  659. {
  660. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  661. //获取切孔列表
  662. List<CutHole> aCutHole = m_MeasureFile.ListCutHole;
  663. foreach (CutHole cutHoleItem in aCutHole)
  664. {
  665. if (cutHoleItem.HoleName == CutHoleName)
  666. {
  667. //设置开始时间与结束时间
  668. uControl_ParaInfo.StartTime = cutHoleItem.START.ToString();
  669. uControl_ParaInfo.EndTime = cutHoleItem.END.ToString();
  670. uControl_ParaInfo.ShowTime();
  671. break;
  672. }
  673. }
  674. switch (States)
  675. {
  676. //准备
  677. case (int)ThreadState.Ready:
  678. //修改切孔状态
  679. uControl_ParaInfo.lblShowState.Text = "准备";
  680. break;
  681. //等待
  682. case (int)ThreadState.Waiting:
  683. //修改切孔状态
  684. uControl_ParaInfo.lblShowState.Text = "等待";
  685. break;
  686. //进行中
  687. case (int)ThreadState.InProcess:
  688. //修改切孔状态
  689. uControl_ParaInfo.lblShowState.Text = "进行中";
  690. break;
  691. //完成
  692. case (int)ThreadState.Success:
  693. //修改切孔状态
  694. uControl_ParaInfo.lblShowState.Text = "完成";
  695. break;
  696. //失败
  697. case (int)ThreadState.Failed:
  698. uControl_ParaInfo.lblShowState.Text = "失败";
  699. uControl_ParaInfo.pbMeasure.Value = 100;
  700. uControl_ParaInfo.lblCompletedAmount.Text = "100%";
  701. break;
  702. }
  703. break;
  704. }
  705. }
  706. }
  707. }
  708. }
  709. #endregion
  710. #region 拖动无窗体的控件
  711. [DllImport("user32.dll")]//拖动无窗体的控件
  712. public static extern bool ReleaseCapture();
  713. [DllImport("user32.dll")]
  714. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  715. public const int WM_SYSCOMMAND = 0x0112;
  716. public const int SC_MOVE = 0xF010;
  717. public const int HTCAPTION = 0x0002;
  718. private void FormHOZMain_MouseDown(object sender, MouseEventArgs e)
  719. {
  720. if (this.WindowState == FormWindowState.Normal)
  721. {
  722. //拖动窗体
  723. ReleaseCapture();
  724. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  725. }
  726. }
  727. #endregion
  728. #region 开始、结束线程事件
  729. private void pbStart_Click(object sender, EventArgs e)
  730. {
  731. if (plLeftContent.Controls.Count==0)
  732. {
  733. MessageBox.Show("请添加切孔信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  734. return;
  735. }
  736. if (IsSave)
  737. {
  738. if (m_BackgroundWorker.IsBusy)
  739. {
  740. MessageBox.Show("线程已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  741. return;
  742. }
  743. if (MessageBox.Show("是否开始测量!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
  744. {
  745. m_BackgroundWorker.RunWorkerAsync(this);
  746. }
  747. }
  748. else
  749. {
  750. MessageBox.Show("请保存当前测量文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  751. return;
  752. }
  753. }
  754. private void pbStop_Click(object sender, EventArgs e)
  755. {
  756. if (m_Ms != null)
  757. {
  758. if (MessageBox.Show("是否停止当前测量?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
  759. {
  760. //线程停止变量
  761. m_Ms.key_stop = true;
  762. }
  763. }
  764. }
  765. #endregion
  766. #region 清空内容容器中的控件
  767. /// <summary>
  768. /// 清空内容容器中的控件
  769. /// </summary>
  770. private void ClearPanelControls()
  771. {
  772. //清空内容容器中的控件
  773. plLeftContent.Controls.Clear();
  774. plPrarInfo.Controls.Clear();
  775. }
  776. /// <summary>
  777. /// 清空日志控件内容
  778. /// </summary>
  779. private void ClearContrlsContent()
  780. {
  781. try
  782. {
  783. if (ucLog != null)
  784. {
  785. if (ucLog.dgvLog != null)
  786. {
  787. if (ucLog.dgvLog.Rows.Count > 0)
  788. {
  789. ucLog.dgvLog.Rows.Clear();
  790. }
  791. }
  792. }
  793. if (listmsg != null)
  794. {
  795. if (listmsg.Items.Count > 0)
  796. {
  797. listmsg.Items.Clear();
  798. }
  799. }
  800. }
  801. catch (Exception ex)
  802. {
  803. LogManager.LogError(ex.Message);
  804. }
  805. }
  806. #endregion
  807. #region 测试修改切孔中流程状态
  808. private void button1_Click(object sender, EventArgs e)
  809. {
  810. if (plPrarInfo.Controls.Count > 0)
  811. {
  812. string name = textBox1.Text;
  813. string code = textBox2.Text;
  814. bool state = Convert.ToBoolean(comboBox1.Text);
  815. foreach (Control item in plPrarInfo.Controls)
  816. {
  817. if (item is UserControl)
  818. {
  819. if (item.Name == name)
  820. {
  821. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  822. TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
  823. foreach (TimeLineItem tlItem in ParaItem)
  824. {
  825. if (tlItem.Code == code)
  826. {
  827. tlItem.State = Convert.ToInt32(state);
  828. break;
  829. }
  830. }
  831. uControl_ParaInfo.TimeLineInvalidate();
  832. break;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. #endregion
  839. private void FormHOZMain_Resize(object sender, EventArgs e)
  840. {
  841. plMain.Left = 12;
  842. plMain.Top = 12;
  843. plMain.Width = this.Width - 24;
  844. plMain.Height = this.Height - 24;
  845. plFill.Width = plMain.Width - plLeft.Width - 4;
  846. plFill.Height = plMain.Height - plTop.Height - 4;
  847. plFill.Left = plLeft.Width + 4;
  848. plFill.Top = plTop.Height + 4;
  849. }
  850. }
  851. }