Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created March 4, 2016 06:26
Show Gist options
  • Save kakopappa/4598295677ac13805937 to your computer and use it in GitHub Desktop.
Save kakopappa/4598295677ac13805937 to your computer and use it in GitHub Desktop.
How to get the parent window title using child window handle
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
public static String GetWindowTitle(IntPtr handle) {
IntPtr windowParent = IntPtr.Zero;
while (handle != IntPtr.Zero) {
windowParent = handle;
handle = GetParent(handle);
}
int length = GetWindowTextLength(hWnd);
StringBuilder text = new StringBuilder(length + 1);
GetWindowText(hWnd, text, text.Capacity);
return text.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment