Exemple #1
0
        void InitFolder(IntPtr pidl, string dirPath, IShellFolder pShellFolderInterface,
                        bool isThrow = true)
        {
            //  Bind the shell folder interface.
            int          result = 0;
            IShellFolder shellFolderInterface = IFolder(pidl, pShellFolderInterface, ref result);

            this.ShellFolderInterface = shellFolderInterface;

            // as ShellFolderInterface2
            this.ItemInterface = null;
            if (this.IsFileSystem)
            {
                // if (RunningVista)
                this.ItemInterface = ILShell32.SHCreateItemFromParsingName(dirPath, IntPtr.Zero,
                                                                           typeof(IShellItem).GUID);
            }

            //  Validate the result.
            if (shellFolderInterface != null && result != 0)
            {
                //  Throw the failure as an exception.
                if (isThrow)
                {
                    Marshal.ThrowExceptionForHR((int)result);
                }
            }
        }
Exemple #2
0
        public IntPtr GetDisplayName(SIGDN sigdnName)
        {
            if (sigdnName == SIGDN.FILESYSPATH)
            {
                StringBuilder result = new StringBuilder(FileSystem.MAX_PATH);
                if (!ILShell32.SHGetPathFromIDList(_pidl, result))
                {
                    throw new ArgumentException();
                }
                return(Marshal.StringToHGlobalUni(result.ToString()));
            }
            else
            {
                //IShellFolder parentFolder = GetParent().GetIShellFolder();
                IntPtr        childPidl = ILShell32.ILFindLastID(_pidl);
                StringBuilder builder   = new StringBuilder(FileSystem.MAX_PATH);
                builder.Length = 0; // TODO
                //STRRET strret = new STRRET();

                //TODO
                //parentFolder.GetDisplayNameOf(childPidl,
                //    (SHGNO)((int)sigdnName & 0xffff), out strret);
                //ShlWapi.StrRetToBuf(ref strret, childPidl, builder,
                //    (uint)builder.Capacity);
                return(Marshal.StringToHGlobalUni(builder.ToString()));
            }
        }
Exemple #3
0
        public static IntPtr ParseDesktopContext(ShellItem itemHit, IShellFolder shellFolder, System.Windows.Forms.Control ctrl)
        {
            IntPtr ppv = IntPtr.Zero;

            //  The item pidl is either the folder if the item is a folder, or the combined pidl otherwise.
            if (ILShell32.ILIsEqual(itemHit.PIDL, ShellItem.Desktop.PIDL))
            {
                var fullIdList = itemHit.IsFolder
                    ? PidlManager.PidlToIdlist(itemHit.PIDL)
                    : PidlManager.Combine(PidlManager.PidlToIdlist(itemHit.ParentItem.PIDL),
                                          PidlManager.PidlToIdlist(itemHit.RelativePIDL));

                //  Get the UI object of the context menu.
                // IntPtr itemPidl = PidlManager.IdListToPidl(fullIdList);
                IntPtr   itemPidl = itemHit.RelativePIDL;
                IntPtr[] apidl    = new IntPtr[] { itemPidl };
                // PidlManager.PidlsToAPidl(new IntPtr[] { itemPidl })

                shellFolder.GetUIObjectOf(ctrl.Handle, (uint)1,
                                          apidl,
                                          ref Shell32.IID_IContextMenu, 0,
                                          out ppv);
            }

            return(ppv);
        }
Exemple #4
0
        public void Initialise(IntPtr pidl, string path)
        {
            this.PIDL         = pidl;
            this.RelativePIDL = ILShell32.ILFindLastID(pidl);

            IntPtr folderPidl = PIDL;

            SHFILEINFO?info = PidlManager.GetShFileInfo(pidl);
            var        dir  = System.IO.Path.GetDirectoryName(path);

            if ((info.Value.Attributes & SFGAO.SFGAO_FOLDER) == 0)
            {
                // Item folder
                var handle = PidlManager.FromPath(dir);
                folderPidl = handle.pidl;
                SHFILEINFO?info2 = PidlManager.GetShFileInfo(folderPidl);
                if (!info2.HasValue)
                {
                    return; // new DirectoryNotFoundException
                }
                ParentItem              = new ShellItem();
                ParentItem.PIDL         = folderPidl;
                ParentItem.RelativePIDL = ILShell32.ILFindLastID(folderPidl);

                ParentItem.DisplayName  = info2.Value.szDisplayName;
                ParentItem.Attributes   = (SFGAO)info2.Value.Attributes;
                ParentItem.IsFolder     = (Attributes & SFGAO.SFGAO_FOLDER) != 0;
                ParentItem.IsFileSystem = (Attributes & SFGAO.SFGAO_FILESYSTEM) != 0;
            }

            //IShellItem i = ILShell32.SHCreateItemFromParsingName(path, IntPtr.Zero,
            //           typeof(IShellItem).GUID);
            int          hr = 0;
            IShellFolder pShellFolderInterface =
                IFolder(folderPidl, desktopShellFolder.Value.ShellFolderInterface, ref hr);

            if (pShellFolderInterface == null)
            {
                throw new NullReferenceException("SheelObject ShellFolderInterface error");
            }

            DisplayName  = info.Value.szDisplayName;
            Attributes   = (SFGAO)info.Value.Attributes;
            IsFolder     = (Attributes & SFGAO.SFGAO_FOLDER) != 0;
            IsFileSystem = (Attributes & SFGAO.SFGAO_FILESYSTEM) != 0;

            if ((info.Value.Attributes & SFGAO.SFGAO_FOLDER) == 0)
            {
                this.ParentItem.InitFolder(folderPidl, dir, pShellFolderInterface, false);
            }
            else if (dir == null && path.EndsWith(@":\"))
            {
                this.InitFolder(PIDL, path, pShellFolderInterface, false);
            }
            else
            {
                this.InitFolder(PIDL, dir, pShellFolderInterface, false);
            }
        }
Exemple #5
0
        ShellItemImpl GetParent()
        {
            IntPtr pidl = Shell32.ILClone(_pidl);

            if (ILShell32.ILRemoveLastID(pidl))
            {
                return(new ShellItemImpl(pidl, true));
            }
            else
            {
                return(this);
            }
        }
Exemple #6
0
        public HResult GetParent(out IShellItem ppsi)
        {
            IntPtr pidl = Shell32.ILClone(_pidl);

            if (ILShell32.ILRemoveLastID(pidl))
            {
                ppsi = new ShellItemImpl(pidl, true);
                return(HResult.S_OK);
            }
            else
            {
                ppsi = null;
                return(HResult.MK_E_NOOBJECT);
            }
        }
Exemple #7
0
        public int Compare(IShellItem psi, SICHINT hint)
        {
            ShellItemImpl other       = (ShellItemImpl)psi;
            ShellItemImpl myParent    = GetParent();
            ShellItemImpl theirParent = other.GetParent();

            if (Shell32.ILIsEqual(myParent._pidl, theirParent._pidl))
            {
                return(myParent.GetIShellFolder().CompareIDs(
                           // (SHCIDS)
                           (IntPtr)hint,
                           ILShell32.ILFindLastID(_pidl),
                           ILShell32.ILFindLastID(other._pidl)));
            }
            else
            {
                return(1);
            }
        }
Exemple #8
0
        public static bool ShowMenu(ShellContextMenu menu, IShellFolder shellFolder, ShellItem itemHit,
                                    System.Windows.Forms.Control ctrl, System.Drawing.Point pt)
        {
            if (ctrl is ControlWindow)
            {
                (ctrl as ControlWindow).ParentMenu = menu;
            }

            var PIDL = itemHit.PIDL;

            if (itemHit.IsFolder)
            {
                IShellItem parentInterface = itemHit.ParentItem != null ? itemHit.ParentItem.ItemInterface : null;
                if (parentInterface == null)
                {
                    var parentPath = Path.GetFullPath(itemHit.Path + @"..\");
                    parentInterface = ILShell32.SHCreateItemFromParsingName(parentPath, IntPtr.Zero, typeof(IShellItem).GUID);
                }

                if (menu.ComInterface == null)
                {
                    var aPidl = PidlManager.PidlToIdlist(itemHit.PIDL);

                    if (parentInterface != null && !menu.InitFolderPath(shellFolder, parentInterface, itemHit.PIDL, itemHit.Path))
                    {
                        return(false);
                    }
                }
            }

            if (menu.ComInterface == null && !menu.InitFolder(shellFolder, PIDL))
            {
                return(false);
            }

            menu.ShowContextMenu(ctrl, pt);
            return(true);
        }