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
This commit is contained in:
RKrom 2013-01-08 11:13:18 +00:00
parent 5ce65ab191
commit 8a3fd911bf
3 changed files with 62 additions and 3 deletions

View file

@ -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;
}
}

View file

@ -282,6 +282,16 @@ namespace GreenshotPlugin.Core {
/// </summary>
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;
}
}
}
/// <summary>
/// Get the icon for a hWnd
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Use this to make remove internal windows, like the mainform and the captureforms, invisible
/// </summary>

View file

@ -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);
/// <summary>
/// Wrapper for the GetClassLong which decides if the system is 64-bit or not and calls the right one.
/// </summary>
/// <param name="hWnd">IntPtr</param>
/// <param name="nIndex">int</param>
/// <returns>IntPtr</returns>
public static IntPtr GetClassLongWrapper(IntPtr hWnd, int nIndex) {
if (IntPtr.Size > 4) {
return GetClassLongPtr(hWnd, nIndex);
} else {
return GetClassLong(hWnd, nIndex);
}
}
/// <summary>
/// Wrapper for the GetWindowLong which decides if the system is 64-bit or not and calls the right one.
/// </summary>