Main.cs 12 KB

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