Main.cs 21 KB

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