Set foreground of child window in Visual Studio

youki 996 Reputation points
2020-12-15T11:10:12.35+00:00

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 pinvoke.net (sample code 2 -https://www.pinvoke.net/default.aspx/user32.enumchildwindows) 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;
}
C#
C#
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
    2020-12-16T18:46:10.977+00:00

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