diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-16 13:57:15 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-16 13:57:15 +0300 |
| commit | 5dba4e859e2806ce74d806fbd5dda37b7fa628d5 (patch) | |
| tree | 4e5bf391476a7dfc2c1cd13d020a2e581bd7e032 /Software/Visual_Studio/VSIX/Tango.BuildExtensions/WindowInfo.cs | |
| parent | fe19ef2694d92c91a493b9f3d1f41bc57ebbe6a2 (diff) | |
| download | Tango-5dba4e859e2806ce74d806fbd5dda37b7fa628d5.tar.gz Tango-5dba4e859e2806ce74d806fbd5dda37b7fa628d5.zip | |
Improved Tango Build Engine VS Extension.
Added HardwareSpeedSensor to DB Entities & PMR.
Added progress to proto folder compiler.
Diffstat (limited to 'Software/Visual_Studio/VSIX/Tango.BuildExtensions/WindowInfo.cs')
| -rw-r--r-- | Software/Visual_Studio/VSIX/Tango.BuildExtensions/WindowInfo.cs | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/Software/Visual_Studio/VSIX/Tango.BuildExtensions/WindowInfo.cs b/Software/Visual_Studio/VSIX/Tango.BuildExtensions/WindowInfo.cs new file mode 100644 index 000000000..535ee7ce1 --- /dev/null +++ b/Software/Visual_Studio/VSIX/Tango.BuildExtensions/WindowInfo.cs @@ -0,0 +1,150 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using TestStack.White.InputDevices; +using TestStack.White.WindowsAPI; + +namespace Tango.BuildExtensions +{ + public class WindowInfo + { + [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] + static extern IntPtr GetDesktopWindow(); + + [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] + static extern int SendMessage(IntPtr hwndControl, uint Msg, int wParam, StringBuilder strBuffer); // get text + + [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] + static extern int SendMessage(IntPtr hwndControl, uint Msg, int wParam, int lParam); // text length + + [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)] + static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); + + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); + + private static List<IntPtr> GetAllChildrenHandles(IntPtr hParent, int maxCount) + { + List<IntPtr> result = new List<IntPtr>(); + int ct = 0; + IntPtr prevChild = IntPtr.Zero; + IntPtr currChild = IntPtr.Zero; + while (true && ct < maxCount) + { + currChild = FindWindowEx(hParent, prevChild, null, null); + if (currChild == IntPtr.Zero) break; + result.Add(currChild); + prevChild = currChild; + ++ct; + } + return result; + } + + private static int GetCaptionTextLength(IntPtr hTextBox) + { + // helper for GetCaptionText + uint WM_GETTEXTLENGTH = 0x000E; + int result = SendMessage(hTextBox, WM_GETTEXTLENGTH, + 0, 0); + return result; + } + + private static string GetCaptionText(IntPtr hTextBox) + { + uint WM_GETTEXT = 0x000D; + int len = GetCaptionTextLength(hTextBox); + if (len <= 0) return null; // no text. consider empty string instead. + StringBuilder sb = new StringBuilder(len + 1); + SendMessage(hTextBox, WM_GETTEXT, len + 1, sb); + return sb.ToString(); + } + + public IntPtr Handle { get; set; } + public int Level { get; set; } + public String Caption { get; set; } + public WindowInfo Parent { get; set; } + public List<WindowInfo> Children { get; set; } + + public WindowInfo() + { + Children = new List<WindowInfo>(); + } + + public void SetActive() + { + SetForegroundWindow(Handle); + } + + public void PressKey(KeyboardInput.SpecialKeys key) + { + SetActive(); + Keyboard.Instance.PressSpecialKey(key); + } + + public override string ToString() + { + return Caption; + } + + public static List<WindowInfo> GetAllWindows() + { + List<WindowInfo> windows = new List<WindowInfo>(); + + IntPtr hDesktop = GetDesktopWindow(); + WindowInfo desktop = new WindowInfo(); + desktop.Handle = hDesktop; + desktop.Level = 0; + desktop.Caption = "(Desktop)"; + desktop.Parent = null; + windows.Add(desktop); + + FillWindows(desktop, desktop.Children); + + return windows; + } + + private static void FlattenWindows(WindowInfo parent, List<WindowInfo> windows) + { + windows.Add(parent); + + foreach (var window in parent.Children) + { + windows.Add(window); + FlattenWindows(window, windows); + } + } + + public static WindowInfo GetWindow(String title) + { + var windows = GetAllWindows(); + + List<WindowInfo> flatList = new List<WindowInfo>(); + FlattenWindows(windows.First(), flatList); + + return flatList.FirstOrDefault(x => x.Caption == title); + } + + private static void FillWindows(WindowInfo parent, List<WindowInfo> windows) + { + foreach (var handle in GetAllChildrenHandles(parent.Handle, 5000)) + { + WindowInfo child = new WindowInfo(); + child.Handle = handle; + child.Level = parent.Level + 1; + child.Caption = GetCaptionText(handle); + child.Parent = parent; + + if (child.Caption != null) + { + windows.Add(child); + } + + FillWindows(child, child.Children); + } + } + } +} |
