Main.cs 21 KB

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