Main.cs 20 KB

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