Main.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. thread = new Thread(DeleteLog);
  77. thread.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. /// <summary>
  103. ///设置窗体的最大化和最小化状态
  104. /// </summary>
  105. public void SetForm()
  106. {
  107. int WS_SYSMENU = 0x00080000; // 系统菜单
  108. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  109. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  110. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  111. }
  112. protected override CreateParams CreateParams
  113. {
  114. get
  115. {
  116. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  117. CreateParams cp = base.CreateParams;
  118. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  119. return cp;
  120. }
  121. }
  122. /// <summary>
  123. /// 重载创建文件句柄
  124. /// </summary>
  125. protected override void CreateHandle()
  126. {
  127. if (!IsHandleCreated)
  128. {
  129. try { base.CreateHandle(); }
  130. catch { }
  131. finally
  132. {
  133. if (!IsHandleCreated)
  134. base.RecreateHandle();
  135. }
  136. }
  137. }
  138. Bitmap image = Resources.close_button;
  139. //鼠标点下的时候监听
  140. private void headBar_MouseDown(object sender, MouseEventArgs e)
  141. {
  142. ReleaseCapture();
  143. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  144. }
  145. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  146. {
  147. GlobalEventsHandler.keycode = keyData;
  148. return false;
  149. }
  150. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  151. {
  152. try
  153. {
  154. Brush HeadTextColor = new SolidBrush(Color.Green);
  155. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  156. //先添加TabPage属性
  157. if (e.State == DrawItemState.Selected)
  158. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  159. else
  160. 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);
  161. //再画一个矩形框
  162. using (Pen p = new Pen(SystemBrushes.Control))
  163. {
  164. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  165. myTabRect.Width = CLOSE_SIZE;
  166. myTabRect.Height = CLOSE_SIZE;
  167. e.Graphics.DrawRectangle(p, myTabRect);
  168. }
  169. //画关闭符号
  170. using (Pen objpen = new Pen(Color.Black))
  171. {
  172. //使用图片
  173. Bitmap bt = new Bitmap(image);
  174. Point p5 = new Point(myTabRect.X, 8);
  175. e.Graphics.DrawImage(bt, p5);
  176. }
  177. e.Graphics.Dispose();
  178. }
  179. catch (Exception) { }
  180. }
  181. //关闭按钮功能
  182. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  183. {
  184. if (e.Button == MouseButtons.Left)
  185. {
  186. int x = e.X, y = e.Y;
  187. //计算关闭区域
  188. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  189. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  190. myTabRect.Width = CLOSE_SIZE;
  191. myTabRect.Height = CLOSE_SIZE;
  192. //如果鼠标在区域内就关闭选项卡
  193. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  194. if (isClose == true)
  195. {
  196. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  197. }
  198. }
  199. }
  200. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  201. {
  202. //移除已打开的Form中的Form
  203. TabPage tb = e.Control as TabPage;
  204. AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
  205. (tb.Controls[0] as Form).Close();
  206. //掉用了串口的程序需要在这个步骤关闭串口
  207. try
  208. {
  209. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  210. {
  211. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  212. (tb.Controls[0] as Form).Close();
  213. }
  214. }
  215. catch (Exception) { }
  216. }
  217. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  218. {
  219. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  220. if (logout_confirm == "Yes")
  221. {
  222. //注销的时候切换回默认数据库
  223. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  224. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  225. //清除上个用户的权限信息
  226. SystemInf.Caller.Clear();
  227. //清除已经打开过的窗口的信息
  228. AccordionMenu.OpenedFormName.Clear();
  229. //注销的时候将除了登陆窗口之外的全部窗口关闭
  230. for (int i = 0; i < Application.OpenForms.Count; i++)
  231. {
  232. if (Application.OpenForms[i].Name != "Login")
  233. Application.OpenForms[i].Close();
  234. }
  235. this.Hide();
  236. Login login = new Login();
  237. login.ShowDialog();
  238. this.Close();
  239. }
  240. }
  241. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  242. {
  243. //杀死全部未关闭的打印进程
  244. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  245. for (int i = 0; i < processes.Length; i++)
  246. {
  247. processes[i].Kill();
  248. }
  249. System.Environment.Exit(0);
  250. }
  251. }
  252. }