|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +namespace Rubberduck.UI |
| 9 | +{ |
| 10 | + [ComVisible(false)] |
| 11 | + /// <summary> Collection of WinAPI methods and extensions to handle native windows. </summary> |
| 12 | + // Special Thank You to Carlos Quintero for supplying the project with the original code this file is based on. |
| 13 | + public static class NativeWindowMethods |
| 14 | + { |
| 15 | + /// <summary> Sends a message to the OS. </summary> |
| 16 | + /// |
| 17 | + /// <param name="hWnd"> The window handle. </param> |
| 18 | + /// <param name="wMsg"> The message. </param> |
| 19 | + /// <param name="wParam"> The parameter. </param> |
| 20 | + /// <param name="lParam"> The parameter. </param> |
| 21 | + /// <returns> An IntPtr handle. </returns> |
| 22 | + [DllImport("user32", EntryPoint = "SendMessageW", ExactSpelling = true)] |
| 23 | + public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); |
| 24 | + |
| 25 | + /// <summary> EnumChildWindows delegate. </summary> |
| 26 | + /// |
| 27 | + /// <param name="hwnd"> Main Window Handle</param> |
| 28 | + /// <param name="lParam"> Application defined parameter. Unused. </param> |
| 29 | + /// <returns> An int. </returns> |
| 30 | + public delegate int EnumChildWindowsDelegate(IntPtr hwnd, IntPtr lParam); |
| 31 | + |
| 32 | + /// <summary> WinAPI method to Enumerate Child Windows </summary> |
| 33 | + /// |
| 34 | + /// <param name="parentWindowHandle"> Handle of the parent window. </param> |
| 35 | + /// <param name="lpEnumFunction"> The enum delegate function. </param> |
| 36 | + /// <param name="lParam"> The parameter. </param> |
| 37 | + /// <returns> An int. </returns> |
| 38 | + [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Unicode)] |
| 39 | + public static extern int EnumChildWindows(IntPtr parentWindowHandle, EnumChildWindowsDelegate lpEnumFunction, IntPtr lParam); |
| 40 | + |
| 41 | + /// <summary> Gets window text. </summary> |
| 42 | + /// |
| 43 | + /// <param name="hWnd"> The window handle. </param> |
| 44 | + /// <param name="lpString"> The return string. </param> |
| 45 | + /// <param name="nMaxCount"> Number of maximums. </param> |
| 46 | + /// <returns> Integer Success Code </returns> |
| 47 | + [DllImport("user32", EntryPoint = "GetWindowTextW", ExactSpelling = true, CharSet = CharSet.Unicode)] |
| 48 | + public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); |
| 49 | + |
| 50 | + /// <summary> Gets the parent window of this item. </summary> |
| 51 | + /// |
| 52 | + /// <param name="hWnd"> The window handle. </param> |
| 53 | + /// <returns> The parent window IntPtr handle. </returns> |
| 54 | + [DllImport("User32.dll")] |
| 55 | + public static extern IntPtr GetParent(IntPtr hWnd); |
| 56 | + |
| 57 | + /// <summary> Gets window caption text by handle. </summary> |
| 58 | + /// |
| 59 | + /// <param name="windowHandle"> Handle of the window to be activated. </param> |
| 60 | + /// <returns> The window caption text. </returns> |
| 61 | + public static string GetWindowTextByHwnd(IntPtr windowHandle) |
| 62 | + { |
| 63 | + const int MAX_BUFFER = 300; |
| 64 | + |
| 65 | + StringBuilder bufferStringBuilder = null; |
| 66 | + int charactersCount = 0; |
| 67 | + string result = null; |
| 68 | + |
| 69 | + bufferStringBuilder = new StringBuilder(MAX_BUFFER + 1); |
| 70 | + |
| 71 | + charactersCount = GetWindowText(windowHandle, bufferStringBuilder, MAX_BUFFER); |
| 72 | + if (charactersCount > 0) |
| 73 | + { |
| 74 | + result = bufferStringBuilder.ToString(0, charactersCount); |
| 75 | + } |
| 76 | + |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary>Activates the window by simulating a click.</summary> |
| 81 | + /// |
| 82 | + /// <param name="windowHandle"> Handle of the window to be activated. </param> |
| 83 | + /// <param name="parentWindowHandle"> Handle of the parent window. </param> |
| 84 | + public static void ActivateWindow(IntPtr windowHandle, IntPtr parentWindowHandle) |
| 85 | + { |
| 86 | + const int WM_MOUSEACTIVATE = 0x21; |
| 87 | + const int HTCAPTION = 2; |
| 88 | + const int WM_LBUTTONDOWN = 0x201; |
| 89 | + |
| 90 | + SendMessage(windowHandle, WM_MOUSEACTIVATE, parentWindowHandle, new IntPtr(HTCAPTION + WM_LBUTTONDOWN * 0x10000)); |
| 91 | + } |
| 92 | + |
| 93 | + internal static void EnumChildWindows(IntPtr parentWindowHandle, EnumChildWindowsDelegate callBackEnumWindows) |
| 94 | + { |
| 95 | + int result; |
| 96 | + |
| 97 | + result = EnumChildWindows(parentWindowHandle, callBackEnumWindows, IntPtr.Zero); |
| 98 | + |
| 99 | + if (result != 0) |
| 100 | + { |
| 101 | + System.Diagnostics.Debug.WriteLine("EnumChildWindows failed"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + internal class ChildWindowFinder |
| 106 | + { |
| 107 | + private IntPtr _resultHandle = IntPtr.Zero; |
| 108 | + private string _caption; |
| 109 | + |
| 110 | + internal ChildWindowFinder(string caption) |
| 111 | + { |
| 112 | + _caption = caption; |
| 113 | + } |
| 114 | + |
| 115 | + public int EnumWindowsProcToChildWindowByCaption(IntPtr windowHandle, IntPtr param) |
| 116 | + { |
| 117 | + string caption; |
| 118 | + int result; |
| 119 | + |
| 120 | + // By default it will continue enumeration after this call |
| 121 | + result = 1; |
| 122 | + |
| 123 | + caption = NativeWindowMethods.GetWindowTextByHwnd(windowHandle); |
| 124 | + |
| 125 | + |
| 126 | + if (_caption == caption) |
| 127 | + { |
| 128 | + // Found |
| 129 | + _resultHandle = windowHandle; |
| 130 | + |
| 131 | + // Stop enumeration after this call |
| 132 | + result = 0; |
| 133 | + } |
| 134 | + return result; |
| 135 | + } |
| 136 | + |
| 137 | + public IntPtr ResultHandle |
| 138 | + { |
| 139 | + get |
| 140 | + { |
| 141 | + return _resultHandle; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments