Main.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "Visual Studio 2013 Blue";
  97. }
  98. //关闭打印进程
  99. private static void Closelblprocess()
  100. {
  101. try
  102. {
  103. //杀死全部未关闭的打印进程
  104. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  105. foreach (string line in lines)
  106. {
  107. if (line != "")
  108. {
  109. string processid = line.Split('|')[0];
  110. string lblid = line.Split('|')[1];
  111. try
  112. {
  113. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
  114. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  115. }
  116. catch (Exception)
  117. {
  118. try
  119. {
  120. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  121. }
  122. catch (Exception)
  123. {
  124. }
  125. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  126. StreamReader sr = new StreamReader(fs);
  127. String s = sr.ReadToEnd();
  128. sr.Close();
  129. fs.Close();
  130. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  131. StreamWriter sw = new StreamWriter(fas);
  132. sw.Write(s.Replace(line + "\r\n", ""));
  133. sw.Flush();
  134. sw.Close();
  135. fas.Close();
  136. }
  137. }
  138. }
  139. }
  140. catch
  141. {
  142. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  143. StreamWriter sw = new StreamWriter(fas);
  144. sw.Write("");
  145. sw.Flush();
  146. sw.Close();
  147. fas.Close();
  148. }
  149. }
  150. private void TestPrintEnable()
  151. {
  152. try
  153. {
  154. ApplicationClass lbl = new ApplicationClass();
  155. SystemInf.EnablePrint = true;
  156. lbl.Quit();
  157. }
  158. catch (Exception)
  159. {
  160. SystemInf.EnablePrint = false;
  161. }
  162. }
  163. /// <summary>
  164. ///设置窗体的最大化和最小化状态
  165. /// </summary>
  166. public void SetForm()
  167. {
  168. int WS_SYSMENU = 0x00080000; // 系统菜单
  169. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  170. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  171. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  172. }
  173. protected override CreateParams CreateParams
  174. {
  175. get
  176. {
  177. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  178. CreateParams cp = base.CreateParams;
  179. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  180. return cp;
  181. }
  182. }
  183. /// <summary>
  184. /// 重载创建文件句柄
  185. /// </summary>
  186. protected override void CreateHandle()
  187. {
  188. if (!IsHandleCreated)
  189. {
  190. try { base.CreateHandle(); }
  191. catch { }
  192. finally
  193. {
  194. if (!IsHandleCreated)
  195. base.RecreateHandle();
  196. }
  197. }
  198. }
  199. Bitmap image = Resources.close_16px_558195_easyicon_net;
  200. //鼠标点下的时候监听
  201. private void headBar_MouseDown(object sender, MouseEventArgs e)
  202. {
  203. ReleaseCapture();
  204. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  205. }
  206. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  207. {
  208. GlobalEventsHandler.keycode = keyData;
  209. return false;
  210. }
  211. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  212. {
  213. try
  214. {
  215. Brush HeadTextColor = new SolidBrush(Color.Green);
  216. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  217. //先添加TabPage属性
  218. if (e.State == DrawItemState.Selected)
  219. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  220. else
  221. 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);
  222. //再画一个矩形框
  223. using (Pen p = new Pen(SystemBrushes.Control))
  224. {
  225. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  226. myTabRect.Width = CLOSE_SIZE;
  227. myTabRect.Height = CLOSE_SIZE;
  228. e.Graphics.DrawRectangle(p, myTabRect);
  229. }
  230. //画关闭符号
  231. using (Pen objpen = new Pen(Color.Black))
  232. {
  233. //使用图片
  234. Bitmap bt = new Bitmap(image);
  235. Point p5 = new Point(myTabRect.X, 8);
  236. e.Graphics.DrawImage(bt, p5);
  237. }
  238. e.Graphics.Dispose();
  239. }
  240. catch (Exception) { }
  241. }
  242. //关闭按钮功能
  243. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  244. {
  245. if (e.Button == MouseButtons.Left)
  246. {
  247. int x = e.X, y = e.Y;
  248. //计算关闭区域
  249. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  250. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  251. myTabRect.Width = CLOSE_SIZE;
  252. myTabRect.Height = CLOSE_SIZE;
  253. //如果鼠标在区域内就关闭选项卡
  254. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  255. if (isClose == true)
  256. {
  257. DataTable dt;
  258. string RemindInf = "";
  259. string PaOutBoxCode = "";
  260. string ObCheckno = "";
  261. switch (MainTabControl.SelectedTab.Controls[0].Tag.ToString())
  262. {
  263. case "Packing!PackageCollectionWeigh":
  264. Packing.Packing_PackageCollectionWeigh PackWeigh = (Packing.Packing_PackageCollectionWeigh)MainTabControl.SelectedTab.Controls[0];
  265. if (PackWeigh.ob_checkno.Text != "")
  266. {
  267. ObCheckno = PackWeigh.ob_checkno.Text;
  268. }
  269. if (PackWeigh.pa_outboxcode.Text != "")
  270. {
  271. PaOutBoxCode = PackWeigh.pa_outboxcode.Text;
  272. }
  273. break;
  274. case "Packing!PackageCollection":
  275. Packing.Packing_PackageCollection Pack = (Packing.Packing_PackageCollection)MainTabControl.SelectedTab.Controls[0];
  276. if (Pack.ob_checkno.Text != "")
  277. {
  278. ObCheckno = Pack.ob_checkno.Text;
  279. }
  280. if (Pack.pa_outboxcode.Text != "")
  281. {
  282. PaOutBoxCode = Pack.pa_outboxcode.Text;
  283. }
  284. break;
  285. case "Make!ColorBoxWeigh":
  286. Make.Make_ColorBoxWeigh ColorWeigh = (Make.Make_ColorBoxWeigh)MainTabControl.SelectedTab.Controls[0];
  287. if (ColorWeigh.ob_checkno.Text != "")
  288. {
  289. ObCheckno = ColorWeigh.ob_checkno.Text;
  290. }
  291. break;
  292. case "Packing!CartonBoxWeigh":
  293. Packing.Packing_CartonBoxWeigh CartonWeigh = (Packing.Packing_CartonBoxWeigh)MainTabControl.SelectedTab.Controls[0];
  294. if (CartonWeigh.ob_checkno.Text != "")
  295. {
  296. ObCheckno = CartonWeigh.ob_checkno.Text;
  297. }
  298. break;
  299. default:
  300. break;
  301. }
  302. if (ObCheckno != "")
  303. {
  304. dt = (DataTable)dh.ExecuteSql("select ob_status from oqcbatch where ob_checkno='" + ObCheckno + "'", "select");
  305. if (dt.Rows.Count > 0)
  306. if (dt.Rows[0][0].ToString() == "ENTERING")
  307. RemindInf = "当前批次未送检,是否关闭页面?";
  308. }
  309. if (PaOutBoxCode != "")
  310. {
  311. dt = (DataTable)dh.ExecuteSql("select pa_status from package where pa_outboxcode='" + PaOutBoxCode + "'", "select");
  312. if (dt.Rows.Count > 0)
  313. if (dt.Rows[0][0].ToString() != "1")
  314. RemindInf = "当前箱号未封箱,是否关闭页面?";
  315. }
  316. if (RemindInf != "")
  317. {
  318. string closetab_confirm = MessageBox.Show(this.ParentForm, RemindInf, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  319. if (closetab_confirm != "Yes")
  320. {
  321. return;
  322. }
  323. }
  324. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  325. }
  326. }
  327. }
  328. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  329. {
  330. //移除已打开的Form中的Form
  331. TabPage tb = e.Control as TabPage;
  332. NavagationBar.OpenedFormName.Remove(tb.Tag.ToString());
  333. (tb.Controls[0] as Form).Close();
  334. //掉用了串口的程序需要在这个步骤关闭串口
  335. try
  336. {
  337. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  338. {
  339. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  340. (tb.Controls[0] as Form).Close();
  341. }
  342. }
  343. catch (Exception) { }
  344. }
  345. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  346. {
  347. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  348. if (logout_confirm == "Yes")
  349. {
  350. //注销的时候切换回默认数据库
  351. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  352. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  353. //清除上个用户的权限信息
  354. SystemInf.Caller.Clear();
  355. //清除已经打开过的窗口的信息
  356. NavagationBar.OpenedFormName.Clear();
  357. //注销的时候将除了登陆窗口之外的全部窗口关闭
  358. for (int i = 0; i < System.Windows.Forms.Application.OpenForms.Count; i++)
  359. {
  360. if (System.Windows.Forms.Application.OpenForms[i].Name != "Login")
  361. System.Windows.Forms.Application.OpenForms[i].Close();
  362. }
  363. this.Hide();
  364. Login login = new Login();
  365. login.ShowDialog();
  366. this.Close();
  367. }
  368. }
  369. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  370. {
  371. try
  372. {
  373. //杀死全部未关闭的打印进程
  374. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  375. foreach (string line in lines)
  376. {
  377. if (line != "")
  378. {
  379. string processid = line.Split('|')[0];
  380. string lblid = line.Split('|')[1];
  381. try
  382. {
  383. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
  384. {
  385. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  386. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  387. StreamReader sr = new StreamReader(fs);
  388. String s = sr.ReadToEnd();
  389. sr.Close();
  390. fs.Close();
  391. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  392. StreamWriter sw = new StreamWriter(fas);
  393. sw.Write(s.Replace(line + "\r\n", ""));
  394. sw.Flush();
  395. sw.Close();
  396. fas.Close();
  397. }
  398. }
  399. catch (Exception)
  400. {
  401. }
  402. }
  403. }
  404. }
  405. catch
  406. {
  407. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  408. StreamWriter sw = new StreamWriter(fas);
  409. sw.Write("");
  410. sw.Flush();
  411. sw.Close();
  412. fas.Close();
  413. }
  414. BaseUtil.FormStepInOrOut(this, false);
  415. }
  416. private void MainTabControl_Selected(object sender, TabControlEventArgs e)
  417. {
  418. try
  419. {
  420. for (int i = 0; i < e.TabPage.Controls.Count; i++)
  421. {
  422. if (e.TabPage.Controls[i] is Form)
  423. {
  424. if (e.TabPage.Controls[i].Name == "Make_Repair")
  425. {
  426. Make.Make_Repair repair = e.TabPage.Controls[i] as Make.Make_Repair;
  427. if (repair.Controls["GetSNCode"].Text != "")
  428. {
  429. repair.RefreshData();
  430. }
  431. }
  432. }
  433. }
  434. }
  435. catch (Exception)
  436. {
  437. }
  438. }
  439. private void NavBar_SizeChanged(object sender, EventArgs e)
  440. {
  441. MainTabControl.Location = new Point(NavBar.Width + 5, MainTabControl.Location.Y);
  442. MainTabControl.Width = Width - NavBar.Width;
  443. MainTabControl.Height = Height - headBar1.Height - 27;
  444. }
  445. }
  446. }