Last active
August 29, 2015 13:56
-
-
Save iamralch/9072008 to your computer and use it in GitHub Desktop.
Window Handle Info
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
public class WindowHandleInfo | |
{ | |
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam); | |
[DllImport("user32")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); | |
private IntPtr _MainHandle; | |
public WindowHandleInfo(IntPtr handle) | |
{ | |
this._MainHandle = handle; | |
} | |
public List<IntPtr> GetAllChildHandles() | |
{ | |
List<IntPtr> childHandles = new List<IntPtr>(); | |
GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles); | |
IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); | |
try | |
{ | |
EnumWindowProc childProc = new EnumWindowProc(EnumWindow); | |
EnumChildWindows(this._MainHandle, childProc, pointerChildHandlesList); | |
} | |
finally | |
{ | |
gcChildhandlesList.Free(); | |
} | |
return childHandles; | |
} | |
private bool EnumWindow(IntPtr hWnd, IntPtr lParam) | |
{ | |
GCHandle gcChildhandlesList = GCHandle.FromIntPtr(lParam); | |
if (gcChildhandlesList == null || gcChildhandlesList.Target == null) | |
{ | |
return false; | |
} | |
List<IntPtr> childHandles = gcChildhandlesList.Target as List<IntPtr>; | |
childHandles.Add(hWnd); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment