Main.cs 21 KB

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