Main.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. private void RefreshDB(object sender, EventArgs e)
  96. {
  97. DataHelper dh = new DataHelper();
  98. }
  99. /// <summary>
  100. ///设置窗体的最大化和最小化状态
  101. /// </summary>
  102. public void SetForm()
  103. {
  104. int WS_SYSMENU = 0x00080000; // 系统菜单
  105. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  106. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  107. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  108. }
  109. protected override CreateParams CreateParams
  110. {
  111. get
  112. {
  113. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  114. CreateParams cp = base.CreateParams;
  115. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  116. return cp;
  117. }
  118. }
  119. /// <summary>
  120. /// 重载创建文件句柄
  121. /// </summary>
  122. protected override void CreateHandle()
  123. {
  124. if (!IsHandleCreated)
  125. {
  126. try { base.CreateHandle(); }
  127. catch { }
  128. finally
  129. {
  130. if (!IsHandleCreated)
  131. base.RecreateHandle();
  132. }
  133. }
  134. }
  135. Bitmap image = Resources.close_button;
  136. //鼠标点下的时候监听
  137. private void headBar_MouseDown(object sender, MouseEventArgs e)
  138. {
  139. ReleaseCapture();
  140. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  141. }
  142. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  143. {
  144. GlobalEventsHandler.keycode = keyData;
  145. return false;
  146. }
  147. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  148. {
  149. try
  150. {
  151. Brush HeadTextColor = new SolidBrush(Color.Green);
  152. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  153. //先添加TabPage属性
  154. if (e.State == DrawItemState.Selected)
  155. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  156. else
  157. 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);
  158. //再画一个矩形框
  159. using (Pen p = new Pen(SystemBrushes.Control))
  160. {
  161. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  162. myTabRect.Width = CLOSE_SIZE;
  163. myTabRect.Height = CLOSE_SIZE;
  164. e.Graphics.DrawRectangle(p, myTabRect);
  165. }
  166. //画关闭符号
  167. using (Pen objpen = new Pen(Color.Black))
  168. {
  169. //使用图片
  170. Bitmap bt = new Bitmap(image);
  171. Point p5 = new Point(myTabRect.X, 8);
  172. e.Graphics.DrawImage(bt, p5);
  173. }
  174. e.Graphics.Dispose();
  175. }
  176. catch (Exception) { }
  177. }
  178. //关闭按钮功能
  179. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  180. {
  181. if (e.Button == MouseButtons.Left)
  182. {
  183. int x = e.X, y = e.Y;
  184. //计算关闭区域
  185. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  186. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  187. myTabRect.Width = CLOSE_SIZE;
  188. myTabRect.Height = CLOSE_SIZE;
  189. //如果鼠标在区域内就关闭选项卡
  190. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  191. if (isClose == true)
  192. {
  193. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  194. }
  195. }
  196. }
  197. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  198. {
  199. //移除已打开的Form中的Form
  200. TabPage tb = e.Control as TabPage;
  201. AccordionMenu.OpenedFormName.Remove("UAS_MES." + tb.Text);
  202. (tb.Controls[0] as Form).Close();
  203. //掉用了串口的程序需要在这个步骤关闭串口
  204. try
  205. {
  206. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  207. {
  208. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  209. (tb.Controls[0] as Form).Close();
  210. }
  211. }
  212. catch (Exception) { }
  213. }
  214. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  215. {
  216. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  217. if (logout_confirm == "Yes")
  218. {
  219. //注销的时候切换回默认数据库
  220. SystemInf.ConnectionString = ConfigurationManager.ConnectionStrings["MES"].ConnectionString;
  221. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  222. //清除上个用户的权限信息
  223. SystemInf.Caller.Clear();
  224. //清除已经打开过的窗口的信息
  225. AccordionMenu.OpenedFormName.Clear();
  226. //注销的时候将除了登陆窗口之外的全部窗口关闭
  227. for (int i = 0; i < Application.OpenForms.Count; i++)
  228. {
  229. if (Application.OpenForms[i].Name != "Login")
  230. Application.OpenForms[i].Close();
  231. }
  232. this.Hide();
  233. Login login = new Login();
  234. login.ShowDialog();
  235. RefreshDBConnect.Stop();
  236. this.Close();
  237. }
  238. }
  239. }
  240. }