Binarysharp.MemoryManagement.Windows.WindowCore.GetWindowText C# (CSharp) Method

GetWindowText() public static method

Gets the text of the specified window's title bar.
public static GetWindowText ( IntPtr windowHandle ) : string
windowHandle System.IntPtr A handle to the window containing the text.
return string
        public static string GetWindowText(IntPtr windowHandle)
        {
            // Check if the handle is valid
            HandleManipulator.ValidateAsArgument(windowHandle, "windowHandle");

            // Get the size of the window's title
            var capacity = NativeMethods.GetWindowTextLength(windowHandle);
            // If the window doesn't contain any title
            if (capacity == 0)
                return string.Empty;

            // Get the text of the window's title bar text
            var stringBuilder = new StringBuilder(capacity + 1);
            if (NativeMethods.GetWindowText(windowHandle, stringBuilder, stringBuilder.Capacity) == 0)
                throw new Win32Exception("Couldn't get the text of the window's title bar or the window has no title.");

            return stringBuilder.ToString();
        }

Usage Example

Example #1
0
 /// <summary>
 /// Gets all the windows that contain the specified title.
 /// </summary>
 /// <param name="windowTitle">A part a window title string.</param>
 /// <returns>A collection of <see cref="RemoteWindow"/>.</returns>
 public IEnumerable <RemoteWindow> GetWindowsByTitleContains(string windowTitle)
 {
     return(WindowHandles
            .Where(handle => WindowCore.GetWindowText(handle).Contains(windowTitle))
            .Select(handle => new RemoteWindow(_memorySharp, handle)));
 }