Set foreground of child window in Visual Studio

youki 996 Reputation points

I want to set the foreground of a window in Visual Studio.

What i've tried:

When i have 2 windows "docked" side by side i can't set the foreground of a window under the mouse in VS. I'm using GetCursorPos and WindowFromPoint which usually works for standard windows. I also tried to use EnumChildWindows from (sample code 2 - but it returns 0 when i pass the WindowFromPoint or MainWindowHandle of the process.

I tested it with Chrome and Remote Desktop Manager which is the only application that i have with docked windows and it works, it doesn't return 0.
Could it be that Microsoft is blocking it intentionally?
If it would work, i have to recognize the right windows under the cursor and to use SetForeground. Maybe it's not the right approach?

Best regards

To get the window handles:

public static IntPtr GetProcessMainWindowHandle()
    Process process = null;

    if (GetCursorPos(out Point point))
        IntPtr hWnd = WindowFromPoint(point);

        GetWindowThreadProcessId(hWnd, out uint pid);
        process = Process.GetProcessById((int)pid);

    return process.MainWindowHandle;

public static IntPtr GetHandle()
    IntPtr hWnd = IntPtr.Zero;

    if (GetCursorPos(out Point point))
        hWnd = WindowFromPoint(point);

    return hWnd;
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,215 questions
{count} votes

1 answer

Sort by: Most helpful
  1. youki 996 Reputation points

    Thanks for the support, Timon.
    I've got the answer from Stackoverflow.
    It's a WPF application that's the reason.