From d9a06d63eace8d1cf92ee6cb81f59c3117e76da7 Mon Sep 17 00:00:00 2001 From: elmanuelito Date: Wed, 13 Jan 2016 21:32:17 +0100 Subject: [PATCH] _adding some dev files --- _dev/ListWindows.cs | 17 ++++++++++++++++ _dev/SwitchWindow.cs | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 _dev/ListWindows.cs create mode 100644 _dev/SwitchWindow.cs diff --git a/_dev/ListWindows.cs b/_dev/ListWindows.cs new file mode 100644 index 0000000..7b97c83 --- /dev/null +++ b/_dev/ListWindows.cs @@ -0,0 +1,17 @@ +using System.IO; +using System; +using System.Diagnostics; +class Program +{ + static void Main() + { + Process[] processlist = Process.GetProcesses(); + foreach (Process process in processlist) + { + if (!String.IsNullOrEmpty(process.MainWindowTitle)) + { + Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle); + } + } + } +} diff --git a/_dev/SwitchWindow.cs b/_dev/SwitchWindow.cs new file mode 100644 index 0000000..6f909be --- /dev/null +++ b/_dev/SwitchWindow.cs @@ -0,0 +1,48 @@ +//declarations +using system.IO; +using System.Runtime.InteropServices; +using System.Diagnostics; +//more + +//namespace here + +//class here + +//initialize method + +class Program +{ + static void Main() + { + //related .dll import + [DllImport("user32.dll")] + public static extern void SwitchToThisWindow(IntPtr hWnd); + + String ProcWindow = "main"; + //function which calls switchWindow() is here but not important + + Process[] procs = Process.GetProcessesByName(ProcWindow); + foreach (Process proc in procs) + { + //switch to process by name + SwitchToThisWindow(proc.MainWindowHandle); + + Console.WriteLine("Process: {0} ID: {1} Window title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle); + } + } +} +} + + +// -------------------------------------------------------------------------------- +// --- +// -------------------------------------------------------------------------------- +http://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application +// -------------------------------------------------------------------------------- +// --- +// -------------------------------------------------------------------------------- +AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle); +if (element != null) +{ + element.SetFocus(); +}