Example #1
0
        public ApplicationsControl()
        {
            InitializeComponent();

            this.DataContext  = this;
            this.Applications = User32Helper.GetDesktopWindows();
        }
Example #2
0
 /// <summary>
 /// Pretplata na događaj koji je pozvan nakon što se kontrola učita
 /// Svaki put kad promijenimo tab, ažuriramo jednostavno listu.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void UserControl_Loaded(object sender, RoutedEventArgs e)
 {
     this.Applications.Clear(); // Očisti trenutnu listu i dodaj novu.
     foreach (var application in User32Helper.GetDesktopWindows())
     {
         this.Applications.Add(application);
     }
 }
Example #3
0
        private void cmdCloseAll_Click(object sender, EventArgs e)
        {
            Process      lprocess;
            int          ltabs;
            DialogResult lDR;
            uint         exitCode;

            Shell32.Shell shell;
            String        lstrProcessServiceName;
            bool          lblnFoundServiceName;

            ServiceController[] scServices;
            var     ldesktopPrograms = new List <DesktopWindow>();
            uint    pid;
            uint    temp = 0;
            Process tempProcess;

            lDR = MessageBox.Show("You Are About To Close All Active Programs.\n Continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (lDR == System.Windows.Forms.DialogResult.No)
            {
                return; //get out, we don't want to continue
            }

            //shell = new Shell32.Shell();
            //scServices = ServiceController.GetServices();

            lprocess = Process.GetCurrentProcess();

            ldesktopPrograms = User32Helper.GetDesktopWindows();

            //go thru each window, if it's true and has a title then try to kill it
            for (int i = 0; i < ldesktopPrograms.Count; i++)
            {
                if (ldesktopPrograms[i].IsVisible && ldesktopPrograms[i].Title.Trim() != "")
                {
                    pid = GetWindowThreadProcessId(ldesktopPrograms[i].Handle, out temp);
                    try
                    {
                        tempProcess = Process.GetProcessById((int)temp);
                        if (tempProcess.Id != lprocess.Id && tempProcess.MainWindowHandle != IntPtr.Zero && tempProcess.ProcessName != "explorer")
                        {
                            try
                            {
                                tempProcess.CloseMainWindow();
                                System.Threading.Thread.Sleep(Globals.mintTimeout);
                            }

                            catch (InvalidOperationException err) { tempProcess.Kill(); }

                            catch (Win32Exception err) { tempProcess.Kill(); }

                            catch (Exception err)
                            {
                                tempProcess.Kill(); //no error reporting just kill it
                            }

                            //if we haven't thrown an error and the program still hasn't closed, kill it
                            if (!tempProcess.HasExited)
                            {
                                tempProcess.Kill();
                                System.Threading.Thread.Sleep(Globals.mintTimeout);
                            }
                        }
                    }
                    catch (System.ArgumentException err)
                    {
                        //MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //just skip this ID
                    }
                }
            }

            System.Threading.Thread.Sleep(Globals.mintTimeout);
            ExitWindowsEx(0, 0);
            this.Dispose();
            return;

            /*
             *
             * Dead Code Below
             * First attempt at gracefully exiting programs
             */

            foreach (Process p in Process.GetProcesses(System.Environment.MachineName))
            {
                if ((p.Id != lprocess.Id && p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer"))//make sure we aren't closing this program and that the current process has a GUI window
                {
                    try
                    {
                        p.CloseMainWindow();
                        System.Threading.Thread.Sleep(Globals.mintTimeout);
                    }

                    catch (InvalidOperationException err) { p.Kill(); }

                    catch (Win32Exception err) { p.Kill(); }

                    catch (Exception err)
                    {
                        //MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        p.Kill(); //screw error reporting just kill it
                    }

                    //}
                    if (!p.HasExited)
                    {
                        p.WaitForExit(Globals.mintTimeout * 2);

                        try
                        {
                            GetExitCodeProcess(p.Handle, out exitCode);
                            {
                                if (exitCode != 0)
                                {
                                    p.Kill();
                                }
                            }
                        }
                        catch (Win32Exception err) { p.Kill(); }

                        catch (InvalidOperationException err) { p.Kill(); }

                        catch (Exception err) { p.Kill(); }

                        System.Threading.Thread.Sleep(Globals.mintTimeout);
                        ltabs = 0;

                        /*while (!p.HasExited) //means that most likely we have a popup window asking if we are sure we want to exit
                         * {                    //try enter if control is on the yes button
                         *  //else tab a few times in case the control doesn't default to the yes button
                         *  TryTabbing(ltabs);
                         *  SendKeys.Send("{ENTER}");
                         *  System.Threading.Thread.Sleep(Globals.mintTimeout);
                         *
                         *  ltabs++;
                         *
                         *  if (ltabs > 3) //we've tried closing the program nicely a few times, just kill it at this point ¯\_(ツ)_/¯
                         *  {
                         *      try { p.Kill(); }
                         *      catch (Exception err) { MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                         *      System.Threading.Thread.Sleep(Globals.mintTimeout);
                         *      break;
                         *  }
                         *  try { p.CloseMainWindow(); }
                         *  catch (Exception err) { p.Kill(); }// MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                         *  System.Threading.Thread.Sleep(Globals.mintTimeout);
                         * }*/
                    }
                }
            }
            System.Threading.Thread.Sleep(Globals.mintTimeout);
            this.Dispose();
        }