Main.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using LabelManager2;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using UAS_MES.CustomControl.AccordionMenu;
  12. using UAS_MES.DataOperate;
  13. using UAS_MES.Entity;
  14. using UAS_MES.Properties;
  15. using UAS_MES.PublicMethod;
  16. namespace UAS_MES
  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. Thread TestPrint;
  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. TestPrint = new Thread(TestPrintEnable);
  77. TestPrint.Start();
  78. this.Tag = "ShowDialogWindow";
  79. this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
  80. this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
  81. this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
  82. this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
  83. this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
  84. //设置底部栏目的基础信息
  85. Inf_name.Text = User.UserName;
  86. //inf_position.Text = User.UserPosition;
  87. Inf_linecode.Text = User.UserLineCode;
  88. Inf_source.Text = User.UserSourceCode;
  89. Inf_db.Text = SystemInf.CurrentDB;
  90. Inf_currentstep.Text = User.CurrentStepName;
  91. //设置获取当前屏幕大小自动全屏但是保留任务栏
  92. Rectangle ScreenArea = Screen.GetWorkingArea(this);
  93. Top = 0;
  94. Left = 0;
  95. Width = ScreenArea.Width;
  96. Height = ScreenArea.Height;
  97. SetForm();
  98. //设置导航栏宽度
  99. SystemInf.NavWidth = Menu.Width;
  100. SystemInf.HeadBarHeight = headBar1.Height;
  101. }
  102. //关闭打印进程
  103. private static void Closelblprocess()
  104. {
  105. //杀死全部未关闭的打印进程
  106. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  107. foreach (string line in lines)
  108. {
  109. if (line != "")
  110. {
  111. string processid = line.Split('|')[0];
  112. string lblid = line.Split('|')[1];
  113. try
  114. {
  115. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
  116. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  117. }
  118. catch (Exception)
  119. {
  120. try
  121. {
  122. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  123. }
  124. catch (Exception)
  125. {
  126. }
  127. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  128. StreamReader sr = new StreamReader(fs);
  129. String s = sr.ReadToEnd();
  130. sr.Close();
  131. fs.Close();
  132. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  133. StreamWriter sw = new StreamWriter(fas);
  134. sw.Write(s.Replace(line + "\r\n", ""));
  135. sw.Flush();
  136. sw.Close();
  137. fas.Close();
  138. }
  139. }
  140. }
  141. }
  142. private void TestPrintEnable()
  143. {
  144. try
  145. {
  146. ApplicationClass lbl = new ApplicationClass();
  147. SystemInf.EnablePrint = true;
  148. lbl.Quit();
  149. }
  150. catch (Exception)
  151. {
  152. SystemInf.EnablePrint = false;
  153. }
  154. }
  155. /// <summary>
  156. ///设置窗体的最大化和最小化状态
  157. /// </summary>
  158. public void SetForm()
  159. {
  160. int WS_SYSMENU = 0x00080000; // 系统菜单
  161. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  162. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  163. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  164. }
  165. protected override CreateParams CreateParams
  166. {
  167. get
  168. {
  169. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  170. CreateParams cp = base.CreateParams;
  171. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  172. return cp;
  173. }
  174. }
  175. /// <summary>
  176. /// 重载创建文件句柄
  177. /// </summary>
  178. protected override void CreateHandle()
  179. {
  180. if (!IsHandleCreated)
  181. {
  182. try { base.CreateHandle(); }
  183. catch { }
  184. finally
  185. {
  186. if (!IsHandleCreated)
  187. base.RecreateHandle();
  188. }
  189. }
  190. }
  191. Bitmap image = Resources.close_button;
  192. //鼠标点下的时候监听
  193. private void headBar_MouseDown(object sender, MouseEventArgs e)
  194. {
  195. ReleaseCapture();
  196. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  197. }
  198. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  199. {
  200. GlobalEventsHandler.keycode = keyData;
  201. return false;
  202. }
  203. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  204. {
  205. try
  206. {
  207. Brush HeadTextColor = new SolidBrush(Color.Green);
  208. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  209. //先添加TabPage属性
  210. if (e.State == DrawItemState.Selected)
  211. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  212. else
  213. 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);
  214. //再画一个矩形框
  215. using (Pen p = new Pen(SystemBrushes.Control))
  216. {
  217. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  218. myTabRect.Width = CLOSE_SIZE;
  219. myTabRect.Height = CLOSE_SIZE;
  220. e.Graphics.DrawRectangle(p, myTabRect);
  221. }
  222. //画关闭符号
  223. using (Pen objpen = new Pen(Color.Black))
  224. {
  225. //使用图片
  226. Bitmap bt = new Bitmap(image);
  227. Point p5 = new Point(myTabRect.X, 8);
  228. e.Graphics.DrawImage(bt, p5);
  229. }
  230. e.Graphics.Dispose();
  231. }
  232. catch (Exception) { }
  233. }
  234. //关闭按钮功能
  235. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  236. {
  237. if (e.Button == MouseButtons.Left)
  238. {
  239. int x = e.X, y = e.Y;
  240. //计算关闭区域
  241. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  242. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  243. myTabRect.Width = CLOSE_SIZE;
  244. myTabRect.Height = CLOSE_SIZE;
  245. //如果鼠标在区域内就关闭选项卡
  246. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  247. if (isClose == true)
  248. {
  249. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  250. }
  251. }
  252. }
  253. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  254. {
  255. //移除已打开的Form中的Form
  256. TabPage tb = e.Control as TabPage;
  257. AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
  258. (tb.Controls[0] as Form).Close();
  259. //掉用了串口的程序需要在这个步骤关闭串口
  260. try
  261. {
  262. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  263. {
  264. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  265. (tb.Controls[0] as Form).Close();
  266. }
  267. }
  268. catch (Exception) { }
  269. }
  270. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  271. {
  272. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  273. if (logout_confirm == "Yes")
  274. {
  275. //注销的时候切换回默认数据库
  276. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  277. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  278. //清除上个用户的权限信息
  279. SystemInf.Caller.Clear();
  280. //清除已经打开过的窗口的信息
  281. AccordionMenu.OpenedFormName.Clear();
  282. //注销的时候将除了登陆窗口之外的全部窗口关闭
  283. for (int i = 0; i < System.Windows.Forms.Application.OpenForms.Count; i++)
  284. {
  285. if (System.Windows.Forms.Application.OpenForms[i].Name != "Login")
  286. System.Windows.Forms.Application.OpenForms[i].Close();
  287. }
  288. this.Hide();
  289. Login login = new Login();
  290. login.ShowDialog();
  291. this.Close();
  292. }
  293. }
  294. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  295. {
  296. //杀死全部未关闭的打印进程
  297. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  298. foreach (string line in lines)
  299. {
  300. if (line != "")
  301. {
  302. string processid = line.Split('|')[0];
  303. string lblid = line.Split('|')[1];
  304. try
  305. {
  306. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
  307. {
  308. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  309. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  310. StreamReader sr = new StreamReader(fs);
  311. String s = sr.ReadToEnd();
  312. sr.Close();
  313. fs.Close();
  314. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  315. StreamWriter sw = new StreamWriter(fas);
  316. sw.Write(s.Replace(line + "\r\n", ""));
  317. sw.Flush();
  318. sw.Close();
  319. fas.Close();
  320. }
  321. }
  322. catch (Exception)
  323. {
  324. }
  325. }
  326. }
  327. BaseUtil.FormStepInOrOut(this, false);
  328. }
  329. private void BasicInf_SizeChanged(object sender, EventArgs e)
  330. {
  331. Inf_name.Location = new Point(inf_name_label.Location.X + inf_name_label.Width + 5, inf_name_label.Location.Y);
  332. Inf_currentstep_label.Location = new Point(Inf_name.Location.X + Inf_name.Width + 45, Inf_name.Location.Y);
  333. Inf_currentstep.Location = new Point(Inf_currentstep_label.Location.X + Inf_currentstep_label.Width + 5, Inf_currentstep_label.Location.Y);
  334. Inf_linecode_label.Location = new Point(Inf_currentstep.Location.X + Inf_currentstep.Width + 45, Inf_currentstep.Location.Y);
  335. Inf_linecode.Location = new Point(Inf_linecode_label.Location.X + Inf_linecode_label.Width + 5, Inf_linecode_label.Location.Y);
  336. Inf_source_label.Location = new Point(Inf_linecode.Location.X + Inf_linecode.Width + 45, Inf_linecode.Location.Y);
  337. Inf_source.Location = new Point(Inf_source_label.Location.X + Inf_source_label.Width + 5, Inf_source_label.Location.Y);
  338. Inf_db_label.Location = new Point(Inf_source.Location.X + Inf_source.Width + 45, Inf_source.Location.Y);
  339. Inf_db.Location = new Point(Inf_db_label.Location.X + Inf_db_label.Width + 5, Inf_db_label.Location.Y);
  340. }
  341. }
  342. }