From 8a3fd911bfe4153605ef092e8cada51b1ebbbdac Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 8 Jan 2013 11:13:18 +0000 Subject: [PATCH] Fixed a NullReference and changed the way we read the icon for the windows in the List. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2419 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- GreenshotPlugin/Core/ClipboardHelper.cs | 2 +- GreenshotPlugin/Core/WindowsHelper.cs | 43 +++++++++++++++++++++- GreenshotPlugin/UnmanagedHelpers/User32.cs | 20 +++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/GreenshotPlugin/Core/ClipboardHelper.cs b/GreenshotPlugin/Core/ClipboardHelper.cs index a348599f6..44ca8b023 100644 --- a/GreenshotPlugin/Core/ClipboardHelper.cs +++ b/GreenshotPlugin/Core/ClipboardHelper.cs @@ -297,7 +297,7 @@ EndSelection:<<<<<<<4 LOG.Error("Problem retrieving Image from clipboard.", streamImageEx); } if (returnImage != null) { - LOG.InfoFormat("Got image from clipboard with size {0} and format {1}", tmpImage.Size, tmpImage.PixelFormat); + LOG.InfoFormat("Got image from clipboard with size {0} and format {1}", returnImage.Size, returnImage.PixelFormat); yield return returnImage; } } diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index 0bd81dd46..1c1aba5e1 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -282,6 +282,16 @@ namespace GreenshotPlugin.Core { /// public Image DisplayIcon { get { + try { + using (Icon appIcon = GetAppIcon(this.Handle)) { + if (appIcon != null) { + return appIcon.ToBitmap(); + } + } + } catch (Exception ex) { + LOG.WarnFormat("Couldn't get icon for window {0} due to: {1}", Text, ex.Message); + LOG.Warn(ex); + } if (isMetroApp) { // No method yet to get the metro icon return null; @@ -304,7 +314,38 @@ namespace GreenshotPlugin.Core { } return null; } - } + } + + /// + /// Get the icon for a hWnd + /// + /// + /// + private static Icon GetAppIcon(IntPtr hwnd) { + const int GCL_HICONSM = -34; + const int GCL_HICON = -14; + const int ICON_SMALL = 0; + const int ICON_BIG = 1; + const int ICON_SMALL2 = 2; + + IntPtr iconHandle = User32.SendMessage(hwnd, (int)WindowsMessages.WM_GETICON, ICON_SMALL2, 0); + if (iconHandle == IntPtr.Zero) + iconHandle = User32.SendMessage(hwnd, (int)WindowsMessages.WM_GETICON, ICON_SMALL, 0); + if (iconHandle == IntPtr.Zero) + iconHandle = User32.SendMessage(hwnd, (int)WindowsMessages.WM_GETICON, ICON_BIG, 0); + if (iconHandle == IntPtr.Zero) + iconHandle = User32.GetClassLongWrapper(hwnd, GCL_HICON); + if (iconHandle == IntPtr.Zero) + iconHandle = User32.GetClassLongWrapper(hwnd, GCL_HICONSM); + + if (iconHandle == IntPtr.Zero) + return null; + + Icon icon = Icon.FromHandle(iconHandle); + + return icon; + } + /// /// Use this to make remove internal windows, like the mainform and the captureforms, invisible /// diff --git a/GreenshotPlugin/UnmanagedHelpers/User32.cs b/GreenshotPlugin/UnmanagedHelpers/User32.cs index 6c9019e44..9279abff1 100644 --- a/GreenshotPlugin/UnmanagedHelpers/User32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/User32.cs @@ -108,12 +108,16 @@ namespace GreenshotPlugin.UnmanagedHelpers { [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] public extern static int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32", SetLastError = true)] + public static extern IntPtr GetClassLong(IntPtr hWnd, int nIndex); + [DllImport("user32", SetLastError = true)] + public static extern IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex); + [DllImport("user32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); [DllImport("user32", SetLastError=true)] public extern static int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32", SetLastError=true, EntryPoint = "SendMessageA")] - public static extern bool SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam); + public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); [DllImport("user32", SetLastError = true)] public extern static uint GetWindowLong(IntPtr hwnd, int index); [DllImport("user32", SetLastError = true)] @@ -223,6 +227,20 @@ namespace GreenshotPlugin.UnmanagedHelpers { [DllImport("user32", SetLastError = true)] public static extern IntPtr CreateIconIndirect(ref IconInfo icon); + /// + /// Wrapper for the GetClassLong which decides if the system is 64-bit or not and calls the right one. + /// + /// IntPtr + /// int + /// IntPtr + public static IntPtr GetClassLongWrapper(IntPtr hWnd, int nIndex) { + if (IntPtr.Size > 4) { + return GetClassLongPtr(hWnd, nIndex); + } else { + return GetClassLong(hWnd, nIndex); + } + } + /// /// Wrapper for the GetWindowLong which decides if the system is 64-bit or not and calls the right one. ///