FormHOZMain.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. 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 FormMove();
  485. }
  486. m_FormInit.StartPosition = FormStartPosition.CenterScreen;
  487. m_FormInit.FormBorderStyle = FormBorderStyle.None;
  488. m_FormInit.Width = uControl_Init.Width+ 20;
  489. m_FormInit.Height = uControl_Init.Height + 20;
  490. if (m_FormInit.Controls.Count == 0)
  491. {
  492. uControl_Init.Location = new Point(10,10);
  493. m_FormInit.Controls.Add(uControl_Init);
  494. }
  495. m_FormInit.ShowDialog();
  496. }
  497. private void pbImportTemplateFile_Click(object sender, EventArgs e)
  498. {
  499. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  500. string m_TemplateFilePath = config.AppSettings.Settings["TemplateFilePath"].Value.ToString();
  501. //判断保存的路径是否存在
  502. if (!Directory.Exists(m_TemplateFilePath))
  503. {
  504. ////创建路径
  505. //Directory.CreateDirectory(m_TemplateFilePath);
  506. FolderBrowserDialog dialog = new FolderBrowserDialog();
  507. dialog.Description = "请选择文件路径";
  508. //dialog.RootFolder = Environment.SpecialFolder.Programs;
  509. if (dialog.ShowDialog() == DialogResult.OK)
  510. {
  511. m_TemplateFilePath = dialog.SelectedPath;
  512. config.AppSettings.Settings["TemplateFilePath"].Value = m_TemplateFilePath;
  513. config.Save(ConfigurationSaveMode.Modified);
  514. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  515. }
  516. }
  517. //打开默认路径
  518. OpenFileDialog openFileDialog = new OpenFileDialog();
  519. //设置默认打开路径(绝对路径)
  520. openFileDialog.InitialDirectory = m_TemplateFilePath;
  521. openFileDialog.Filter = "样品参数文件|*.cfg";
  522. if (openFileDialog.ShowDialog() == DialogResult.OK)
  523. {
  524. if (uControl_Init == null)
  525. {
  526. uControl_Init = new UControl_Init(this);
  527. }
  528. //设置选择的模板文件
  529. uControl_Init.ReadConfigPath = openFileDialog.FileName;
  530. //获取文件
  531. uControl_Init.ReadConfigInfo();
  532. }
  533. }
  534. private void pbLog_Click(object sender, EventArgs e)
  535. {
  536. m_FormLog = new Form();
  537. m_FormLog.StartPosition = FormStartPosition.CenterScreen;
  538. if (ucLog == null)
  539. {
  540. ucLog = new UControl_Log();
  541. }
  542. m_FormLog.FormBorderStyle = FormBorderStyle.None;
  543. m_FormLog.Width = ucLog.Width;
  544. m_FormLog.Height = ucLog.Height;
  545. ucLog.Name = "UControl_Log";
  546. //获取日志信息
  547. if (listmsg.Items.Count>0)
  548. {
  549. string[] strLog = new string[listmsg.Items.Count];
  550. for (int i = 0; i < listmsg.Items.Count; i++)
  551. {
  552. //赋值给数组
  553. strLog[i] = listmsg.Items[i].ToString();
  554. }
  555. ucLog.ShowProcessLogInfo(strLog);
  556. }
  557. m_FormLog.Controls.Add(ucLog);
  558. m_FormLog.ShowDialog();
  559. }
  560. //显示日志
  561. private void AddLogListInfo(ThreadStatusEventArgs e)
  562. {
  563. if (m_FormLog != null)
  564. {
  565. foreach (Control item in m_FormLog.Controls)
  566. {
  567. if (item is UserControl)
  568. {
  569. if (item.Name == "UControl_Log")
  570. {
  571. ucLog = (UControl_Log)item;
  572. //获取日志信息
  573. if (listmsg.Items.Count > 0)
  574. {
  575. string[] strLog = new string[listmsg.Items.Count];
  576. for (int i = 0; i < listmsg.Items.Count; i++)
  577. {
  578. //赋值给数组
  579. strLog[i] = listmsg.Items[i].ToString();
  580. }
  581. ucLog.ShowProcessLogInfo(strLog);
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. #endregion
  589. #region 修改切孔状态
  590. /// <summary>
  591. /// 修改切孔状态
  592. /// </summary>
  593. /// <param name="cutHoleCode"></param>
  594. /// <param name="States"></param>
  595. public void ChangeCutHoleState(string CutHoleName, int States)
  596. {
  597. foreach (Control item in plLeftContent.Controls)
  598. {
  599. if (item is UserControl)
  600. {
  601. UControl_CutHole cutHole = (UControl_CutHole)item;
  602. if (cutHole.CutHoleName == CutHoleName)
  603. {
  604. Button btnCutHole = (Button)cutHole.Controls.Find("btnCutHole", false)[0];
  605. switch (States)
  606. {
  607. //运行中
  608. case (int)ThreadState.InProcess:
  609. //btnCutHole.BackColor = Color.Yellow;
  610. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Yellow;
  611. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  612. ChangeCutHoleMeasureState(CutHoleName, States);
  613. break;
  614. //等待
  615. case (int)ThreadState.Waiting:
  616. //btnCutHole.BackColor = Color.Yellow;
  617. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Yellow;
  618. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  619. ChangeCutHoleMeasureState(CutHoleName, States);
  620. break;
  621. //准备
  622. case (int)ThreadState.Ready:
  623. //btnCutHole.BackColor = Color.White;
  624. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Gray;
  625. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  626. ChangeCutHoleMeasureState(CutHoleName, States);
  627. break;
  628. //失败
  629. case (int)ThreadState.Failed:
  630. //btnCutHole.BackColor = Color.Red;
  631. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Red;
  632. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  633. ChangeCutHoleMeasureState(CutHoleName, States);
  634. break;
  635. //完成
  636. case (int)ThreadState.Success:
  637. //btnCutHole.BackColor = Color.GreenYellow;
  638. btnCutHole.BackgroundImage = Properties.Resources.CutHole_Green;
  639. btnCutHole.BackgroundImageLayout = ImageLayout.Center;
  640. //修改切孔状态
  641. ChangeCutHoleMeasureState(CutHoleName, States);
  642. break;
  643. }
  644. break;
  645. }
  646. }
  647. }
  648. }
  649. #endregion
  650. #region 修改切孔状态
  651. public void ChangeCutHoleMeasureState(string CutHoleName, int States)
  652. {
  653. if (plPrarInfo.Controls.Count > 0)
  654. {
  655. foreach (Control item in plPrarInfo.Controls)
  656. {
  657. if (item is UserControl)
  658. {
  659. if (item.Name == CutHoleName)
  660. {
  661. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  662. //获取切孔列表
  663. List<CutHole> aCutHole = m_MeasureFile.ListCutHole;
  664. foreach (CutHole cutHoleItem in aCutHole)
  665. {
  666. if (cutHoleItem.HoleName == CutHoleName)
  667. {
  668. //设置开始时间与结束时间
  669. uControl_ParaInfo.StartTime = cutHoleItem.START.ToString();
  670. uControl_ParaInfo.EndTime = cutHoleItem.END.ToString();
  671. uControl_ParaInfo.ShowTime();
  672. break;
  673. }
  674. }
  675. switch (States)
  676. {
  677. //准备
  678. case (int)ThreadState.Ready:
  679. //修改切孔状态
  680. uControl_ParaInfo.lblShowState.Text = "准备";
  681. break;
  682. //等待
  683. case (int)ThreadState.Waiting:
  684. //修改切孔状态
  685. uControl_ParaInfo.lblShowState.Text = "等待";
  686. break;
  687. //进行中
  688. case (int)ThreadState.InProcess:
  689. //修改切孔状态
  690. uControl_ParaInfo.lblShowState.Text = "进行中";
  691. break;
  692. //完成
  693. case (int)ThreadState.Success:
  694. //修改切孔状态
  695. uControl_ParaInfo.lblShowState.Text = "完成";
  696. break;
  697. //失败
  698. case (int)ThreadState.Failed:
  699. uControl_ParaInfo.lblShowState.Text = "失败";
  700. uControl_ParaInfo.pbMeasure.Value = 100;
  701. uControl_ParaInfo.lblCompletedAmount.Text = "100%";
  702. break;
  703. }
  704. break;
  705. }
  706. }
  707. }
  708. }
  709. }
  710. #endregion
  711. #region 拖动无窗体的控件
  712. [DllImport("user32.dll")]//拖动无窗体的控件
  713. public static extern bool ReleaseCapture();
  714. [DllImport("user32.dll")]
  715. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  716. public const int WM_SYSCOMMAND = 0x0112;
  717. public const int SC_MOVE = 0xF010;
  718. public const int HTCAPTION = 0x0002;
  719. private void FormHOZMain_MouseDown(object sender, MouseEventArgs e)
  720. {
  721. if (this.WindowState == FormWindowState.Normal)
  722. {
  723. //拖动窗体
  724. ReleaseCapture();
  725. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  726. }
  727. }
  728. #endregion
  729. #region 开始、结束线程事件
  730. private void pbStart_Click(object sender, EventArgs e)
  731. {
  732. if (plLeftContent.Controls.Count==0)
  733. {
  734. MessageBox.Show("请添加切孔信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  735. return;
  736. }
  737. if (IsSave)
  738. {
  739. if (m_BackgroundWorker.IsBusy)
  740. {
  741. MessageBox.Show("线程已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  742. return;
  743. }
  744. if (MessageBox.Show("是否开始测量!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
  745. {
  746. m_BackgroundWorker.RunWorkerAsync(this);
  747. }
  748. }
  749. else
  750. {
  751. MessageBox.Show("请保存当前测量文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  752. return;
  753. }
  754. }
  755. private void pbStop_Click(object sender, EventArgs e)
  756. {
  757. if (m_Ms != null)
  758. {
  759. if (MessageBox.Show("是否停止当前测量?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
  760. {
  761. //线程停止变量
  762. m_Ms.key_stop = true;
  763. }
  764. }
  765. }
  766. #endregion
  767. #region 清空内容容器中的控件
  768. /// <summary>
  769. /// 清空内容容器中的控件
  770. /// </summary>
  771. private void ClearPanelControls()
  772. {
  773. //清空内容容器中的控件
  774. plLeftContent.Controls.Clear();
  775. plPrarInfo.Controls.Clear();
  776. }
  777. /// <summary>
  778. /// 清空日志控件内容
  779. /// </summary>
  780. private void ClearContrlsContent()
  781. {
  782. try
  783. {
  784. if (ucLog != null)
  785. {
  786. if (ucLog.dgvLog != null)
  787. {
  788. if (ucLog.dgvLog.Rows.Count > 0)
  789. {
  790. ucLog.dgvLog.Rows.Clear();
  791. }
  792. }
  793. }
  794. if (listmsg != null)
  795. {
  796. if (listmsg.Items.Count > 0)
  797. {
  798. listmsg.Items.Clear();
  799. }
  800. }
  801. }
  802. catch (Exception ex)
  803. {
  804. LogManager.LogError(ex.Message);
  805. }
  806. }
  807. #endregion
  808. #region 测试修改切孔中流程状态
  809. private void button1_Click(object sender, EventArgs e)
  810. {
  811. if (plPrarInfo.Controls.Count > 0)
  812. {
  813. string name = textBox1.Text;
  814. string code = textBox2.Text;
  815. bool state = Convert.ToBoolean(comboBox1.Text);
  816. foreach (Control item in plPrarInfo.Controls)
  817. {
  818. if (item is UserControl)
  819. {
  820. if (item.Name == name)
  821. {
  822. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  823. TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
  824. foreach (TimeLineItem tlItem in ParaItem)
  825. {
  826. if (tlItem.Code == code)
  827. {
  828. tlItem.State = Convert.ToInt32(state);
  829. break;
  830. }
  831. }
  832. uControl_ParaInfo.TimeLineInvalidate();
  833. break;
  834. }
  835. }
  836. }
  837. }
  838. }
  839. #endregion
  840. private void FormHOZMain_Resize(object sender, EventArgs e)
  841. {
  842. plMain.Left = 12;
  843. plMain.Top = 12;
  844. plMain.Width = this.Width - 24;
  845. plMain.Height = this.Height - 24;
  846. plFill.Width = plMain.Width - plLeft.Width - 4;
  847. plFill.Height = plMain.Height - plTop.Height - 4;
  848. plFill.Left = plLeft.Width + 4;
  849. plFill.Top = plTop.Height + 4;
  850. }
  851. }
  852. }