Main.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. Closelblprocess();
  74. thread = new Thread(DeleteLog);
  75. thread.Start();
  76. TestPrint = new Thread(TestPrintEnable);
  77. TestPrint.Start();
  78. this.Tag = "ShowDialogWindow";
  79. this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
  80. this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
  81. this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
  82. this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
  83. this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
  84. //设置底部栏目的基础信息
  85. Inf_name.Text = User.UserName;
  86. //inf_position.Text = User.UserPosition;
  87. Inf_linecode.Text = User.UserLineCode;
  88. Inf_source.Text = User.UserSourceCode;
  89. Inf_db.Text = SystemInf.CurrentDB;
  90. Inf_currentstep.Text = User.CurrentStepName;
  91. //设置获取当前屏幕大小自动全屏但是保留任务栏
  92. Rectangle ScreenArea = Screen.GetWorkingArea(this);
  93. Top = 0;
  94. Left = 0;
  95. Width = ScreenArea.Width;
  96. Height = ScreenArea.Height;
  97. SetForm();
  98. //设置导航栏宽度
  99. SystemInf.NavWidth = Menu.Width;
  100. SystemInf.HeadBarHeight = headBar1.Height;
  101. }
  102. //关闭打印进程
  103. private static void Closelblprocess()
  104. {
  105. try
  106. {
  107. //杀死全部未关闭的打印进程
  108. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  109. foreach (string line in lines)
  110. {
  111. if (line != "")
  112. {
  113. string processid = line.Split('|')[0];
  114. string lblid = line.Split('|')[1];
  115. try
  116. {
  117. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
  118. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  119. }
  120. catch (Exception)
  121. {
  122. try
  123. {
  124. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  125. }
  126. catch (Exception)
  127. {
  128. }
  129. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  130. StreamReader sr = new StreamReader(fs);
  131. String s = sr.ReadToEnd();
  132. sr.Close();
  133. fs.Close();
  134. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  135. StreamWriter sw = new StreamWriter(fas);
  136. sw.Write(s.Replace(line + "\r\n", ""));
  137. sw.Flush();
  138. sw.Close();
  139. fas.Close();
  140. }
  141. }
  142. }
  143. }
  144. catch
  145. {
  146. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  147. StreamWriter sw = new StreamWriter(fas);
  148. sw.Write("");
  149. sw.Flush();
  150. sw.Close();
  151. fas.Close();
  152. }
  153. }
  154. private void TestPrintEnable()
  155. {
  156. try
  157. {
  158. ApplicationClass lbl = new ApplicationClass();
  159. SystemInf.EnablePrint = true;
  160. lbl.Quit();
  161. }
  162. catch (Exception)
  163. {
  164. SystemInf.EnablePrint = false;
  165. }
  166. }
  167. /// <summary>
  168. ///设置窗体的最大化和最小化状态
  169. /// </summary>
  170. public void SetForm()
  171. {
  172. int WS_SYSMENU = 0x00080000; // 系统菜单
  173. int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
  174. int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
  175. SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  176. }
  177. protected override CreateParams CreateParams
  178. {
  179. get
  180. {
  181. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  182. CreateParams cp = base.CreateParams;
  183. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  184. return cp;
  185. }
  186. }
  187. /// <summary>
  188. /// 重载创建文件句柄
  189. /// </summary>
  190. protected override void CreateHandle()
  191. {
  192. if (!IsHandleCreated)
  193. {
  194. try { base.CreateHandle(); }
  195. catch { }
  196. finally
  197. {
  198. if (!IsHandleCreated)
  199. base.RecreateHandle();
  200. }
  201. }
  202. }
  203. Bitmap image = Resources.close_button;
  204. //鼠标点下的时候监听
  205. private void headBar_MouseDown(object sender, MouseEventArgs e)
  206. {
  207. ReleaseCapture();
  208. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  209. }
  210. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  211. {
  212. GlobalEventsHandler.keycode = keyData;
  213. return false;
  214. }
  215. private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  216. {
  217. try
  218. {
  219. Brush HeadTextColor = new SolidBrush(Color.Green);
  220. Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  221. //先添加TabPage属性
  222. if (e.State == DrawItemState.Selected)
  223. e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
  224. else
  225. 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);
  226. //再画一个矩形框
  227. using (Pen p = new Pen(SystemBrushes.Control))
  228. {
  229. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  230. myTabRect.Width = CLOSE_SIZE;
  231. myTabRect.Height = CLOSE_SIZE;
  232. e.Graphics.DrawRectangle(p, myTabRect);
  233. }
  234. //画关闭符号
  235. using (Pen objpen = new Pen(Color.Black))
  236. {
  237. //使用图片
  238. Bitmap bt = new Bitmap(image);
  239. Point p5 = new Point(myTabRect.X, 8);
  240. e.Graphics.DrawImage(bt, p5);
  241. }
  242. e.Graphics.Dispose();
  243. }
  244. catch (Exception) { }
  245. }
  246. //关闭按钮功能
  247. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  248. {
  249. if (e.Button == MouseButtons.Left)
  250. {
  251. int x = e.X, y = e.Y;
  252. //计算关闭区域
  253. Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  254. myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  255. myTabRect.Width = CLOSE_SIZE;
  256. myTabRect.Height = CLOSE_SIZE;
  257. //如果鼠标在区域内就关闭选项卡
  258. bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
  259. if (isClose == true)
  260. {
  261. this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  262. }
  263. }
  264. }
  265. private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
  266. {
  267. //移除已打开的Form中的Form
  268. TabPage tb = e.Control as TabPage;
  269. AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
  270. (tb.Controls[0] as Form).Close();
  271. //掉用了串口的程序需要在这个步骤关闭串口
  272. try
  273. {
  274. if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
  275. {
  276. //在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
  277. (tb.Controls[0] as Form).Close();
  278. }
  279. }
  280. catch (Exception) { }
  281. }
  282. private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  283. {
  284. string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  285. if (logout_confirm == "Yes")
  286. {
  287. //注销的时候切换回默认数据库
  288. SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  289. DataHelper.DBConnectionString = SystemInf.ConnectionString;
  290. //清除上个用户的权限信息
  291. SystemInf.Caller.Clear();
  292. //清除已经打开过的窗口的信息
  293. AccordionMenu.OpenedFormName.Clear();
  294. //注销的时候将除了登陆窗口之外的全部窗口关闭
  295. for (int i = 0; i < System.Windows.Forms.Application.OpenForms.Count; i++)
  296. {
  297. if (System.Windows.Forms.Application.OpenForms[i].Name != "Login")
  298. System.Windows.Forms.Application.OpenForms[i].Close();
  299. }
  300. this.Hide();
  301. Login login = new Login();
  302. login.ShowDialog();
  303. this.Close();
  304. }
  305. }
  306. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  307. {
  308. try
  309. {
  310. //杀死全部未关闭的打印进程
  311. string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
  312. foreach (string line in lines)
  313. {
  314. if (line != "")
  315. {
  316. string processid = line.Split('|')[0];
  317. string lblid = line.Split('|')[1];
  318. try
  319. {
  320. if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
  321. {
  322. System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
  323. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
  324. StreamReader sr = new StreamReader(fs);
  325. String s = sr.ReadToEnd();
  326. sr.Close();
  327. fs.Close();
  328. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  329. StreamWriter sw = new StreamWriter(fas);
  330. sw.Write(s.Replace(line + "\r\n", ""));
  331. sw.Flush();
  332. sw.Close();
  333. fas.Close();
  334. }
  335. }
  336. catch (Exception)
  337. {
  338. }
  339. }
  340. }
  341. }
  342. catch
  343. {
  344. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
  345. StreamWriter sw = new StreamWriter(fas);
  346. sw.Write("");
  347. sw.Flush();
  348. sw.Close();
  349. fas.Close();
  350. }
  351. BaseUtil.FormStepInOrOut(this, false);
  352. }
  353. private void BasicInf_SizeChanged(object sender, EventArgs e)
  354. {
  355. Inf_name.Location = new Point(inf_name_label.Location.X + inf_name_label.Width + 5, inf_name_label.Location.Y);
  356. Inf_currentstep_label.Location = new Point(Inf_name.Location.X + Inf_name.Width + 45, Inf_name.Location.Y);
  357. Inf_currentstep.Location = new Point(Inf_currentstep_label.Location.X + Inf_currentstep_label.Width + 5, Inf_currentstep_label.Location.Y);
  358. Inf_linecode_label.Location = new Point(Inf_currentstep.Location.X + Inf_currentstep.Width + 45, Inf_currentstep.Location.Y);
  359. Inf_linecode.Location = new Point(Inf_linecode_label.Location.X + Inf_linecode_label.Width + 5, Inf_linecode_label.Location.Y);
  360. Inf_source_label.Location = new Point(Inf_linecode.Location.X + Inf_linecode.Width + 45, Inf_linecode.Location.Y);
  361. Inf_source.Location = new Point(Inf_source_label.Location.X + Inf_source_label.Width + 5, Inf_source_label.Location.Y);
  362. Inf_db_label.Location = new Point(Inf_source.Location.X + Inf_source.Width + 45, Inf_source.Location.Y);
  363. Inf_db.Location = new Point(Inf_db_label.Location.X + Inf_db_label.Width + 5, Inf_db_label.Location.Y);
  364. }
  365. private void UpperCollection_CheckedChanged(object sender, EventArgs e)
  366. {
  367. SystemInf.UpperCollection = UpperCollection.Checked;
  368. }
  369. }
  370. }