Main.cs 20 KB

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