Skip to content

Commit

Permalink
Retry SetForegroundWindow with DTE.MainWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Oct 21, 2021
1 parent 7a16990 commit f3344d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ public void ActivateMainWindow()
{
var dte = GetDTE();

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

if (activeVisualStudioWindow == IntPtr.Zero)
if (activeVisualStudioWindow != IntPtr.Zero)
{
activeVisualStudioWindow = (IntPtr)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

0 comments on commit f3344d5

Please sign in to comment.