示例#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);
                }
            }
        }
示例#2
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);
        }