Created
March 4, 2016 06:26
-
-
Save kakopappa/4598295677ac13805937 to your computer and use it in GitHub Desktop.
How to get the parent window title using child window handle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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