Find the hWnd of the active control systemwide

It's easy to get the ActiveControl.hWnd within your project, but if you need the active control of any window outside your project you have to use the API.

This will start by getting the hWnd of the forground window (even if not one of your windows) using the API GetForegroundWindow. From there we need to "attach" the foreign thread to our own using AttachThreadInput, but we need to GetWindowThreadProcessId first to use in that function. Now GetFocus will include the foreign window when it gives us an hWnd.

Code to Add to a .bas Module