Main.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using UAS_MES.CustomControl.AccordionMenu;
  10. using UAS_MES.DataOperate;
  11. using UAS_MES.Entity;
  12. using UAS_MES.Properties;
  13. using UAS_MES.PublicMethod;
  14. namespace UAS_MES
  15. {
  16. public partial class Main : Form
  17. {
  18. //所有用到了headBar的部分都需要这段代码
  19. [DllImport("user32.dll")]
  20. public static extern bool ReleaseCapture();
  21. [DllImport("user32.dll")]
  22. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  23. [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
  24. public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
  25. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  26. public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
  27. public const int WM_SYSCOMMAND = 0x0112;
  28. public const int SC_MOVE = 0xF010;
  29. public const int HTCAPTION = 0x0002;
  30. string SerialPort1 = "Make!ColorBoxWeigh#Make!CartonBoxWeigh";
  31. Thread thread;
  32. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  33. public Main()
  34. {
  35. InitializeComponent();
  36. DoubleBuffered = true;
  37. SetStyle(ControlStyles.UserPaint, true);
  38. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  39. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  40. }
  41. /// <summary>
  42. /// 删除7天前的日志
  43. /// </summary>
  44. private void DeleteLog()
  45. {
  46. DirectoryInfo dir = new DirectoryInfo(sysdisc + @":\Log");
  47. List<string> DeleteFileName = new List<string>();
  48. //保存6天前和今天的操作日志
  49. for (int i = 1; i <= 6; i++)
  50. {
  51. DeleteFileName.Add(DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd") + ".txt");
  52. }
  53. DeleteFileName.Add(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  54. if (dir.Exists)
  55. {
  56. FileInfo[] file = dir.GetFiles();
  57. foreach (FileInfo f in file)
  58. {
  59. if (!DeleteFileName.Contains(f.Name))
  60. f.Delete();
  61. }
  62. }
  63. }
  64. //tabPage标签图片尺寸
  65. const int CLOSE_SIZE = 16;
  66. //窗体加载的时候将headBar的事件委托给指定的函数
  67. private void Main_Load(object sender, EventArgs e)
  68. {
  69. thread = new Thread(DeleteLog);
  70. thread.Start();
  71. RefreshDBConnect.Interval = 600 * 1000;
  72. RefreshDBConnect.Tick += RefreshDB;
  73. RefreshDBConnect.Start();
  74. this.Tag = "ShowDialogWindow";
  75. this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
  76. this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
  77. this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
  78. this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
  79. this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
  80. //设置底部栏目的基础信息
  81. inf_name.Text = User.UserName;
  82. //inf_position.Text = User.UserPosition;
  83. Inf_linecode.Text = User.UserLineCode;
  84. inf_source.Text = User.UserSourceCode;
  85. inf_db.Text = SystemInf.CurrentDB;
  86. inf_currentstep.Text = User.CurrentStepName;
  87. //设置获取当前屏幕大小自动全屏但是保留任务栏
  88. Rectangle ScreenArea = Screen.GetWorkingArea(this);
  89. Top = 0;
  90. Left = 0;
  91. Width = ScreenArea.Width;
  92. Height = ScreenArea.Height;
  93. SetForm();
  94. //设置导航栏宽度
  95. SystemInf.NavWidth = Menu.Width;
  96. }
  97. private void RefreshDB(object sender, EventArgs e)
  98. {
  99. DataHelper dh = new DataHelper();
  100. }
  101. /// <summary>
  102. ///设置窗体的最大化和最小化状态
  103. /// </summary>
  104. public void SetForm()
  105. {
  106. int WS_SYSMENU = 0x00080000; // 系统菜单
  107. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  108. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  109. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  110. }
  111. protected override CreateParams CreateParams
  112. {
  113. get
  114. {
  115. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  116. CreateParams cp = base.CreateParams;
  117. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  118. return cp;
  119. }
  120. }
  121. /// <summary>
  122. /// 重载创建文件句柄
  123. /// </summary>
  124. protected override void CreateHandle()
  125. {
  126. if (!IsHandleCreated)
  127. {
  128. try { base.CreateHandle(); }
  129. catch { }
  130. finally
  131. {
  132. if (!IsHandleCreated)
  133. base.RecreateHandle();
  134. }
  135. }
  136. }
  137. Bitmap image = Resources.close_button;
  138. //鼠标点下的时候监听
  139. private void headBar_MouseDown(object sender, MouseEventArgs e)
  140. {
  141. ReleaseCapture();
  142. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  143. }
  144. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  145. {
  146. GlobalEventsHandler.keycode = keyData;
  147. return false;
  148. }
  149. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  150. {
  151. try
  152. {
  153. Brush HeadTextColor = new SolidBrush(Color.Green);
  154. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  155. //先添加TabPage属性
  156. if (e.State == DrawItemState.Selected)
  157. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  158. else
  159. 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);
  160. //再画一个矩形框
  161. using (Pen p = new Pen(SystemBrushes.Control))
  162. {
  163. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  164. myTabRect.Width = CLOSE_SIZE;
  165. myTabRect.Height = CLOSE_SIZE;
  166. e.Graphics.DrawRectangle(p, myTabRect);
  167. }
  168. //画关闭符号
  169. using (Pen objpen = new Pen(Color.Black))
  170. {
  171. //使用图片
  172. Bitmap bt = new Bitmap(image);
  173. Point p5 = new Point(myTabRect.X, 8);
  174. e.Graphics.DrawImage(bt, p5);
  175. }
  176. e.Graphics.Dispose();
  177. }
  178. catch (Exception) { }
  179. }
  180. //关闭按钮功能
  181. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  182. {
  183. if (e.Button == MouseButtons.Left)
  184. {
  185. int x = e.X, y = e.Y;
  186. //计算关闭区域
  187. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  188. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  189. myTabRect.Width = CLOSE_SIZE;
  190. myTabRect.Height = CLOSE_SIZE;
  191. //如果鼠标在区域内就关闭选项卡
  192. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  193. if (isClose == true)
  194. {
  195. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  196. }
  197. }
  198. }
  199. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  200. {
  201. //移除已打开的Form中的Form
  202. TabPage tb = e.Control as TabPage;
  203. AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
  204. (tb.Controls[0] as Form).Close();
  205. //掉用了串口的程序需要在这个步骤关闭串口
  206. try
  207. {
  208. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  209. {
  210. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  211. (tb.Controls[0] as Form).Close();
  212. }
  213. }
  214. catch (Exception) { }
  215. }
  216. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  217. {
  218. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  219. if (logout_confirm == "Yes")
  220. {
  221. //注销的时候切换回默认数据库
  222. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  223. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  224. //清除上个用户的权限信息
  225. SystemInf.Caller.Clear();
  226. //清除已经打开过的窗口的信息
  227. AccordionMenu.OpenedFormName.Clear();
  228. //注销的时候将除了登陆窗口之外的全部窗口关闭
  229. for (int i = 0; i < Application.OpenForms.Count; i++)
  230. {
  231. if (Application.OpenForms[i].Name != "Login")
  232. Application.OpenForms[i].Close();
  233. }
  234. this.Hide();
  235. Login login = new Login();
  236. login.ShowDialog();
  237. RefreshDBConnect.Stop();
  238. this.Close();
  239. }
  240. }
  241. }
  242. }