Exemple #1
0
        /// <summary>
        /// Returns the icon associated with the application executable.
        /// </summary>
        /// <returns></returns>
        public Icon GetAssemblyIcon()
        {
            var sfi   = new NativeMethods.SHFILEINFO();
            var flags = NativeMethods.SHGFI.SHGFI_LARGEICON | NativeMethods.SHGFI.SHGFI_ICON;

            Icon ret = null;

            if (SafeNativeMethods.SHGetFileInfo(ModuleName, 0, ref sfi, Marshal.SizeOf(sfi), flags) != IntPtr.Zero)
            {
                ret = Icon.FromHandle(sfi.hIcon);
            }
            return(ret);
        }
Exemple #2
0
        /// <summary>
        /// Returns the icon associated with the window.
        /// </summary>
        /// <returns></returns>
        public virtual Icon GetWindowIcon()
        {
            if (IsDesktop)
            {
                IntPtr pidl;
                if (SafeNativeMethods.SHGetFolderLocation(new HandleRef(this, Handle), 0, IntPtr.Zero, 0, out pidl) == 0)
                {
                    var sfi   = new NativeMethods.SHFILEINFO();
                    var flags = NativeMethods.SHGFI.SHGFI_LARGEICON | NativeMethods.SHGFI.SHGFI_ICON | NativeMethods.SHGFI.SHGFI_PIDL;

                    Icon retIcon = null;
                    if (SafeNativeMethods.SHGetFileInfo(new HandleRef(this, pidl), 0, ref sfi, Marshal.SizeOf(sfi), flags) != IntPtr.Zero)
                    {
                        var ico = Icon.FromHandle(sfi.hIcon);
                        retIcon = (Icon)ico.Clone();
                        ico.Dispose();
                        SafeNativeMethods.DestroyIcon(new HandleRef(sfi, sfi.hIcon));
                    }

                    Marshal.FreeCoTaskMem(pidl);
                    return(retIcon);
                }
                return(null);
            }
            else
            {
                var hIcon = SendMessage(NativeMethods.WM_GETICON, NativeMethods.ICON_BIG, 0);
                if (hIcon != IntPtr.Zero)
                {
                    // Return a cloned icon (the original icon can be destroyed at any time, and is unsafe to use.)
                    // BUG: Cloning blows memory?
                    var appIcon = Icon.FromHandle(hIcon);
                    var retIcon = (Icon)appIcon.Clone();
                    appIcon.Dispose();
                    return(retIcon);
                }
                else
                {
                    return(null);
                }
            }
        }
 public static extern IntPtr SHGetFileInfo(
     HandleRef pidl,
     int dwFileAttributes,
     ref NativeMethods.SHFILEINFO psfi,
     int cbFileInfo,
     NativeMethods.SHGFI uFlags);
 public static extern IntPtr SHGetFileInfo(
     string pszPath,
     int dwFileAttributes,
     ref NativeMethods.SHFILEINFO psfi,
     int cbFileInfo,
     NativeMethods.SHGFI uFlags);
Exemple #5
0
		/// <summary>
		/// Returns the icon associated with the window.
		/// </summary>
		/// <returns></returns>
		public virtual Icon GetWindowIcon()
		{
			if (IsDesktop)
			{
				IntPtr pidl;
				if (SafeNativeMethods.SHGetFolderLocation(new HandleRef(this, Handle), 0, IntPtr.Zero, 0, out pidl) == 0)
				{
					var sfi = new NativeMethods.SHFILEINFO();
					var flags = NativeMethods.SHGFI.SHGFI_LARGEICON | NativeMethods.SHGFI.SHGFI_ICON | NativeMethods.SHGFI.SHGFI_PIDL;

					Icon retIcon = null;
					if (SafeNativeMethods.SHGetFileInfo(new HandleRef(this, pidl), 0, ref sfi, Marshal.SizeOf(sfi), flags) != IntPtr.Zero)
					{
						var ico = Icon.FromHandle(sfi.hIcon);
						retIcon = (Icon)ico.Clone();
						ico.Dispose();
						SafeNativeMethods.DestroyIcon(new HandleRef(sfi, sfi.hIcon));
					}

					Marshal.FreeCoTaskMem(pidl);
					return retIcon;
				}
				return null;
			}
			else
			{
				var hIcon = SendMessage(NativeMethods.WM_GETICON, NativeMethods.ICON_BIG, 0);
				if (hIcon != IntPtr.Zero)
				{
					// Return a cloned icon (the original icon can be destroyed at any time, and is unsafe to use.)
					// BUG: Cloning blows memory?
					var appIcon = Icon.FromHandle(hIcon);
					var retIcon = (Icon)appIcon.Clone();
					appIcon.Dispose();
					return retIcon;
				}
				else
					return null;
			}
		}
Exemple #6
0
		/// <summary>
		/// Returns the icon associated with the application executable.
		/// </summary>
		/// <returns></returns>
		public Icon GetAssemblyIcon()
		{
			var sfi = new NativeMethods.SHFILEINFO();
			var flags = NativeMethods.SHGFI.SHGFI_LARGEICON | NativeMethods.SHGFI.SHGFI_ICON;

			Icon ret = null;
			if (SafeNativeMethods.SHGetFileInfo(ModuleName, 0, ref sfi, Marshal.SizeOf(sfi), flags) != IntPtr.Zero)
				ret = Icon.FromHandle(sfi.hIcon);
			return ret;
		}