C#'s GetForegroundWindow()
returns the same result for multiple windows, EnumWindows
does not really return that window at all. Each process really has its own tier.
I've also went through both of the GetWindowThreadProcessId()
functions and enumerated through them with GetChildWindows()
, but still they don't return the same window as GetForegroundWindow()
.
How do you properly start with GetForegroundWindow()
and turn it into what you'd retrieve from a proper EnumWindows
?
Goal: GetForegroundWindow
+ enumerate properly to retrieve proper top-level handles (all tabs in chrome, your project form(s)), but without having to sift through the mess GetAllWindows()
brings you. Thanks.
private ArrayList GetAllWindows()
{
var windowHandles = new ArrayList();
EnumedWindow callBackPtr = GetWindowHandle;
EnumWindows(callBackPtr, windowHandles);
foreach (IntPtr windowHandle in windowHandles.ToArray())
{
EnumChildWindows(windowHandle, callBackPtr, windowHandles);
}
return windowHandles;
}
private delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumedWindow lpEnumFunc, ArrayList lParam);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumedWindow callback, ArrayList lParam);
List<IntPtr> ids = new List<IntPtr>();
private bool GetWindowHandle(IntPtr windowHandle, ArrayList windowHandles)
{
windowHandles.Add(windowHandle);
listBox1.Items.Add(windowHandle);
//ids.Add(GetWindowThreadProcessId(windowHandle, IntPtr.Zero));
return true;
}