diff --git a/Greenshot/Helpers/CaptureHelper.cs b/Greenshot/Helpers/CaptureHelper.cs index 9cfc3a665..3351db1d8 100644 --- a/Greenshot/Helpers/CaptureHelper.cs +++ b/Greenshot/Helpers/CaptureHelper.cs @@ -844,7 +844,7 @@ namespace Greenshot.Helpers { // Restore the window making sure it's visible! windowToCapture.Restore(); } else { - windowToCapture.ToForeground(); + windowToCapture.ToForeground(false); } tmpCapture = windowToCapture.CaptureGdiWindow(captureForWindow); if (tmpCapture != null) { @@ -946,7 +946,7 @@ namespace Greenshot.Helpers { _capture.CaptureDetails.DpiY = graphics.DpiY; } // Set previouslyActiveWindow as foreground window - previouslyActiveWindow?.ToForeground(); + previouslyActiveWindow?.ToForeground(false); if (_capture.CaptureDetails != null) { ((Bitmap) _capture.Image)?.SetResolution(_capture.CaptureDetails.DpiX, _capture.CaptureDetails.DpiY); } diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index a33601115..90e6e6767 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -1250,11 +1250,13 @@ namespace GreenshotPlugin.Core { size = result ? new Size((int)windowInfo.cxWindowBorders, (int)windowInfo.cyWindowBorders) : Size.Empty; return result; } - + /// /// Set the window as foreground window /// - public static void ToForeground(IntPtr handle) + /// hWnd of the window to bring to the foreground + /// bool with true to use a trick to really bring the window to the foreground + public static void ToForeground(IntPtr handle, bool workaround = true) { // Do nothing if the window is already in the foreground if (User32.GetForegroundWindow() == handle) @@ -1266,10 +1268,14 @@ namespace GreenshotPlugin.Core { const int EXTENDEDKEY = 0x1; const int KEYUP = 0x2; - // Simulate an "ALT" key press. - User32.keybd_event(ALT, 0x45, EXTENDEDKEY | 0, 0); - // Simulate an "ALT" key release. - User32.keybd_event(ALT, 0x45, EXTENDEDKEY | KEYUP, 0); + // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx + if (workaround) + { + // Simulate an "ALT" key press. + User32.keybd_event(ALT, 0x45, EXTENDEDKEY | 0, 0); + // Simulate an "ALT" key release. + User32.keybd_event(ALT, 0x45, EXTENDEDKEY | KEYUP, 0); + } // Show window in forground. User32.SetForegroundWindow(handle); @@ -1278,8 +1284,9 @@ namespace GreenshotPlugin.Core { /// /// Set the window as foreground window /// - public void ToForeground() { - ToForeground(Handle); + /// true to use a workaround, otherwise the window might only flash + public void ToForeground(bool workaround = true) { + ToForeground(Handle, workaround); } ///