Beispiel #1
0
        BitmapSource GetWindowIcon(IntPtr windowHandle, bool bigIconSize = true)
        {
            var hIcon = windowNativeApi.SendMessage(
                windowHandle,
                (int)Message.WM_GETICON,
                bigIconSize ? WindowNativeApi.ICON_LARGE : WindowNativeApi.ICON_SMALL,
                IntPtr.Zero);

            if (hIcon == IntPtr.Zero)
            {
                hIcon = windowNativeApi.GetClassLongPtr(windowHandle, WindowNativeApi.GCL_HICON);
            }

            if (hIcon == IntPtr.Zero)
            {
                hIcon = windowNativeApi.LoadIcon(IntPtr.Zero, WindowNativeApi.IDI_APPLICATION);
            }

            if (hIcon != IntPtr.Zero)
            {
                return(Imaging.CreateBitmapSourceFromHIcon(
                           hIcon,
                           Int32Rect.Empty,
                           BitmapSizeOptions.FromEmptyOptions()));
            }

            throw new InvalidOperationException("Could not load window icon.");
        }
        async Task <BitmapSource> GetWindowIconAsync(
            Process process,
            IntPtr windowHandle,
            bool bigIconSize = true)
        {
            if (process.Id != 0)
            {
                try
                {
                    var processDirectoryName = Path.GetFileName(Path.GetDirectoryName(process.MainModule.FileName));
                    Debug.Assert(processDirectoryName != null, nameof(processDirectoryName) + " != null");

                    var universalWindowsApplicationPackage = packageManager.FindPackageForUser(string.Empty, processDirectoryName);
                    if (universalWindowsApplicationPackage != null)
                    {
                        return(await GetIconFromUniversalWindowsApplicationAsync(
                                   universalWindowsApplicationPackage,
                                   new Size(1024, 1024)));
                    }
                }
                catch (Win32Exception)
                {
                    //issue #560 trying to fetch MainModule of a 32 bit process from Shapeshifter which is a 64 bit process.
                }
            }

            var hIcon = windowNativeApi.SendMessage(
                windowHandle,
                (int)Message.WM_GETICON,
                bigIconSize ? WindowNativeApi.ICON_LARGE : WindowNativeApi.ICON_SMALL,
                IntPtr.Zero);

            if (hIcon == IntPtr.Zero)
            {
                hIcon = windowNativeApi.GetClassLongPtr(windowHandle, WindowNativeApi.GCL_HICON);
            }

            if (hIcon == IntPtr.Zero)
            {
                hIcon = windowNativeApi.LoadIcon(IntPtr.Zero, WindowNativeApi.IDI_APPLICATION);
            }

            if (hIcon != IntPtr.Zero)
            {
                return(Imaging.CreateBitmapSourceFromHIcon(
                           hIcon,
                           Int32Rect.Empty,
                           BitmapSizeOptions.FromEmptyOptions()));
            }

            throw new InvalidOperationException("Could not load window icon.");
        }