Beispiel #1
0
        /// <summary>
        /// Converts this raw icon source into a managed System.Drawing.Icon image.
        /// </summary>
        /// <returns>A new Icon object.</returns>
        /// <remarks></remarks>
        public Icon ToIcon()
        {
            Icon iconOut;

            if (IsPngFormat)
            {
                Bitmap bmp = (Bitmap)ToImage();

                var bmi    = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
                var lpicon = default(ICONINFO);

                int i;

                var bm = bmi.LockBits(new Rectangle(0, 0, bmi.Width, bmi.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

                MemPtr mm = bm.Scan0;

                int z = (int)(Math.Max(bmp.Width, 32) * bmp.Height / 8d);

                for (i = 0; i < z; i++)
                {
                    mm.ByteAt(i) = 255;
                }

                bmi.UnlockBits(bm);

                lpicon.hbmColor = bmp.GetHbitmap();
                lpicon.hbmMask  = bmi.GetHbitmap();
                lpicon.fIcon    = 1;

                var hIcon = User32.CreateIconIndirect(ref lpicon);

                if (hIcon != IntPtr.Zero)
                {
                    iconOut = (Icon)Icon.FromHandle(hIcon).Clone();
                    User32.DestroyIcon(hIcon);
                }
                else
                {
                    iconOut = null;
                }

                NativeShell.DeleteObject(lpicon.hbmMask);
                NativeShell.DeleteObject(lpicon.hbmColor);
            }
            else
            {
                iconOut = _constructIcon();
            }

            return(iconOut);
        }
Beispiel #2
0
        /// <summary>
        /// Refresh using the IAssocHandler
        /// </summary>
        /// <param name="handler"></param>
        internal void Refresh(IAssocHandler handler)
        {
            string pth = null;

            int idx = 0;

            _Handler  = handler;
            Preferred = _Handler.IsRecommended() == HResult.Ok;

            string argppsz = ExePath;

            handler.GetName(out argppsz);

            ExePath = argppsz;

            if (File.Exists(ExePath) == false)
            {
                throw new SystemException("Program path not found");
            }

            handler.GetUIName(out string strRet);
            UIName = strRet;

            handler.GetIconLocation(out pth, out idx);
            Icon = Resources.LoadLibraryIcon(pth, idx, IconSize);

            if (Icon is null)
            {
                int iix = (int)NativeShell.Shell_GetCachedImageIndex(pth, idx, 0U);

                switch (IconSize)
                {
                case StandardIcons.Icon256:
                    Icon = Resources.GetFileIconFromIndex(iix, Resources.SystemIconSizes.Jumbo);
                    break;

                case StandardIcons.Icon48:
                    Icon = Resources.GetFileIconFromIndex(iix, Resources.SystemIconSizes.ExtraLarge);
                    break;

                case StandardIcons.Icon32:
                    Icon = Resources.GetFileIconFromIndex(iix, Resources.SystemIconSizes.Large);
                    break;

                default:
                    Icon = Resources.GetFileIconFromIndex(iix, Resources.SystemIconSizes.Small);
                    break;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Create a bitmap structure with bits from this raw icon image.
        /// </summary>
        /// <returns>An array of bytes that represent data to create a bitmap.</returns>
        /// <remarks></remarks>
        private byte[] _makeBitmap()
        {
            byte[] _makeBitmapRet = default;
            if (!IsPngFormat)
            {
                _makeBitmapRet = _image;
                return(_makeBitmapRet);
            }

            IntPtr bmp  = default;
            var    hbmp = Resources.MakeDIBSection((Bitmap)ToImage(), ref bmp);
            var    mm   = new SafePtr();
            var    bm   = new BITMAPINFOHEADER();
            int    maskSize;
            int    w = _entry.cWidth;
            int    h = _entry.cHeight;

            if (w == 0)
            {
                w = 256;
            }
            if (h == 0)
            {
                h = 256;
            }
            bm.biSize      = 40;
            bm.biWidth     = w;
            bm.biHeight    = h * 2;
            bm.biPlanes    = 1;
            bm.biBitCount  = 32;
            bm.biSizeImage = w * h * 4;
            maskSize       = (int)(Math.Max(w, 32) * h / 8d);
            mm.Alloc(bm.biSizeImage + 40 + maskSize);
            var ptr1 = mm.DangerousGetHandle() + 40;
            var ptr2 = mm.DangerousGetHandle() + 40 + bm.biSizeImage;

            Marshal.StructureToPtr(bm, mm.DangerousGetHandle(), false);
            Native.MemCpy(bmp, ptr1, bm.biSizeImage);
            bm = mm.ToStruct <BITMAPINFOHEADER>();
            _setMask(ptr1, ptr2, w, h);
            _entry.dwImageSize = (int)mm.Length;
            _makeBitmapRet     = (byte[])mm;
            mm.Free();
            NativeShell.DeleteObject(hbmp);
            return(_makeBitmapRet);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new <see cref="SystemFileType"/> object from the given extension with the specified parameters.
        /// </summary>
        /// <param name="ext">The file extension.</param>
        /// <param name="parent">The parent <see cref="AllSystemFileTypes"/> object.</param>
        /// <param name="size">The default icon size.</param>
        /// <returns></returns>
        public static SystemFileType FromExtension(string ext, AllSystemFileTypes parent = null, StandardIcons size = StandardIcons.Icon48)
        {
            var c = new SystemFileType(ext);

            if (parent is object)
            {
                c.Parent = parent;
            }
            var assoc = NativeShell.EnumFileHandlers(ext);

            if (assoc is null || assoc.Count() == 0)
            {
                return(null);
            }
            c.Populate(assoc, size);
            if (c.Handlers.Count == 0)
            {
                return(null);
            }
            else
            {
                return(c);
            }
        }
        /// <summary>
        /// Create a new file-system-linked directory object
        /// </summary>
        /// <param name="path"></param>
        public DirectoryObject(string parsingName, bool isSpecial = false, bool initialize = true, StandardIcons iconSize = StandardIcons.Icon48)
        {
            if (string.IsNullOrEmpty(parsingName))
            {
                throw new ArgumentNullException(nameof(Path), "Path is null or not found.");
            }

            _IsSpecial = isSpecial;
            _IconSize  = iconSize;

            // Dim mm As New MemPtr
            // Dim res As HResult = SHCreateItemFromParsingName(path, Nothing, Guid.Parse(ShellIIDGuid.IShellItem), shitem)
            // Dim fp As String = Nothing
            // Dim parts As String() = Nothing

            // If (res = HResult.Ok) Then
            // shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, mm)

            // fp = mm
            // ParsingName = fp

            // mm.CoTaskMemFree()
            // ElseIf (path.Substring(0, 2) = "::") Then
            // parts = path.Split("\")

            // res = SHCreateItemFromParsingName(parts(parts.Length - 1), Nothing, Guid.Parse(ShellIIDGuid.IShellItem), shitem)
            // If res = HResult.Ok Then
            // shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, mm)

            // fp = mm
            // ParsingName = fp

            // mm.CoTaskMemFree()
            // End If
            // End If

            // res = SHCreateItemFromParsingName("shell:" + If(parts IsNot Nothing, parts(parts.Length - 1), If(fp, path)), Nothing, Guid.Parse(ShellIIDGuid.IShellItem), shitem)

            // If (res = HResult.Ok) Then
            // Dim ip As IntPtr

            // res = shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, ip)
            // mm = ip

            // If (res = HResult.Ok) Then
            // CanonicalName = If(fp Is Nothing, "shell:" + path, mm.ToString())
            // If (ParsingName Is Nothing) Then ParsingName = mm

            // _Path = ParsingName

            // mm.CoTaskMemFree()
            // shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, mm)

            // DisplayName = mm
            // mm.CoTaskMemFree()
            // End If
            // End If


            // shitem = Nothing

            if (_IsSpecial)
            {
                // let's see if we can parse it.
                IShellItem shitem  = null;
                var        mm      = new MemPtr();
                var        argriid = Guid.Parse(ShellIIDGuid.IShellItem);
                var        res     = NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref argriid, ref shitem);
                string     fp      = null;
                if (res == HResult.Ok)
                {
                    shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, out mm.handle);

                    fp = (string)mm;

                    ParsingName   = fp;
                    CanonicalName = fp;

                    mm.CoTaskMemFree();
                    shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, out mm.handle);

                    DisplayName = (string)mm;
                    mm.CoTaskMemFree();

                    _IsSpecial = true;

                    if (initialize)
                    {
                        Refresh(_IconSize);
                    }
                    else
                    {
                        _Folders.Add(new DirectoryObject());
                        OnPropertyChanged(nameof(Folders));
                    }

                    return;
                }

                HResult localSHCreateItemFromParsingName()
                {
                    var argriid = Guid.Parse(ShellIIDGuid.IShellItem); var ret = NativeShell.SHCreateItemFromParsingName("shell:" + (fp ?? parsingName), IntPtr.Zero, ref argriid, ref shitem); return(ret);
                }

                res = localSHCreateItemFromParsingName();
                if (res == HResult.Ok)
                {
                    shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, out mm.handle);

                    CanonicalName = (string)mm;

                    if (ParsingName is null)
                    {
                        ParsingName = (string)mm;
                    }
                    _Path = ParsingName;
                    mm.CoTaskMemFree();

                    shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, out mm.handle);

                    DisplayName = (string)mm;
                    mm.CoTaskMemFree();
                }

                shitem = null;
                if (!string.IsNullOrEmpty(DisplayName) && !string.IsNullOrEmpty(parsingName))
                {
                    _IsSpecial = true;
                    if (initialize)
                    {
                        Refresh(_IconSize);
                    }
                    else
                    {
                        _Folders.Add(new DirectoryObject());
                        OnPropertyChanged(nameof(Folders));
                    }

                    return;
                }
            }

            if (!string.IsNullOrEmpty(ParsingName))
            {
                _Path = ParsingName;
            }
            else
            {
                _Path       = parsingName;
                ParsingName = parsingName;
            }

            if (System.IO.Directory.Exists(_Path) == false)
            {
                return;
            }
            // Throw New DirectoryNotFoundException("Directory Not Found: " & _Path)
            else
            {
                DisplayName   = Path.GetFileName(_Path);
                CanonicalName = _Path;
                parsingName   = _Path;
            }

            if (initialize)
            {
                Refresh(_IconSize);
            }
            else
            {
                _Folders.Add(new DirectoryObject());
                OnPropertyChanged(nameof(Folders));
            }
        }
        /// <summary>
        /// Refresh the contents of the directory.
        /// </summary>
        public void Refresh(StandardIcons?iconSize = default)
        {
            if (iconSize is null)
            {
                iconSize = _IconSize;
            }

            _Children.Clear();
            _Folders.Clear();

            FileObject      fobj;
            DirectoryObject dobj;
            IShellItem      shitem = null;
            IShellFolder    shfld;
            IEnumIDList     enumer;

            MemPtr mm;
            var    mm2 = new MemPtr();

            string fp;

            string pname = ParsingName;

            if (pname is object && pname.LastIndexOf(@"\") == pname.Length - 1)
            {
                pname = pname.Substring(0, pname.Length - 1);
            }

            var argriid = Guid.Parse(ShellIIDGuid.IShellItem);
            var res     = NativeShell.SHCreateItemFromParsingName(ParsingName, IntPtr.Zero, ref argriid, ref shitem);

            _IconSize = (StandardIcons)iconSize;

            int?argiIndex = null;

            _Icon      = Resources.GetFileIcon(ParsingName, FileObject.StandardToSystem(_IconSize), iIndex: ref argiIndex);
            _IconImage = Resources.GetFileIconWPF(ParsingName, FileObject.StandardToSystem(_IconSize));

            if (res == HResult.Ok)
            {
                var argbhid  = Guid.Parse(ShellBHIDGuid.ShellFolderObject);
                var argriid1 = Guid.Parse(ShellIIDGuid.IShellFolder2);

                shitem.BindToHandler(IntPtr.Zero, ref argbhid, ref argriid1, out shfld);
                _SysInterface = shfld;

                shfld.EnumObjects(IntPtr.Zero, ShellFolderEnumerationOptions.Folders | ShellFolderEnumerationOptions.IncludeHidden | ShellFolderEnumerationOptions.NonFolders | ShellFolderEnumerationOptions.InitializeOnFirstNext, out enumer);

                if (enumer != null)
                {
                    var    glist = new List <string>();
                    uint   cf;
                    var    x = IntPtr.Zero;
                    string pout;

                    // mm.AllocCoTaskMem((MAX_PATH * 2) + 8)

                    mm2.Alloc(NativeShell.MAX_PATH * 2 + 8);

                    do
                    {
                        cf = 0U;
                        mm2.ZeroMemory(0L, NativeShell.MAX_PATH * 2 + 8);
                        res = enumer.Next(1U, out x, out cf);
                        mm  = x;

                        if (cf == 0L)
                        {
                            break;
                        }

                        if (res != HResult.Ok)
                        {
                            break;
                        }

                        mm2.IntAt(0L) = 2;

                        // shfld.GetAttributesOf(1, mm, attr)
                        shfld.GetDisplayNameOf(mm, (uint)ShellItemDesignNameOptions.ParentRelativeParsing, mm2.handle);
                        MemPtr inv;

                        if (IntPtr.Size == 4)
                        {
                            inv = (IntPtr)mm2.IntAt(1L);
                        }
                        else
                        {
                            inv = (IntPtr)mm2.LongAt(1L);
                        }

                        if (inv.Handle != IntPtr.Zero)
                        {
                            if (inv.CharAt(0L) != '\0')
                            {
                                fp = (string)inv;
                                var lpInfo = new SHFILEINFO();

                                // Dim sgfin As ShellFileGetAttributesOptions = 0,
                                // sgfout As ShellFileGetAttributesOptions = 0

                                int iFlags = User32.SHGFI_PIDL | User32.SHGFI_ATTRIBUTES;
                                lpInfo.dwAttributes = 0;
                                x = User32.SHGetItemInfo(mm.Handle, 0, ref lpInfo, Marshal.SizeOf(lpInfo), iFlags);
                                if (ParsingName is object)
                                {
                                    if (pname.LastIndexOf(@"\") == pname.Length - 1)
                                    {
                                        pname = pname.Substring(0, pname.Length - 1);
                                    }
                                    pout = $@"{pname}\{fp}";
                                }
                                else
                                {
                                    pout = fp;
                                }

                                if (lpInfo.dwAttributes == 0)
                                {
                                    lpInfo.dwAttributes = (int)FileTools.GetFileAttributes(pout);
                                }

                                FileAttributes drat = (FileAttributes)(int)(lpInfo.dwAttributes);
                                if ((lpInfo.dwAttributes & (int)FileAttributes.Directory) == (int)FileAttributes.Directory && !File.Exists(pout))
                                {
                                    dobj          = new DirectoryObject(pout, _IsSpecial, false);
                                    dobj.Parent   = this;
                                    dobj.IconSize = _IconSize;
                                    _Children.Add(dobj);
                                    _Folders.Add(dobj);
                                }
                                else
                                {
                                    fobj          = new FileObject(pout, _IsSpecial, true, _IconSize);
                                    fobj.Parent   = this;
                                    fobj.IconSize = _IconSize;
                                    _Children.Add(fobj);
                                }
                            }

                            inv.CoTaskMemFree();
                        }

                        mm.CoTaskMemFree();
                    }while (res == HResult.Ok);
                    mm2.Free();
                }
            }

            OnPropertyChanged(nameof(Folders));
            OnPropertyChanged(nameof(Icon));
            OnPropertyChanged(nameof(IconImage));
            OnPropertyChanged(nameof(IconSize));
            OnPropertyChanged(nameof(ParsingName));
            OnPropertyChanged(nameof(DisplayName));
        }
Beispiel #7
0
        /// <summary>
        /// Construct an icon from the raw image data.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        private Icon _constructIcon()
        {
            Icon _constructIconRet = default;

            if (_hIcon != IntPtr.Zero)
            {
                User32.DestroyIcon(_hIcon);
                _hIcon = IntPtr.Zero;
            }

            MemPtr mm  = (MemPtr)_image;
            var    bmp = mm.ToStruct <BITMAPINFOHEADER>();

            IntPtr hBmp;
            IntPtr ptr;
            IntPtr ppBits = new IntPtr();

            var lpicon = default(ICONINFO);

            IntPtr hicon;
            IntPtr hBmpMask = new IntPtr();

            bool hasMask;

            if (bmp.biHeight == bmp.biWidth * 2)
            {
                hasMask      = true;
                bmp.biHeight = (int)(bmp.biHeight / 2d);
            }
            else
            {
                hasMask = false;
            }

            bmp.biSizeImage = (int)(bmp.biWidth * bmp.biHeight * (bmp.biBitCount / 8d));

            bmp.biXPelsPerMeter = (int)(24.5d * 1000d);
            bmp.biYPelsPerMeter = (int)(24.5d * 1000d);

            bmp.biClrUsed      = 0;
            bmp.biClrImportant = 0;
            bmp.biPlanes       = 1;

            Marshal.StructureToPtr(bmp, mm.Handle, false);

            ptr = mm.Handle + bmp.biSize;

            if (bmp.biSize != 40)
            {
                return(null);
            }

            hBmp = User32.CreateDIBSection(IntPtr.Zero, mm.Handle, 0U, ref ppBits, IntPtr.Zero, 0);

            Native.MemCpy(ptr, ppBits, bmp.biSizeImage);

            if (hasMask)
            {
                ptr             = ptr + bmp.biSizeImage;
                bmp.biBitCount  = 1;
                bmp.biSizeImage = 0;
                Marshal.StructureToPtr(bmp, mm.Handle, false);
                hBmpMask = User32.CreateDIBSection(IntPtr.Zero, mm.Handle, 0U, ref ppBits, IntPtr.Zero, 0);
                Native.MemCpy(ptr, ppBits, (long)(Math.Max(bmp.biWidth, 32) * bmp.biHeight / 8d));
            }

            lpicon.fIcon    = 1;
            lpicon.hbmColor = hBmp;
            lpicon.hbmMask  = hBmpMask;
            hicon           = User32.CreateIconIndirect(ref lpicon);
            NativeShell.DeleteObject(hBmp);
            if (hasMask)
            {
                NativeShell.DeleteObject(hBmpMask);
            }
            _constructIconRet = Icon.FromHandle(hicon);
            _hIcon            = hicon;
            mm.Free();
            return(_constructIconRet);
        }
Beispiel #8
0
        private void AssignNewNativeObject(IP_ADAPTER_ADDRESSES nativeObject, bool noCreateIcon = false)
        {
            // Store the native object.
            Source = nativeObject;

            if (!noCreateIcon)
            {
                // First thing's first... let's get the icon for the object from its parsing name.
                // Which is magically the parsing name of the network device list and the adapter's GUID name.
                string s  = @"::{7007ACC7-3202-11D1-AAD2-00805FC1270E}\" + AdapterName;
                var    mm = new MemPtr();

                NativeShell.SHParseDisplayName(s, IntPtr.Zero, out mm.handle, 0, out _);

                if (mm.Handle != IntPtr.Zero)
                {
                    // Get a WPFImage

                    // string library = @"%systemroot%\system32\shell32.dll"


                    if (OperStatus == IF_OPER_STATUS.IfOperStatusUp)
                    {
                        //if (HasInternet == InternetStatus.HasInternet)
                        //{
                        //    var icn = Resources.LoadLibraryIcon(Environment.ExpandEnvironmentVariables(@"%systemroot%\system32\netcenter.dll") + ",2", StandardIcons.Icon16);
                        //    var icn2 = Resources.GetItemIcon(mm, Resources.SystemIconSizes.ExtraLarge);

                        //    var bmp = new System.Drawing.Bitmap(icn2.Width, icn2.Height,  System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        //    var g = System.Drawing.Graphics.FromImage(bmp);

                        //    g.DrawIcon(icn2, 0, 0);
                        //    g.DrawIcon(icn, 0, icn2.Height - 16);

                        //    g.Dispose();

                        //    _Icon = Resources.MakeWPFImage(bmp);
                        //    bmp.Dispose();
                        //    icn.Dispose();
                        //    icn2.Dispose();
                        //}
                        //else
                        //{


                        _Icon = Resources.MakeWPFImage(Resources.GetItemIcon(mm, Resources.SystemIconSizes.ExtraLarge));

                        //}
                    }
                    else
                    {
                        _Icon = Resources.MakeWPFImage((System.Drawing.Bitmap)Resources.GrayIcon(Resources.GetItemIcon(mm, Resources.SystemIconSizes.ExtraLarge)));
                    }
                    mm.Free();

                    _canShowNet = true;
                }
                else
                {
                    _canShowNet = false;
                }
            }

            OnPropertyChanged(nameof(ReceiveLinkSpeed));
            OnPropertyChanged(nameof(TransmitLinkSpeed));
            OnPropertyChanged(nameof(OperStatus));

            foreach (var pr in allProps)
            {
                if (pr.Name.Contains("Address"))
                {
                    OnPropertyChanged(pr.Name);
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Advanced initialization of FileObject.  Use this for items in special folders.
        /// </summary>
        /// <param name="parsingName">The shell parsing name of the file.</param>
        /// <param name="isSpecial">Is the file known to be special?</param>
        /// <param name="initialize">True to get file info and load icons.</param>
        /// <param name="iconSize">Default icon size.  This can be changed with the <see cref="IconSize"/> property.</param>
        public FileObject(string parsingName, bool isSpecial, bool initialize, StandardIcons iconSize = StandardIcons.Icon48)
        {
            this.isSpecial = isSpecial;
            filename       = parsingName;
            try
            {
                var     mm   = new MemPtr();
                var     riid = Guid.Parse(ShellIIDGuid.IShellItem);
                HResult res;

                if (this.isSpecial)
                {
                    // let's see if we can parse it.
                    IShellItem   shitem  = null;
                    IShellItemPS pssh    = null;
                    IShellItem2  shitem2 = null;

                    res = NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref riid, ref shitem);
                    string fp = null;

                    if (res == HResult.Ok)
                    {
                        NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref riid, ref pssh);
                        psshellObj = pssh;

                        riid = Guid.Parse(ShellIIDGuid.IShellItem2);
                        NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref riid, ref shitem2);
                        psshellObj2 = shitem2;


                        shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, out mm.handle);
                        fp = (string)mm;

                        ParsingName   = fp;
                        CanonicalName = fp;

                        mm.CoTaskMemFree();

                        shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, out mm.handle);

                        DisplayName = (string)mm;

                        mm.CoTaskMemFree();

                        this.isSpecial = true;

                        if (initialize)
                        {
                            Refresh();
                        }

                        shellObj = shitem;

                        return;
                    }

                    HResult localSHCreateItemFromParsingName()
                    {
                        riid = Guid.Parse(ShellIIDGuid.IShellItem); var ret = NativeShell.SHCreateItemFromParsingName("shell:" + (fp ?? parsingName), IntPtr.Zero, ref riid, ref shitem); return(ret);
                    }

                    res = localSHCreateItemFromParsingName();

                    if (res == HResult.Ok)
                    {
                        shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, out mm.handle);
                        CanonicalName = (string)mm;

                        if (ParsingName is null)
                        {
                            ParsingName = (string)mm;
                        }

                        filename = ParsingName;
                        mm.CoTaskMemFree();


                        shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, out mm.handle);

                        DisplayName = (string)mm;

                        mm.CoTaskMemFree();
                    }

                    shellObj = shitem;
                    shitem   = null;

                    if (!string.IsNullOrEmpty(DisplayName) && !string.IsNullOrEmpty(ParsingName))
                    {
                        this.isSpecial = true;
                        if (initialize)
                        {
                            Refresh(this.iconSize);
                        }
                        return;
                    }
                }

                if (File.Exists(parsingName) == false)
                {
                    if (!this.isSpecial)
                    {
                        throw new FileNotFoundException("File Not Found: " + parsingName);
                    }
                }
                else if (initialize)
                {
                    Refresh(this.iconSize);
                }
            }
            catch
            {
            }
        }
Beispiel #10
0
        internal bool Populate(IAssocHandler[] assoc = null, StandardIcons size = StandardIcons.Icon48)
        {
            if (assoc is null)
            {
                assoc = NativeShell.EnumFileHandlers(_Ext);
            }
            if (assoc is null)
            {
                return(false);
            }
            _Col.Clear();
            string p = null;

            if (_Parent is null)
            {
                foreach (var a in assoc)
                {
                    p = null;
                    a.GetName(out p);
                    if (File.Exists(p) == false)
                    {
                        continue;
                    }
                    _Col.Add(new UIHandler(a, _Parent));
                }
            }
            else
            {
                foreach (var a in assoc)
                {
                    p = null;
                    a.GetName(out p);
                    if (File.Exists(p) == false)
                    {
                        continue;
                    }
                    _Col.Add(_Parent.HandlerFromAssocHandler(a, _Ext));
                }
            }

            OnPropertyChanged("Handlers");
            try
            {
                var         pk = Registry.ClassesRoot.OpenSubKey(_Ext);
                RegistryKey pk2;
                if (pk is object && (string)(pk.GetValue(null)) is object)
                {
                    pk2 = Registry.ClassesRoot.OpenSubKey((string)(pk.GetValue(null)));
                    if (pk2 is object)
                    {
                        string d = (string)(pk2.GetValue(null));
                        if (string.Equals(d, _Desc) == false)
                        {
                            _Desc = (string)(pk2.GetValue(null));
                            OnPropertyChanged("Description");
                        }

                        pk2.Close();
                        pk2 = Registry.ClassesRoot.OpenSubKey((string)(pk.GetValue(null)) + @"\DefaultIcon");
                        pk.Close();
                        if (pk2 is object)
                        {
                            d = (string)(pk2.GetValue(null));
                            pk2.Close();
                            if (d is object)
                            {
                                int i = d.LastIndexOf(",");
                                int c;
                                if (i == -1)
                                {
                                    c = 0;
                                }
                                else
                                {
                                    c = int.Parse(d.Substring(i + 1));
                                    d = d.Substring(0, i);
                                }

                                _DefaultIcon = Resources.LoadLibraryIcon(d, c, size);
                                if (_DefaultIcon is object)
                                {
                                    _DefaultImage = Resources.MakeWPFImage(_DefaultIcon);
                                    OnPropertyChanged("DefaultImage");
                                    OnPropertyChanged("DefaultIcon");
                                }
                            }
                        }
                    }
                }

                if (_Desc is null || string.IsNullOrEmpty(_Desc))
                {
                    _Desc = _Ext + " file";
                }
            }
            catch
            {
            }

            var cn = _Col.ToArray();

            _Col.Clear();
            Array.Sort(cn, new UIHandlerComp());
            foreach (var cxn in cn)
            {
                _Col.Add(cxn);
            }
            return(true);
        }
Beispiel #11
0
        /// <summary>
        /// Populates the Open With menu and returns a <see cref="NativeMenu"/> object.
        /// </summary>
        /// <param name="fileName">The file name whose menu to retrieve.</param>
        /// <param name="openWithCmd">The menu item id of the Open With menu item of the parent menu.</param>
        /// <param name="hMenu">The handle of the parent menu.</param>
        /// <returns></returns>
        public static NativeMenu GetOpenWithMenu(string fileName, IntPtr openWithCmd, IntPtr hMenu)
        {
            // Create a native context menu submenu populated with "open with" items
            var col = new MenuItemBagCollection();
            var nm  = new NativeMenu(hMenu);

            string ext = Path.GetExtension(fileName).ToLower();

            NativeMenuItem nmi;
            var            assoc = NativeShell.EnumFileHandlers(ext);

            nm.Items.Clear();

            if (assoc is null)
            {
                nm.Destroy();
                return(null);
            }

            foreach (IAssocHandler handler in assoc)
            {
                Icon icn;

                string pth = null;

                int idx;

                string uiname   = null;
                string pathname = null;

                handler.GetIconLocation(out pth, out idx);

                int iix = (int)NativeShell.Shell_GetCachedImageIndex(pth, idx, 0U);

                icn = Resources.GetFileIconFromIndex(iix, (Resources.SystemIconSizes)(int)(User32.SHIL_SMALL));

                handler.GetName(out pathname);

                if (File.Exists(pathname) == false)
                {
                    continue;
                }

                handler.GetUIName(out uiname);

                if (icn is null)
                {
                    nmi = nm.Items.Add(uiname);
                }
                else
                {
                    nmi = nm.Items.Add(uiname, icn);
                }

                col.Add(new MenuItemBag(nmi, handler));
            }

            nm.Items.Add(null);
            nmi    = nm.Items.Add("&Choose default program...");
            nmi.Id = (int)openWithCmd;
            nm.Bag = col;
            return(nm);
        }