FormHOZMain.cs 35 KB

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