Main.cs 20 KB

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