Main.cs 12 KB

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