Main.cs 12 KB

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