Browse Source

关闭主窗口时关闭此程序打开的打印驱动

Hcsy 8 years ago
parent
commit
0f984c27f8
1 changed files with 69 additions and 32 deletions
  1. 69 32
      UAS-MES/Main.cs

+ 69 - 32
UAS-MES/Main.cs

@@ -81,9 +81,42 @@ namespace UAS_MES
         const int CLOSE_SIZE = 16;
         //窗体加载的时候将headBar的事件委托给指定的函数
         private void Main_Load(object sender, EventArgs e)
+        {
+            Closelblprocess();
+            thread = new Thread(DeleteLog);
+            thread.Start();
+            TestPrint = new Thread(TestPrintEnable);
+            TestPrint.Start();
+            this.Tag = "ShowDialogWindow";
+            this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
+            this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
+            this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
+            this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
+            this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
+            //设置底部栏目的基础信息
+            inf_name.Text = User.UserName;
+            //inf_position.Text = User.UserPosition;
+            Inf_linecode.Text = User.UserLineCode;
+            inf_source.Text = User.UserSourceCode;
+            inf_db.Text = SystemInf.CurrentDB;
+            inf_currentstep.Text = User.CurrentStepName;
+            //设置获取当前屏幕大小自动全屏但是保留任务栏
+            Rectangle ScreenArea = Screen.GetWorkingArea(this);
+            Top = 0;
+            Left = 0;
+            Width = ScreenArea.Width;
+            Height = ScreenArea.Height;
+            SetForm();
+            //设置导航栏宽度
+            SystemInf.NavWidth = Menu.Width;
+            SystemInf.HeadBarHeight = headBar1.Height;
+        }
+
+        //关闭打印进程
+        private static void Closelblprocess()
         {
             //杀死全部未关闭的打印进程
-            string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder+ "lblprocess.txt");
+            string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
             foreach (string line in lines)
             {
                 if (line != "")
@@ -93,7 +126,7 @@ namespace UAS_MES
                     try
                     {
                         if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
-                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();   
+                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
                     }
                     catch (Exception)
                     {
@@ -104,47 +137,20 @@ namespace UAS_MES
                         catch (Exception)
                         {
                         }
-                        FileStream fs = new FileStream(SystemInf.CacheFolder+ "lblprocess.txt", FileMode.Open, FileAccess.Read);
+                        FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
                         StreamReader sr = new StreamReader(fs);
                         String s = sr.ReadToEnd();
                         sr.Close();
                         fs.Close();
                         FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
                         StreamWriter sw = new StreamWriter(fas);
-                        sw.Write(s.Replace(line+"\r\n",""));
+                        sw.Write(s.Replace(line + "\r\n", ""));
                         sw.Flush();
                         sw.Close();
                         fas.Close();
-                    }              
+                    }
                 }
             }
-            thread = new Thread(DeleteLog);
-            thread.Start();
-            TestPrint = new Thread(TestPrintEnable);
-            TestPrint.Start();
-            this.Tag = "ShowDialogWindow";
-            this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
-            this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
-            this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
-            this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
-            this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
-            //设置底部栏目的基础信息
-            inf_name.Text = User.UserName;
-            //inf_position.Text = User.UserPosition;
-            Inf_linecode.Text = User.UserLineCode;
-            inf_source.Text = User.UserSourceCode;
-            inf_db.Text = SystemInf.CurrentDB;
-            inf_currentstep.Text = User.CurrentStepName;
-            //设置获取当前屏幕大小自动全屏但是保留任务栏
-            Rectangle ScreenArea = Screen.GetWorkingArea(this);
-            Top = 0;
-            Left = 0;
-            Width = ScreenArea.Width;
-            Height = ScreenArea.Height;
-            SetForm();
-            //设置导航栏宽度
-            SystemInf.NavWidth = Menu.Width;
-            SystemInf.HeadBarHeight = headBar1.Height;
         }
 
         private void TestPrintEnable()
@@ -313,6 +319,37 @@ namespace UAS_MES
 
         private void Main_FormClosing(object sender, FormClosingEventArgs e)
         {
+            //杀死全部未关闭的打印进程
+            string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
+            foreach (string line in lines)
+            {
+                if (line != "")
+                {
+                    string processid = line.Split('|')[0];
+                    string lblid = line.Split('|')[1];
+                    try
+                    {
+                        if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
+                        {
+                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
+                            FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
+                            StreamReader sr = new StreamReader(fs);
+                            String s = sr.ReadToEnd();
+                            sr.Close();
+                            fs.Close();
+                            FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
+                            StreamWriter sw = new StreamWriter(fas);
+                            sw.Write(s.Replace(line + "\r\n", ""));
+                            sw.Flush();
+                            sw.Close();
+                            fas.Close();
+                        }
+                    }
+                    catch (Exception)
+                    {
+                    }
+                }
+            }
             BaseUtil.FormStepInOrOut(this, false);
         }
     }