private ImageSource GetShellItemImageSource(IconSize size) { ImageSource img; ShellItem item = new ShellItem("shell:appsfolder\\" + AppUserModelId); item.AllowAsync = false; switch (size) { case IconSize.Small: img = item.SmallIcon; break; case IconSize.ExtraLarge: img = item.ExtraLargeIcon; break; case IconSize.Jumbo: img = item.JumboIcon; break; default: img = item.LargeIcon; break; } item.Dispose(); if (img != null) { return(img); } return(IconImageConverter.GetDefaultIcon()); }
private void SetIconFromHIcon(IntPtr hIcon) { if (hIcon == IntPtr.Zero) { if (Icon == null) { // Use default only if we don't have a valid icon already Icon = IconImageConverter.GetDefaultIcon(); } return; } ImageSource icon = IconImageConverter.GetImageFromHIcon(hIcon, false); if (icon != null) { Icon = icon; } else if (icon == null && Icon == null) { // Use default only if we don't have a valid icon already Icon = IconImageConverter.GetDefaultIcon(); } }
private ImageSource GetDisplayIcon(IconSize size) { ImageSource icon = null; IShellItemImageFactory imageFactory = GetImageFactory(AbsolutePidl); if (imageFactory == null) { icon = IconImageConverter.GetDefaultIcon(); } else { try { int iconPoints = IconHelper.GetSize(size); SIZE imageSize = new SIZE { cx = (int)(iconPoints * DpiHelper.DpiScale), cy = (int)(iconPoints * DpiHelper.DpiScale) }; IntPtr hBitmap = IntPtr.Zero; SIIGBF flags = 0; if (size == IconSize.Small) { // for 16pt icons, thumbnails are too small flags = SIIGBF.ICONONLY; } if (imageFactory?.GetImage(imageSize, flags, out hBitmap) == NativeMethods.S_OK) { if (hBitmap != IntPtr.Zero) { icon = IconImageConverter.GetImageFromHBitmap(hBitmap); } } } catch (Exception e) { ShellLogger.Error($"ShellItem: Unable to get icon from ShellItemImageFactory: {e.Message}"); } finally { Marshal.FinalReleaseComObject(imageFactory); } } if (icon == null) { // Fall back to SHGetFileInfo icon = IconImageConverter.GetImageFromAssociatedIcon(AbsolutePidl, size); } if (icon == null) { icon = IconImageConverter.GetDefaultIcon(); } return(icon); }
public void SetIcon() { if (!_iconLoading) { _iconLoading = true; var thread = new Thread(() => { if (isUWP && !string.IsNullOrEmpty(AppUserModelID)) { // UWP apps try { BitmapImage img = new BitmapImage(); img.BeginInit(); img.UriSource = new Uri(UWPInterop.StoreAppHelper.GetAppIcon(AppUserModelID, Configuration.Settings.TaskbarIconSize)[0], UriKind.Absolute); img.CacheOption = BitmapCacheOption.OnLoad; img.EndInit(); img.Freeze(); Icon = img; } catch { if (_icon == null) { Icon = IconImageConverter.GetDefaultIcon(); } } } else { // non-UWP apps IntPtr hIco = default; uint WM_GETICON = (uint)NativeMethods.WM.GETICON; uint WM_QUERYDRAGICON = (uint)NativeMethods.WM.QUERYDRAGICON; int GCL_HICON = -14; int GCL_HICONSM = -34; int sizeSetting = Configuration.Settings.TaskbarIconSize; if (sizeSetting == 1) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 2, 0, 2, 1000, ref hIco); if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 0, 0, 2, 1000, ref hIco); } } else { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 1, 0, 2, 1000, ref hIco); } if (hIco == IntPtr.Zero && sizeSetting == 1) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICONSM); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICONSM); } } if (hIco == IntPtr.Zero) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICON); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICON); } } if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_QUERYDRAGICON, 0, 0, 0, 1000, ref hIco); } if (hIco == IntPtr.Zero && _icon == null) { // last resort: find icon by executable. if we already have an icon from a previous fetch, then just skip this if (Shell.Exists(WinFileName)) { int size = 1; if (sizeSetting != 1) { size = 0; } hIco = Shell.GetIconByFilename(WinFileName, size); } } if (hIco != IntPtr.Zero) { bool returnDefault = (_icon == null); // only return a default icon if we don't already have one. otherwise let's use what we have. ImageSource icon = IconImageConverter.GetImageFromHIcon(hIco, returnDefault); if (icon != null) { icon.Freeze(); Icon = icon; } } else if (Configuration.Settings.EnableTaskbarPolling && iconTries == 0) { DispatcherTimer getIcon = new DispatcherTimer(DispatcherPriority.Background, Application.Current.Dispatcher); getIcon.Interval = new TimeSpan(0, 0, 2); getIcon.Tick += getIcon_Tick; getIcon.Start(); } } _iconLoading = false; }); thread.IsBackground = true; thread.SetApartmentState(ApartmentState.STA); thread.Start(); } }
private bool SysTrayCallback(uint message, SafeNotifyIconData nicData) { if (nicData.hWnd == IntPtr.Zero) { return(false); } NotifyIcon trayIcon = new NotifyIcon(this, nicData.hWnd); trayIcon.UID = nicData.uID; lock (_lockObject) { if ((NIM)message == NIM.NIM_ADD || (NIM)message == NIM.NIM_MODIFY) { try { bool exists = false; bool titleChanged = false; // hide icons while we are shell which require UWP support & we have a separate implementation for if (nicData.guidItem == new Guid(VOLUME_GUID) && ((EnvironmentHelper.IsAppRunningAsShell && EnvironmentHelper.IsWindows10OrBetter) || GroupPolicyHelper.HideScaVolume)) { return(false); } // hide icons per group policy if ((nicData.guidItem == new Guid(HEALTH_GUID) && GroupPolicyHelper.HideScaHealth) || (nicData.guidItem == new Guid(MEETNOW_GUID) && GroupPolicyHelper.HideScaMeetNow) || (nicData.guidItem == new Guid(NETWORK_GUID) && GroupPolicyHelper.HideScaNetwork) || (nicData.guidItem == new Guid(POWER_GUID) && GroupPolicyHelper.HideScaPower)) { return(false); } foreach (NotifyIcon ti in TrayIcons) { if (ti.Equals(nicData)) { exists = true; trayIcon = ti; break; } } if ((NIF.STATE & nicData.uFlags) != 0) { trayIcon.IsHidden = nicData.dwState == 1; } if ((NIF.TIP & nicData.uFlags) != 0 && !string.IsNullOrEmpty(nicData.szTip)) { trayIcon.Title = nicData.szTip; titleChanged = true; } if ((NIF.ICON & nicData.uFlags) != 0) { if (nicData.hIcon != IntPtr.Zero) { System.Windows.Media.ImageSource icon = IconImageConverter.GetImageFromHIcon(nicData.hIcon, false); if (icon != null) { trayIcon.Icon = icon; } else if (icon == null && trayIcon.Icon == null) { // Use default only if we don't have a valid icon already trayIcon.Icon = IconImageConverter.GetDefaultIcon(); } } else { trayIcon.Icon = null; } } trayIcon.HWnd = nicData.hWnd; trayIcon.UID = nicData.uID; if ((NIF.GUID & nicData.uFlags) != 0) { trayIcon.GUID = nicData.guidItem; } if (nicData.uVersion > 0 && nicData.uVersion <= 4) { trayIcon.Version = nicData.uVersion; } if ((NIF.MESSAGE & nicData.uFlags) != 0) { trayIcon.CallbackMessage = nicData.uCallbackMessage; } if (!exists) { // default placement to a menu bar-like rect trayIcon.Placement = defaultPlacement; // set properties used for pinning trayIcon.Path = ShellHelper.GetPathForWindowHandle(trayIcon.HWnd); trayIcon.SetPinValues(); if (trayIcon.Icon == null) { trayIcon.Icon = IconImageConverter.GetDefaultIcon(); } TrayIcons.Add(trayIcon); if ((NIF.INFO & nicData.uFlags) != 0) { handleBalloonData(nicData, trayIcon); } ShellLogger.Debug($"NotificationArea: Added: {trayIcon.Title} Path: {trayIcon.Path} Hidden: {trayIcon.IsHidden} GUID: {trayIcon.GUID} UID: {trayIcon.UID} Version: {trayIcon.Version}"); if ((NIM)message == NIM.NIM_MODIFY) { // return an error to the notifyicon as we received a modify for an icon we did not yet have return(false); } } else { if ((NIF.INFO & nicData.uFlags) != 0) { handleBalloonData(nicData, trayIcon); } if (titleChanged && trayIcon.GUID == default && !trayIcon.IsPinned) { // set properties used for pinning trayIcon.SetPinValues(); } ShellLogger.Debug($"NotificationArea: Modified: {trayIcon.Title}"); } } catch (Exception ex) { ShellLogger.Error("NotificationArea: Unable to modify the icon in the collection.", ex); } } else if ((NIM)message == NIM.NIM_DELETE) { try { foreach (var icon in TrayIcons) { if (icon.Equals(trayIcon)) { TrayIcons.Remove(trayIcon); ShellLogger.Debug($"NotificationArea: Removed: {icon.Title}"); return(true); } } return(false); } catch (Exception ex) { ShellLogger.Error("NotificationArea: Unable to remove the icon from the collection.", ex); } } else if ((NIM)message == NIM.NIM_SETVERSION) { if (nicData.uVersion > 4) { return(false); } foreach (NotifyIcon ti in TrayIcons) { if (ti.Equals(nicData)) { ti.Version = nicData.uVersion; ShellLogger.Debug($"NotificationArea: Modified version to {ti.Version} on: {ti.Title}"); break; } } } } return(true); }
public void SetIcon() { if (!_iconLoading) { _iconLoading = true; var thread = new Thread(() => { if (WinFileName.Contains("ApplicationFrameHost.exe") && !string.IsNullOrEmpty(AppUserModelID)) { // UWP apps try { BitmapImage img = new BitmapImage(); img.BeginInit(); img.UriSource = new Uri(UWPInterop.StoreAppHelper.GetAppIcon(AppUserModelID, Configuration.Settings.TaskbarIconSize)[0], UriKind.Absolute); img.CacheOption = BitmapCacheOption.OnLoad; img.EndInit(); img.Freeze(); Icon = img; } catch { Icon = IconImageConverter.GetDefaultIcon(); } } else { // non-UWP apps IntPtr hIco = default(IntPtr); uint WM_GETICON = 0x007f; IntPtr IDI_APPLICATION = new IntPtr(0x7F00); int GCL_HICON = -14; int GCL_HICONSM = -34; int sizeSetting = Configuration.Settings.TaskbarIconSize; if (sizeSetting == 1) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 2, 0, 2, 1000, ref hIco); if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 0, 0, 2, 1000, ref hIco); } } else { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 1, 0, 2, 1000, ref hIco); } if (hIco == IntPtr.Zero && sizeSetting == 1) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICONSM); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICONSM); } } if (hIco == IntPtr.Zero) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICON); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICON); } } if (hIco == IntPtr.Zero) { string winFileName = GetFileNameForWindow(Handle); if (Shell.Exists(winFileName)) { int size = 1; if (sizeSetting != 1) { size = 0; } hIco = Shell.GetIconByFilename(winFileName, size); } } if (hIco != IntPtr.Zero) { ImageSource icon = IconImageConverter.GetImageFromHIcon(hIco); icon.Freeze(); Icon = icon; } else if (iconTries == 0) { DispatcherTimer getIcon = new DispatcherTimer(DispatcherPriority.Background, Application.Current.Dispatcher); getIcon.Interval = new TimeSpan(0, 0, 2); getIcon.Tick += getIcon_Tick; getIcon.Start(); } } _iconLoading = false; }); thread.IsBackground = true; thread.SetApartmentState(ApartmentState.STA); thread.Start(); } }
private void setIcon() { if (!_iconLoading && ShowInTaskbar) { _iconLoading = true; Task.Factory.StartNew(() => { if (isUWP && !string.IsNullOrEmpty(AppUserModelID)) { // UWP apps try { BitmapImage img = new BitmapImage(); img.BeginInit(); img.UriSource = new Uri(UWPInterop.StoreAppHelper.GetAppIcon(AppUserModelID, Configuration.Settings.Instance.TaskbarIconSize)[0], UriKind.Absolute); img.CacheOption = BitmapCacheOption.OnLoad; img.EndInit(); img.Freeze(); Icon = img; } catch { if (_icon == null) { Icon = IconImageConverter.GetDefaultIcon(); } } } else { // non-UWP apps IntPtr hIco = default; uint WM_GETICON = (uint)NativeMethods.WM.GETICON; uint WM_QUERYDRAGICON = (uint)NativeMethods.WM.QUERYDRAGICON; int GCL_HICON = -14; int GCL_HICONSM = -34; int sizeSetting = Configuration.Settings.Instance.TaskbarIconSize; if (sizeSetting == 1) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 2, 0, 2, 1000, ref hIco); if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 0, 0, 2, 1000, ref hIco); } } else { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 1, 0, 2, 1000, ref hIco); } if (hIco == IntPtr.Zero && sizeSetting == 1) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICONSM); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICONSM); } } if (hIco == IntPtr.Zero) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICON); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICON); } } if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_QUERYDRAGICON, 0, 0, 0, 1000, ref hIco); } if (hIco == IntPtr.Zero && _icon == null) { // last resort: find icon by executable. if we already have an icon from a previous fetch, then just skip this if (Shell.Exists(WinFileName)) { int size = 1; if (sizeSetting != 1) { size = 0; } hIco = Shell.GetIconByFilename(WinFileName, size); } } if (hIco != IntPtr.Zero) { if (_hIcon != hIco) { _hIcon = hIco; bool returnDefault = (_icon == null); // only return a default icon if we don't already have one. otherwise let's use what we have. ImageSource icon = IconImageConverter.GetImageFromHIcon(hIco, returnDefault); if (icon != null) { icon.Freeze(); Icon = icon; } } else { NativeMethods.DestroyIcon(hIco); } } } _iconLoading = false; }, CancellationToken.None, TaskCreationOptions.None, Shell.IconScheduler); } }
private void setIcon() { if (!_iconLoading && ShowInTaskbar) { _iconLoading = true; Task.Factory.StartNew(() => { if (IsUWP && !string.IsNullOrEmpty(AppUserModelID)) { // UWP apps try { var storeApp = UWPInterop.StoreAppHelper.AppList.GetAppByAumid(AppUserModelID); if (storeApp != null) { Icon = storeApp.GetIconImageSource(_tasksService.TaskIconSize); } else { Icon = IconImageConverter.GetDefaultIcon(); } } catch { if (_icon == null) { Icon = IconImageConverter.GetDefaultIcon(); } } } else { // non-UWP apps IntPtr hIco = default; uint WM_GETICON = (uint)NativeMethods.WM.GETICON; uint WM_QUERYDRAGICON = (uint)NativeMethods.WM.QUERYDRAGICON; int GCL_HICON = -14; int GCL_HICONSM = -34; IconSize sizeSetting = _tasksService.TaskIconSize; if (sizeSetting == IconSize.Small) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 2, 0, 2, 1000, ref hIco); if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 0, 0, 2, 1000, ref hIco); } } else { NativeMethods.SendMessageTimeout(Handle, WM_GETICON, 1, 0, 2, 1000, ref hIco); } if (hIco == IntPtr.Zero && sizeSetting == IconSize.Small) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICONSM); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICONSM); } } if (hIco == IntPtr.Zero) { if (!Environment.Is64BitProcess) { hIco = NativeMethods.GetClassLong(Handle, GCL_HICON); } else { hIco = NativeMethods.GetClassLongPtr(Handle, GCL_HICON); } } if (hIco == IntPtr.Zero) { NativeMethods.SendMessageTimeout(Handle, WM_QUERYDRAGICON, 0, 0, 0, 1000, ref hIco); } if (hIco == IntPtr.Zero && _icon == null) { // last resort: find icon by executable. if we already have an icon from a previous fetch, then just skip this if (ShellHelper.Exists(WinFileName)) { IconSize size = IconSize.Small; if (sizeSetting != size) { size = IconSize.Large; } hIco = IconHelper.GetIconByFilename(WinFileName, size); } } if (hIco != IntPtr.Zero) { if (_hIcon != hIco) { _hIcon = hIco; bool returnDefault = (_icon == null); // only return a default icon if we don't already have one. otherwise let's use what we have. ImageSource icon = IconImageConverter.GetImageFromHIcon(hIco, returnDefault); if (icon != null) { icon.Freeze(); Icon = icon; } } else { NativeMethods.DestroyIcon(hIco); } } } _iconLoading = false; }, CancellationToken.None, TaskCreationOptions.None, IconHelper.IconScheduler); } }