Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry SetForegroundWindow with DTE.MainWindow #57314

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ public void ActivateMainWindow()

var activeVisualStudioWindow = dte.ActiveWindow.HWnd;
Debug.WriteLine($"DTE.ActiveWindow.HWnd = {activeVisualStudioWindow}");

if (activeVisualStudioWindow == IntPtr.Zero)
if (activeVisualStudioWindow != IntPtr.Zero)
{
activeVisualStudioWindow = dte.MainWindow.HWnd;
Debug.WriteLine($"DTE.MainWindow.HWnd = {activeVisualStudioWindow}");
if (IntegrationHelper.TrySetForegroundWindow(activeVisualStudioWindow))
return;
}

IntegrationHelper.SetForegroundWindow(activeVisualStudioWindow);
activeVisualStudioWindow = dte.MainWindow.HWnd;
Debug.WriteLine($"DTE.MainWindow.HWnd = {activeVisualStudioWindow}");
if (!IntegrationHelper.TrySetForegroundWindow(activeVisualStudioWindow))
throw new InvalidOperationException("Failed to set the foreground window.");
});

public int GetErrorListErrorCount()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static void KillProcess(string processName)
}
}

public static void SetForegroundWindow(IntPtr window)
public static bool TrySetForegroundWindow(IntPtr window)
{
var activeWindow = NativeMethods.GetLastActivePopup(window);
activeWindow = NativeMethods.IsWindowVisible(activeWindow) ? activeWindow : window;
Expand Down Expand Up @@ -213,9 +213,11 @@ public static void SetForegroundWindow(IntPtr window)

if (!NativeMethods.SetForegroundWindow(activeWindow))
{
throw new InvalidOperationException("Failed to set the foreground window.");
return false;
}
}

return true;
}

public static void SendInput(NativeMethods.INPUT[] inputs)
Expand Down