Main.cs 12 KB

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