Home
Manage Your Code
Snippet: Get the title text of an open window (C#)
Title: Get the title text of an open window Language: C#
Description: Given the handle of an open window, returns the text of the title. Views: 185
Author: Matt Schmidt Date Added: 8/15/2008
Copy Code  
1        /// <summary>
2        /// Gets the title text of a window.
3        /// </summary>
4        /// <param name="hwnd">Window handle</param>
5        /// <returns>Title text of window.</returns>
6        private String GetWindowTitle(int hwnd)
7        {
8            int length = GetWindowTextLength((IntPtr)hwnd);
9            StringBuilder windowTitle = new StringBuilder(length + 1);
10            GetWindowText((IntPtr)hwnd, windowTitle, windowTitle.Capacity);
11            return windowTitle.ToString();
12        }
13
14        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
15        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
16
17        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
18        public static extern int GetWindowTextLength(IntPtr hWnd);
Notes
Make sure you include the win32 wrappers at the bottom.