FormHOZMain.cs 38 KB

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