Main.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  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.CustomControl.AccordionMenu;
  11. using UAS_MES.DataOperate;
  12. using UAS_MES.Entity;
  13. using UAS_MES.Properties;
  14. using UAS_MES.PublicMethod;
  15. namespace UAS_MES
  16. {
  17. public partial class Main : Form
  18. {
  19. //所有用到了headBar的部分都需要这段代码
  20. [DllImport("user32.dll")]
  21. public static extern bool ReleaseCapture();
  22. [DllImport("user32.dll")]
  23. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  24. [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
  25. public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
  26. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  27. public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
  28. public const int WM_SYSCOMMAND = 0x0112;
  29. public const int SC_MOVE = 0xF010;
  30. public const int HTCAPTION = 0x0002;
  31. string SerialPort1 = "Make!ColorBoxWeigh#Make!CartonBoxWeigh";
  32. Thread thread;
  33. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  34. public Main()
  35. {
  36. InitializeComponent();
  37. DoubleBuffered = true;
  38. SetStyle(ControlStyles.UserPaint, true);
  39. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  40. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  41. }
  42. /// <summary>
  43. /// 删除7天前的日志
  44. /// </summary>
  45. private void DeleteLog()
  46. {
  47. DirectoryInfo dir = new DirectoryInfo(sysdisc + @":\Log");
  48. List<string> DeleteFileName = new List<string>();
  49. //保存6天前和今天的操作日志
  50. for (int i = 1; i <= 30; i++)
  51. {
  52. DeleteFileName.Add(DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd") + ".txt");
  53. }
  54. DeleteFileName.Add(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  55. if (dir.Exists)
  56. {
  57. FileInfo[] file = dir.GetFiles();
  58. foreach (FileInfo f in file)
  59. {
  60. if (!DeleteFileName.Contains(f.Name))
  61. f.Delete();
  62. }
  63. }
  64. }
  65. //tabPage标签图片尺寸
  66. const int CLOSE_SIZE = 16;
  67. //窗体加载的时候将headBar的事件委托给指定的函数
  68. private void Main_Load(object sender, EventArgs e)
  69. {
  70. //杀死全部未关闭的打印进程
  71. //Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  72. //for (int i = 0; i < processes.Length; i++)
  73. //{
  74. // processes[i].Kill();
  75. //}
  76. string[] lines = System.IO.File.ReadAllLines(sysdisc + @":\" + "lblprocess" + ".txt");
  77. foreach (string line in lines)
  78. {
  79. if (line != "")
  80. {
  81. string processid = line.Split('|')[0];
  82. string lblid = line.Split('|')[1];
  83. try
  84. {
  85. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName == System.Diagnostics.Process.GetCurrentProcess().ProcessName)
  86. {
  87. }
  88. else
  89. {
  90. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  91. }
  92. }
  93. catch (Exception)
  94. {
  95. try
  96. {
  97. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  98. }
  99. catch (Exception)
  100. {
  101. }
  102. Console.WriteLine("删除");
  103. FileStream fas = new FileStream(sysdisc + @":\" + "lblprocess" + ".txt", FileMode.Create, FileAccess.ReadWrite);
  104. StreamWriter sw = new StreamWriter(fas);
  105. StreamReader sr = new StreamReader(fas);
  106. String s = sr.ReadToEnd();
  107. sw.Write(s.Replace(line+"\r\n",""));
  108. sw.Flush();
  109. sw.Close();
  110. sr.Close();
  111. fas.Close();
  112. }
  113. }
  114. }
  115. thread = new Thread(DeleteLog);
  116. thread.Start();
  117. this.Tag = "ShowDialogWindow";
  118. this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
  119. this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
  120. this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
  121. this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
  122. this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
  123. //设置底部栏目的基础信息
  124. inf_name.Text = User.UserName;
  125. //inf_position.Text = User.UserPosition;
  126. Inf_linecode.Text = User.UserLineCode;
  127. inf_source.Text = User.UserSourceCode;
  128. inf_db.Text = SystemInf.CurrentDB;
  129. inf_currentstep.Text = User.CurrentStepName;
  130. //设置获取当前屏幕大小自动全屏但是保留任务栏
  131. Rectangle ScreenArea = Screen.GetWorkingArea(this);
  132. Top = 0;
  133. Left = 0;
  134. Width = ScreenArea.Width;
  135. Height = ScreenArea.Height;
  136. SetForm();
  137. //设置导航栏宽度
  138. SystemInf.NavWidth = Menu.Width;
  139. SystemInf.HeadBarHeight = headBar1.Height;
  140. }
  141. /// <summary>
  142. ///设置窗体的最大化和最小化状态
  143. /// </summary>
  144. public void SetForm()
  145. {
  146. int WS_SYSMENU = 0x00080000; // 系统菜单
  147. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  148. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  149. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  150. }
  151. protected override CreateParams CreateParams
  152. {
  153. get
  154. {
  155. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  156. CreateParams cp = base.CreateParams;
  157. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  158. return cp;
  159. }
  160. }
  161. /// <summary>
  162. /// 重载创建文件句柄
  163. /// </summary>
  164. protected override void CreateHandle()
  165. {
  166. if (!IsHandleCreated)
  167. {
  168. try { base.CreateHandle(); }
  169. catch { }
  170. finally
  171. {
  172. if (!IsHandleCreated)
  173. base.RecreateHandle();
  174. }
  175. }
  176. }
  177. Bitmap image = Resources.close_button;
  178. //鼠标点下的时候监听
  179. private void headBar_MouseDown(object sender, MouseEventArgs e)
  180. {
  181. ReleaseCapture();
  182. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  183. }
  184. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  185. {
  186. GlobalEventsHandler.keycode = keyData;
  187. return false;
  188. }
  189. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  190. {
  191. try
  192. {
  193. Brush HeadTextColor = new SolidBrush(Color.Green);
  194. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  195. //先添加TabPage属性
  196. if (e.State == DrawItemState.Selected)
  197. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  198. else
  199. 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);
  200. //再画一个矩形框
  201. using (Pen p = new Pen(SystemBrushes.Control))
  202. {
  203. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  204. myTabRect.Width = CLOSE_SIZE;
  205. myTabRect.Height = CLOSE_SIZE;
  206. e.Graphics.DrawRectangle(p, myTabRect);
  207. }
  208. //画关闭符号
  209. using (Pen objpen = new Pen(Color.Black))
  210. {
  211. //使用图片
  212. Bitmap bt = new Bitmap(image);
  213. Point p5 = new Point(myTabRect.X, 8);
  214. e.Graphics.DrawImage(bt, p5);
  215. }
  216. e.Graphics.Dispose();
  217. }
  218. catch (Exception) { }
  219. }
  220. //关闭按钮功能
  221. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  222. {
  223. if (e.Button == MouseButtons.Left)
  224. {
  225. int x = e.X, y = e.Y;
  226. //计算关闭区域
  227. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  228. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  229. myTabRect.Width = CLOSE_SIZE;
  230. myTabRect.Height = CLOSE_SIZE;
  231. //如果鼠标在区域内就关闭选项卡
  232. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  233. if (isClose == true)
  234. {
  235. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  236. }
  237. }
  238. }
  239. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  240. {
  241. //移除已打开的Form中的Form
  242. TabPage tb = e.Control as TabPage;
  243. AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
  244. (tb.Controls[0] as Form).Close();
  245. //掉用了串口的程序需要在这个步骤关闭串口
  246. try
  247. {
  248. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  249. {
  250. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  251. (tb.Controls[0] as Form).Close();
  252. }
  253. }
  254. catch (Exception) { }
  255. }
  256. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  257. {
  258. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  259. if (logout_confirm == "Yes")
  260. {
  261. //注销的时候切换回默认数据库
  262. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  263. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  264. //清除上个用户的权限信息
  265. SystemInf.Caller.Clear();
  266. //清除已经打开过的窗口的信息
  267. AccordionMenu.OpenedFormName.Clear();
  268. //注销的时候将除了登陆窗口之外的全部窗口关闭
  269. for (int i = 0; i < Application.OpenForms.Count; i++)
  270. {
  271. if (Application.OpenForms[i].Name != "Login")
  272. Application.OpenForms[i].Close();
  273. }
  274. this.Hide();
  275. Login login = new Login();
  276. login.ShowDialog();
  277. this.Close();
  278. }
  279. }
  280. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  281. {
  282. //杀死全部未关闭的打印进程
  283. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  284. for (int i = 0; i < processes.Length; i++)
  285. {
  286. processes[i].Kill();
  287. }
  288. System.Environment.Exit(0);
  289. }
  290. }
  291. }