From 15d5bb58e4541c180078c11fe84e10f4ca8e8922 Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 6 Nov 2012 06:11:37 +0000 Subject: [PATCH] Fixes: Catching an outlook exception that can be ignored. Making sure that the window to be captured is active, so the colors are right. Also a little cleanup, and added FindWindow to the User32.cs so we can use this to find Metro (Modern UI) applications, this still needs to be added to the WindowsHelper.cs git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2244 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- .../OfficeExport/OutlookEmailExporter.cs | 10 +++++++--- GreenshotPlugin/Core/WindowsHelper.cs | 20 +++++++++---------- GreenshotPlugin/UnmanagedHelpers/User32.cs | 11 ++++++---- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs b/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs index 9bb4f60b8..1004ea439 100644 --- a/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs +++ b/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs @@ -375,12 +375,16 @@ namespace Greenshot.Interop.Office { newMail.GetInspector().Activate(); LOG.InfoFormat("Managed to display the message."); return; - } catch (Exception) { - LOG.WarnFormat("Retrying to show email in {0} seconds... Retries left: {1}", retryInXSeconds, retries); + } catch (Exception displayEx) { + LOG.WarnFormat("Error displaying message: {0}, retrying to show email in {1} seconds... Retries left: {2}", displayEx, retryInXSeconds, retries); } } LOG.WarnFormat("Retry failed, saving message to draft."); - newMail.Save(); + try { + newMail.Save(); + } catch (Exception saveEx) { + LOG.WarnFormat("Saving message to draft failed: {0}", saveEx); + } }); retryDisplayEmail.Name = "Retry to display email"; retryDisplayEmail.IsBackground = true; diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index 1851618a0..aa1098e6b 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -544,11 +544,7 @@ namespace GreenshotPlugin.Core { return User32.IsIconic(this.hWnd) || Location.X <= -32000; } set { - User32.SendMessage( - this.hWnd, - User32.WM_SYSCOMMAND, - (IntPtr)User32.SC_MINIMIZE, - IntPtr.Zero); + User32.SendMessage(this.hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MINIMIZE, IntPtr.Zero); } } @@ -560,11 +556,7 @@ namespace GreenshotPlugin.Core { return User32.IsZoomed(this.hWnd); } set { - User32.SendMessage( - this.hWnd, - User32.WM_SYSCOMMAND, - (IntPtr)User32.SC_MAXIMIZE, - IntPtr.Zero); + User32.SendMessage(this.hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_MAXIMIZE, IntPtr.Zero); } } @@ -700,7 +692,7 @@ namespace GreenshotPlugin.Core { /// public void Restore() { if (Iconic) { - User32.SendMessage(this.hWnd, User32.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero); + User32.SendMessage(this.hWnd, (int)WindowsMessages.WM_SYSCOMMAND, (IntPtr)User32.SC_RESTORE, IntPtr.Zero); } User32.BringWindowToTop(this.hWnd); User32.SetForegroundWindow(this.hWnd); @@ -871,6 +863,9 @@ namespace GreenshotPlugin.Core { tempForm.BackColor = Color.Black; // Make sure everything is visible tempForm.Refresh(); + // Make sure the application window is active, so the colors & buttons are right + ToForeground(); + // Make sure all changes are processed and visisble Application.DoEvents(); using (Bitmap blackBitmap = WindowCapture.CaptureRectangle(captureRectangle)) { capturedBitmap = ApplyTransparency(blackBitmap, whiteBitmap); @@ -898,6 +893,9 @@ namespace GreenshotPlugin.Core { } // Make sure everything is visible tempForm.Refresh(); + // Make sure the application window is active, so the colors & buttons are right + ToForeground(); + // Make sure all changes are processed and visisble Application.DoEvents(); // Capture from the screen capturedBitmap = WindowCapture.CaptureRectangle(captureRectangle); diff --git a/GreenshotPlugin/UnmanagedHelpers/User32.cs b/GreenshotPlugin/UnmanagedHelpers/User32.cs index c6c846c29..886e5e808 100644 --- a/GreenshotPlugin/UnmanagedHelpers/User32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/User32.cs @@ -264,9 +264,6 @@ namespace GreenshotPlugin.UnmanagedHelpers { /// User32 Wrappers /// public class User32 { - public const int WM_COMMAND = 0x111; - public const int WM_SYSCOMMAND = 0x112; - public const int SC_RESTORE = 0xF120; public const int SC_CLOSE = 0xF060; public const int SC_MAXIMIZE = 0xF030; @@ -409,7 +406,13 @@ namespace GreenshotPlugin.UnmanagedHelpers { public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer); [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext); - + + // Added for finding Metro apps + [DllImport("user32", SetLastError = true)] + public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); + [DllImport("user32", SetLastError = true)] + public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); + /// uiFlags: 0 - Count of GDI objects /// uiFlags: 1 - Count of USER objects /// - Win32 GDI objects (pens, brushes, fonts, palettes, regions, device contexts, bitmap headers)