Main.cs 20 KB

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