示例#1
0
        private void Refresh()
        {
            var flags = GetFlags();

            if (curFlags == flags && curId == Identifier)
            {
                return;
            }

            if (hIcon != IntPtr.Zero)
            {
                DestroyIcon(hIcon);
                hIcon = default;
            }

            var info = SHSTOCKICONINFO.Default;
            var hr   = SHGetStockIconInfo(curId = Identifier, curFlags = flags, ref info);

            // If we get an error, return null as the icon requested might not be supported on the current system
            if (hr == HRESULT.E_INVALIDARG)
            {
                throw new InvalidOperationException("Invalid identifier.");
            }
            else if (hr.Succeeded)
            {
                hIcon            = info.hIcon;
                location         = new IconLocation(info.szPath, info.iIcon);
                systemImageIndex = info.iSysImageIndex;
            }
            else
            {
                location         = null;
                systemImageIndex = 0;
            }
        }
示例#2
0
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     if (value is string s)
     {
         return(IconLocation.TryParse(s, out var loc) ? loc : null);
     }
     return(base.ConvertFrom(context, culture, value));
 }
示例#3
0
        /// <summary>Tries to parse the specified string to create a <see cref="IconLocation"/> instance.</summary>
        /// <param name="value">The string representation in the format of either "ModuleFileName,ResourceIndex" or "ModuleFileName,-ResourceID".</param>
        /// <param name="loc">The resulting <see cref="IconLocation"/> instance on success.</param>
        /// <returns><c>true</c> if successfully parsed.</returns>
        public static bool TryParse(string value, out IconLocation loc)
        {
            var parts = value?.Split(',');

            if (parts != null && parts.Length == 2 && int.TryParse(parts[1], out var i))
            {
                loc = new IconLocation(parts[0], i);
                return(true);
            }
            loc = new IconLocation();
            return(false);
        }
示例#4
0
        /// <summary>Gets the Shell icon for the given file name or extension.</summary>
        /// <param name="fileNameOrExtension">The file name or extension .</param>
        /// <param name="iconType">Flags to specify the type of the icon to retrieve. This uses the <see cref="SHGetFileInfo(string, System.IO.FileAttributes, ref SHFILEINFO, int, SHGFI)"/> method and can only retrieve small or large icons.</param>
        /// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
        public static Icon GetFileIcon(string fileNameOrExtension, ShellIconType iconType = ShellIconType.Large)
        {
            const SHGFI baseFlags = SHGFI.SHGFI_USEFILEATTRIBUTES | SHGFI.SHGFI_ICON;
            var         shfi      = new SHFILEINFO();
            var         ret       = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, baseFlags | (SHGFI)iconType);

            if (ret == IntPtr.Zero)
            {
                ret = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, SHGFI.SHGFI_ICON | (SHGFI)iconType);
            }
            return(ret == IntPtr.Zero ? null : IconLocation.GetClonedIcon(shfi.hIcon));
        }
示例#5
0
        /// <summary>Gets the system icon for the given file name or extension.</summary>
        /// <param name="fileNameOrExtension">The file name or extension.</param>
        /// <param name="iconSize">Size of the icon.</param>
        /// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
        public static Icon GetSystemIcon(string fileNameOrExtension, ShellImageSize iconSize = ShellImageSize.Large)
        {
            var shfi       = new SHFILEINFO();
            var hImageList = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, SHGFI.SHGFI_SYSICONINDEX | (iconSize == ShellImageSize.Small ? SHGFI.SHGFI_SMALLICON : 0));

            if (hImageList == IntPtr.Zero)
            {
                return(null);
            }
            if (iconSize <= ShellImageSize.Small)
            {
                return(IconLocation.GetClonedIcon(ImageList_GetIcon(hImageList, shfi.iIcon, IMAGELISTDRAWFLAGS.ILD_TRANSPARENT)));
            }
            SHGetImageList((SHIL)iconSize, typeof(IImageList).GUID, out var il).ThrowIfFailed();
            return(IconLocation.GetClonedIcon(il.GetIcon(shfi.iIcon, IMAGELISTDRAWFLAGS.ILD_TRANSPARENT)));
        }
示例#6
0
        public static void RegisterApplication(string fullExePath, bool userOnly     = false, bool acceptsUrls        = false, Guid?dropTarget = null, IndirectString friendlyName = null,
                                               IEnumerable <string> supportedTypes   = null, IconLocation defaultIcon = null, bool noStartPage = false, IconLocation taskGroupIcon = null,
                                               bool useExecutableForTaskbarGroupIcon = false)
        {
            if (fullExePath == null)
            {
                throw new ArgumentNullException(nameof(fullExePath));
            }
            fullExePath = Path.GetFullPath(fullExePath);
            var fn = Path.GetFileName(fullExePath).ToLower();

            // Handle registrations in user or machine "App Paths"
            var root = userOnly ? Registry.CurrentUser : Registry.LocalMachine;

            using (var reg = root.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths"))
                using (var sk = reg?.CreateSubKey(fn))
                {
                    if (sk == null)
                    {
                        throw new InvalidOperationException("Unable to create application key in the 'App Paths' subkey.");
                    }
                    // Build short path and store as default value
                    var shortPath = fullExePath;
                    var l         = fullExePath.Length + 5;
                    var sb        = new StringBuilder(l, l);
                    var rl        = PInvoke.Kernel32.GetShortPathName(fullExePath.Length > PInvoke.Kernel32.MAX_PATH ? @"\\?\" + fullExePath : fullExePath, sb, (uint)sb.Capacity);
                    if (rl > 0 && rl <= l)
                    {
                        shortPath = sb.ToString();
                    }
                    sk.SetValue(null, shortPath);
                    // Add Path value
                    sk.SetValue("Path", Path.GetDirectoryName(fullExePath));
                    // Add UseUrl value if needed
                    if (acceptsUrls)
                    {
                        sk.SetValue("UseUrl", 1U, RegistryValueKind.DWord);
                    }
                    // Add DropTarget GUID if needed
                    if (dropTarget != null)
                    {
                        sk.SetValue("DropTarget", dropTarget.Value.ToString());
                    }
                }

            // Handle registrations in HKCR\Applications
            using (var reg = Registry.ClassesRoot.OpenSubKey(@"Applications"))
                using (var sk = reg?.CreateSubKey(fn))
                {
                    if (sk == null)
                    {
                        throw new InvalidOperationException("Unable to create application key in the HKCR\\Applications subkey.");
                    }
                    if (friendlyName != null)
                    {
                        sk.SetValue("FriendlyAppName", friendlyName.ToString());
                    }
                    if (supportedTypes != null)
                    {
                        using (var stk = sk.CreateSubKey("SupportedTypes"))
                            foreach (var s in supportedTypes)
                            {
                                stk?.SetValue(s, string.Empty, RegistryValueKind.String);
                            }
                    }
                    if (defaultIcon != null)
                    {
                        sk.CreateSubKey("DefaultIcon", defaultIcon.ToString());
                    }
                    if (noStartPage)
                    {
                        sk.SetValue("NoStartPage", string.Empty, RegistryValueKind.String);
                    }
                    if (taskGroupIcon != null)
                    {
                        sk.SetValue("TaskbarGroupIcon", taskGroupIcon.ToString());
                    }
                    if (useExecutableForTaskbarGroupIcon)
                    {
                        sk.SetValue("UseExecutableForTaskbarGroupIcon", string.Empty, RegistryValueKind.String);
                    }
                }

            NotifyShell();
        }