Main.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using LabelManager2;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using UAS_MES.CustomControl.AccordionMenu;
  13. using UAS_MES.DataOperate;
  14. using UAS_MES.Entity;
  15. using UAS_MES.Properties;
  16. using UAS_MES.PublicMethod;
  17. namespace UAS_MES
  18. {
  19. public partial class Main : Form
  20. {
  21. //所有用到了headBar的部分都需要这段代码
  22. [DllImport("user32.dll")]
  23. public static extern bool ReleaseCapture();
  24. [DllImport("user32.dll")]
  25. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  26. [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
  27. public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
  28. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  29. public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
  30. public const int WM_SYSCOMMAND = 0x0112;
  31. public const int SC_MOVE = 0xF010;
  32. public const int HTCAPTION = 0x0002;
  33. string SerialPort1 = "Make!ColorBoxWeigh#Make!CartonBoxWeigh";
  34. Thread thread;
  35. Thread TestPrint;
  36. DataHelper dh = SystemInf.dh;
  37. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  38. string lblpath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo";
  39. public Main()
  40. {
  41. InitializeComponent();
  42. DoubleBuffered = true;
  43. SetStyle(ControlStyles.UserPaint, true);
  44. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  45. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  46. }
  47. /// <summary>
  48. /// 删除7天前的日志
  49. /// </summary>
  50. private void DeleteLog()
  51. {
  52. DirectoryInfo dir = new DirectoryInfo(sysdisc + @":\Log");
  53. List<string> DeleteFileName = new List<string>();
  54. //保存6天前和今天的操作日志
  55. for (int i = 1; i <= 30; i++)
  56. {
  57. DeleteFileName.Add(System.DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd") + ".txt");
  58. }
  59. DeleteFileName.Add(System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  60. if (dir.Exists)
  61. {
  62. FileInfo[] file = dir.GetFiles();
  63. foreach (FileInfo f in file)
  64. {
  65. if (!DeleteFileName.Contains(f.Name))
  66. f.Delete();
  67. }
  68. }
  69. }
  70. //tabPage标签图片尺寸
  71. const int CLOSE_SIZE = 16;
  72. //窗体加载的时候将headBar的事件委托给指定的函数
  73. private void Main_Load(object sender, EventArgs e)
  74. {
  75. Closelblprocess();
  76. thread = new Thread(DeleteLog);
  77. thread.Start();
  78. TestPrint = new Thread(TestPrintEnable);
  79. TestPrint.Start();
  80. this.Tag = "ShowDialogWindow";
  81. this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
  82. this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
  83. this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
  84. this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
  85. this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
  86. //设置底部栏目的基础信息
  87. Inf_name.Text = User.UserName;
  88. //inf_position.Text = User.UserPosition;
  89. Inf_linecode.Text = User.UserLineCode;
  90. Inf_source.Text = User.UserSourceCode;
  91. Inf_db.Text = SystemInf.CurrentDB;
  92. Inf_currentstep.Text = User.CurrentStepName;
  93. //设置获取当前屏幕大小自动全屏但是保留任务栏
  94. Rectangle ScreenArea = Screen.GetWorkingArea(this);
  95. Top = 0;
  96. Left = 0;
  97. Width = ScreenArea.Width;
  98. Height = ScreenArea.Height;
  99. SetForm();
  100. //设置导航栏宽度
  101. SystemInf.NavWidth = Menu.Width;
  102. SystemInf.HeadBarHeight = headBar1.Height;
  103. }
  104. //关闭打印进程
  105. private static void Closelblprocess()
  106. {
  107. try
  108. {
  109. //杀死全部未关闭的打印进程
  110. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  111. foreach (string line in lines)
  112. {
  113. if (line != "")
  114. {
  115. string processid = line.Split('|')[0];
  116. string lblid = line.Split('|')[1];
  117. try
  118. {
  119. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
  120. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  121. }
  122. catch (Exception)
  123. {
  124. try
  125. {
  126. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  127. }
  128. catch (Exception)
  129. {
  130. }
  131. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  132. StreamReader sr = new StreamReader(fs);
  133. String s = sr.ReadToEnd();
  134. sr.Close();
  135. fs.Close();
  136. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  137. StreamWriter sw = new StreamWriter(fas);
  138. sw.Write(s.Replace(line + "\r\n", ""));
  139. sw.Flush();
  140. sw.Close();
  141. fas.Close();
  142. }
  143. }
  144. }
  145. }
  146. catch
  147. {
  148. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  149. StreamWriter sw = new StreamWriter(fas);
  150. sw.Write("");
  151. sw.Flush();
  152. sw.Close();
  153. fas.Close();
  154. }
  155. }
  156. private void TestPrintEnable()
  157. {
  158. try
  159. {
  160. ApplicationClass lbl = new ApplicationClass();
  161. SystemInf.EnablePrint = true;
  162. lbl.Quit();
  163. }
  164. catch (Exception)
  165. {
  166. SystemInf.EnablePrint = false;
  167. }
  168. }
  169. /// <summary>
  170. ///设置窗体的最大化和最小化状态
  171. /// </summary>
  172. public void SetForm()
  173. {
  174. int WS_SYSMENU = 0x00080000; // 系统菜单
  175. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  176. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  177. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  178. }
  179. protected override CreateParams CreateParams
  180. {
  181. get
  182. {
  183. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  184. CreateParams cp = base.CreateParams;
  185. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  186. return cp;
  187. }
  188. }
  189. /// <summary>
  190. /// 重载创建文件句柄
  191. /// </summary>
  192. protected override void CreateHandle()
  193. {
  194. if (!IsHandleCreated)
  195. {
  196. try { base.CreateHandle(); }
  197. catch { }
  198. finally
  199. {
  200. if (!IsHandleCreated)
  201. base.RecreateHandle();
  202. }
  203. }
  204. }
  205. Bitmap image = Resources.close_button;
  206. //鼠标点下的时候监听
  207. private void headBar_MouseDown(object sender, MouseEventArgs e)
  208. {
  209. ReleaseCapture();
  210. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  211. }
  212. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  213. {
  214. GlobalEventsHandler.keycode = keyData;
  215. return false;
  216. }
  217. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  218. {
  219. try
  220. {
  221. Brush HeadTextColor = new SolidBrush(Color.Green);
  222. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  223. //先添加TabPage属性
  224. if (e.State == DrawItemState.Selected)
  225. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  226. else
  227. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);
  228. //再画一个矩形框
  229. using (Pen p = new Pen(SystemBrushes.Control))
  230. {
  231. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  232. myTabRect.Width = CLOSE_SIZE;
  233. myTabRect.Height = CLOSE_SIZE;
  234. e.Graphics.DrawRectangle(p, myTabRect);
  235. }
  236. //画关闭符号
  237. using (Pen objpen = new Pen(Color.Black))
  238. {
  239. //使用图片
  240. Bitmap bt = new Bitmap(image);
  241. Point p5 = new Point(myTabRect.X, 8);
  242. e.Graphics.DrawImage(bt, p5);
  243. }
  244. e.Graphics.Dispose();
  245. }
  246. catch (Exception) { }
  247. }
  248. //关闭按钮功能
  249. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  250. {
  251. if (e.Button == MouseButtons.Left)
  252. {
  253. int x = e.X, y = e.Y;
  254. //计算关闭区域
  255. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  256. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  257. myTabRect.Width = CLOSE_SIZE;
  258. myTabRect.Height = CLOSE_SIZE;
  259. //如果鼠标在区域内就关闭选项卡
  260. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  261. if (isClose == true)
  262. {
  263. DataTable dt;
  264. string RemindInf = "";
  265. string PaOutBoxCode = "";
  266. string ObCheckno = "";
  267. switch (MainTabControl.SelectedTab.Controls[0].Tag.ToString())
  268. {
  269. case "Make!PackageCollectionWeigh":
  270. Make.Make_PackageCollectionWeigh PackWeigh = (Make.Make_PackageCollectionWeigh)MainTabControl.SelectedTab.Controls[0];
  271. if (PackWeigh.ob_checkno.Text != "")
  272. {
  273. ObCheckno = PackWeigh.ob_checkno.Text;
  274. }
  275. if (PackWeigh.pa_outboxcode.Text != "")
  276. {
  277. PaOutBoxCode = PackWeigh.pa_outboxcode.Text;
  278. }
  279. break;
  280. case "Make!PackageCollection":
  281. Make.Make_PackageCollection Pack = (Make.Make_PackageCollection)MainTabControl.SelectedTab.Controls[0];
  282. if (Pack.ob_checkno.Text != "")
  283. {
  284. ObCheckno = Pack.ob_checkno.Text;
  285. }
  286. if (Pack.pa_outboxcode.Text != "")
  287. {
  288. PaOutBoxCode = Pack.pa_outboxcode.Text;
  289. }
  290. break;
  291. case "Make!ColorBoxWeigh":
  292. Make.Make_ColorBoxWeigh ColorWeigh = (Make.Make_ColorBoxWeigh)MainTabControl.SelectedTab.Controls[0];
  293. if (ColorWeigh.ob_checkno.Text != "")
  294. {
  295. ObCheckno = ColorWeigh.ob_checkno.Text;
  296. }
  297. break;
  298. case "Make!CartonBoxWeigh":
  299. Make.Make_CartonBoxWeigh CartonWeigh = (Make.Make_CartonBoxWeigh)MainTabControl.SelectedTab.Controls[0];
  300. if (CartonWeigh.ob_checkno.Text != "")
  301. {
  302. ObCheckno = CartonWeigh.ob_checkno.Text;
  303. }
  304. break;
  305. default:
  306. break;
  307. }
  308. if (ObCheckno != "")
  309. {
  310. dt = (DataTable)dh.ExecuteSql("select ob_status from oqcbatch where ob_checkno='" + ObCheckno + "'", "select");
  311. if (dt.Rows.Count > 0)
  312. if (dt.Rows[0][0].ToString() == "ENTERING")
  313. RemindInf = "当前批次未送检,是否关闭页面?";
  314. }
  315. if (PaOutBoxCode != "")
  316. {
  317. dt = (DataTable)dh.ExecuteSql("select pa_status from package where pa_outboxcode='" + PaOutBoxCode + "'", "select");
  318. if (dt.Rows.Count > 0)
  319. if (dt.Rows[0][0].ToString() != "1")
  320. RemindInf = "当前箱号未封箱,是否关闭页面?";
  321. }
  322. if (RemindInf != "")
  323. {
  324. string closetab_confirm = MessageBox.Show(this.ParentForm, RemindInf, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  325. if (closetab_confirm != "Yes")
  326. {
  327. return;
  328. }
  329. }
  330. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  331. }
  332. }
  333. }
  334. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  335. {
  336. //移除已打开的Form中的Form
  337. TabPage tb = e.Control as TabPage;
  338. AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
  339. (tb.Controls[0] as Form).Close();
  340. //掉用了串口的程序需要在这个步骤关闭串口
  341. try
  342. {
  343. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  344. {
  345. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  346. (tb.Controls[0] as Form).Close();
  347. }
  348. }
  349. catch (Exception) { }
  350. }
  351. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  352. {
  353. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  354. if (logout_confirm == "Yes")
  355. {
  356. //注销的时候切换回默认数据库
  357. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  358. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  359. //清除上个用户的权限信息
  360. SystemInf.Caller.Clear();
  361. //清除已经打开过的窗口的信息
  362. AccordionMenu.OpenedFormName.Clear();
  363. //注销的时候将除了登陆窗口之外的全部窗口关闭
  364. for (int i = 0; i < System.Windows.Forms.Application.OpenForms.Count; i++)
  365. {
  366. if (System.Windows.Forms.Application.OpenForms[i].Name != "Login")
  367. System.Windows.Forms.Application.OpenForms[i].Close();
  368. }
  369. this.Hide();
  370. Login login = new Login();
  371. login.ShowDialog();
  372. this.Close();
  373. }
  374. }
  375. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  376. {
  377. try
  378. {
  379. //杀死全部未关闭的打印进程
  380. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  381. foreach (string line in lines)
  382. {
  383. if (line != "")
  384. {
  385. string processid = line.Split('|')[0];
  386. string lblid = line.Split('|')[1];
  387. try
  388. {
  389. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
  390. {
  391. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  392. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  393. StreamReader sr = new StreamReader(fs);
  394. String s = sr.ReadToEnd();
  395. sr.Close();
  396. fs.Close();
  397. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  398. StreamWriter sw = new StreamWriter(fas);
  399. sw.Write(s.Replace(line + "\r\n", ""));
  400. sw.Flush();
  401. sw.Close();
  402. fas.Close();
  403. }
  404. }
  405. catch (Exception)
  406. {
  407. }
  408. }
  409. }
  410. }
  411. catch
  412. {
  413. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  414. StreamWriter sw = new StreamWriter(fas);
  415. sw.Write("");
  416. sw.Flush();
  417. sw.Close();
  418. fas.Close();
  419. }
  420. BaseUtil.FormStepInOrOut(this, false);
  421. }
  422. private void BasicInf_SizeChanged(object sender, EventArgs e)
  423. {
  424. Inf_name.Location = new Point(inf_name_label.Location.X + inf_name_label.Width + 5, inf_name_label.Location.Y);
  425. Inf_currentstep_label.Location = new Point(Inf_name.Location.X + Inf_name.Width + 45, Inf_name.Location.Y);
  426. Inf_currentstep.Location = new Point(Inf_currentstep_label.Location.X + Inf_currentstep_label.Width + 5, Inf_currentstep_label.Location.Y);
  427. Inf_linecode_label.Location = new Point(Inf_currentstep.Location.X + Inf_currentstep.Width + 45, Inf_currentstep.Location.Y);
  428. Inf_linecode.Location = new Point(Inf_linecode_label.Location.X + Inf_linecode_label.Width + 5, Inf_linecode_label.Location.Y);
  429. Inf_source_label.Location = new Point(Inf_linecode.Location.X + Inf_linecode.Width + 45, Inf_linecode.Location.Y);
  430. Inf_source.Location = new Point(Inf_source_label.Location.X + Inf_source_label.Width + 5, Inf_source_label.Location.Y);
  431. Inf_db_label.Location = new Point(Inf_source.Location.X + Inf_source.Width + 45, Inf_source.Location.Y);
  432. Inf_db.Location = new Point(Inf_db_label.Location.X + Inf_db_label.Width + 5, Inf_db_label.Location.Y);
  433. }
  434. private void MainTabControl_Selected(object sender, TabControlEventArgs e)
  435. {
  436. try
  437. {
  438. for (int i = 0; i < e.TabPage.Controls.Count; i++)
  439. {
  440. if (e.TabPage.Controls[i] is Form)
  441. {
  442. if (e.TabPage.Controls[i].Name == "Make_Repair")
  443. {
  444. Make.Make_Repair repair = e.TabPage.Controls[i] as Make.Make_Repair;
  445. if (repair.Controls["GetSNCode"].Text != "")
  446. {
  447. repair.RefreshData();
  448. }
  449. }
  450. }
  451. }
  452. }
  453. catch (Exception)
  454. {
  455. }
  456. }
  457. }
  458. }