private static CollumnInfo[] GetCollumnInfo(ShellFolder2 sf2)
        {
            List <CollumnInfo> retVal = new List <CollumnInfo>();
            uint i = 1;

            while (i < 500)
            {
                ShellDll.SHCOLSTATEF state;
                int hr = sf2.GetDefaultColumnState(i, out state);
                if (hr == -2147316575) //COMException, Out of Bounds
                {
                    break;
                }

                PropertyKey propKey;
                hr = sf2.MapColumnToSCID(i, out propKey);
                if (hr == -2147316575) //COMException, Out of Bounds
                {
                    break;
                }

                retVal.Add(new CollumnInfo()
                {
                    ColumnIndex = i, Flags = state, PropertyKey = propKey
                });

                i++;
            }
            return(retVal.ToArray());
        }
        public static void InvokeCommand(DirectoryInfoEx parent, IntPtr[] pidls, uint cmd, Point ptInvoke)
        {
            IntPtr       icontextMenuPtr;
            IContextMenu iContextMenu;

            //0.15: Fixed ShellFolder not freed.
            using (ShellFolder2 parentShellFolder = parent.ShellFolder)
                if (GetIContextMenu(parentShellFolder, pidls, out icontextMenuPtr, out iContextMenu))
                {
                    try
                    {
                        InvokeCommand(
                            iContextMenu,
                            cmd,
                            parent.FullName,
                            ptInvoke);
                    }
                    catch (Exception) { }
                    finally
                    {
                        if (iContextMenu != null)
                        {
                            Marshal.ReleaseComObject(iContextMenu);
                        }

                        if (icontextMenuPtr != IntPtr.Zero)
                        {
                            Marshal.Release(icontextMenuPtr);
                        }
                    }
                }
        }
Example #3
0
        //#region IStream/IStorage

        //public static bool GetIStream(ShellItem item, out IntPtr streamPtr, out IStream stream)
        //{
        //    if (item.ParentItem.ShellFolder.BindToStorage(
        //                item.PIDLRel.Ptr,
        //                IntPtr.Zero,
        //                ref ShellAPI.IID_IStream,
        //                out streamPtr) == ShellAPI.S_OK)
        //    {
        //        stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
        //        return true;
        //    }
        //    else
        //    {
        //        stream = null;
        //        streamPtr = IntPtr.Zero;
        //        return false;
        //    }
        //}

        //public static bool GetIStorage(ShellItem item, out IntPtr storagePtr, out IStorage storage)
        //{
        //    if (item.ParentItem.ShellFolder.BindToStorage(
        //                item.PIDLRel.Ptr,
        //                IntPtr.Zero,
        //                ref ShellAPI.IID_IStorage,
        //                out storagePtr) == ShellAPI.S_OK)
        //    {
        //        storage = (IStorage)Marshal.GetTypedObjectForIUnknown(storagePtr, typeof(IStorage));
        //        return true;
        //    }
        //    else
        //    {
        //        storage = null;
        //        storagePtr = IntPtr.Zero;
        //        return false;
        //    }
        //}

        //#endregion

        #region Drag/Drop

        /// <summary>
        /// This method will use the GetUIObjectOf method of IShellFolder to obtain the IDataObject of a
        /// ShellItem.
        /// </summary>
        /// <param name="item">The item for which to obtain the IDataObject</param>
        /// <param name="dataObjectPtr">A pointer to the returned IDataObject</param>
        /// <returns>the IDataObject the ShellItem</returns>
        public static IntPtr GetIDataObject(FileSystemInfoEx[] items)
        {
            DirectoryInfoEx parent = items[0].Parent != null ? items[0].Parent : DirectoryInfoEx.DesktopDirectory;

            IntPtr[] pidls = new IntPtr[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                pidls[i] = items[i].PIDLRel.Ptr;
            }

            IntPtr dataObjectPtr;

            //0.15: Fixed ShellFolder not freed.
            using (ShellFolder2 parentShellFolder = parent.ShellFolder)
                if (parentShellFolder.GetUIObjectOf(
                        IntPtr.Zero,
                        (uint)pidls.Length,
                        pidls,
                        ref ShellAPI.IID_IDataObject,
                        IntPtr.Zero,
                        out dataObjectPtr) == ShellAPI.S_OK)
                {
                    return(dataObjectPtr);
                }
                else
                {
                    return(IntPtr.Zero);
                }
        }
 public static object GetProperty(FileInfoEx file, PropertyKey propKey)
 {
     if (file.Exists)
     {
         using (ShellFolder2 sf2 = file.Parent.ShellFolder)
         {
             ShellDll.PropertyKey pkey = propKey;
             return(GetProperty(sf2, file, ref pkey));
         }
     }
     return(null);
 }
 public static bool GetIExtractImage(
     FileInfoEx file,
     out IntPtr iExtractImagePtr,
     out IExtractImage iExtractImage)
 {
     using (ShellFolder2 sf2 = file.Parent.ShellFolder)
     {
         PIDL filePIDL = file.PIDLRel;
         try
         {
             return(GetIExtractImage(sf2, filePIDL.Ptr, out iExtractImagePtr, out iExtractImage));
         }
         finally { filePIDL.Free(); }
     }
 }
 public static void InvokeCommand(DirectoryInfoEx parent, FileSystemInfoEx[] entries, uint cmd, Point ptInvoke)
 {
     PIDL[]   pidls = IOTools.GetPIDL(entries, true);
     IntPtr[] ptrs  = IOTools.GetPIDLPtr(pidls);
     using (ShellFolder2 parentShellFolder = parent.ShellFolder)
     {
         try
         {
             InvokeCommand(parent, ptrs, cmd, ptInvoke);
         }
         finally
         {
             IOTools.FreePIDL(pidls);
         }
     }
 }
        public static object GetProperty(ShellFolder2 sf2, FileInfoEx file, ref PropertyKey propKey)
        {
            PropertyKey pKey   = propKey;
            var         output = file.RequestRelativePIDL(relPidl =>
            {
                object retVal = null;
                if (relPidl != null)
                {
                    int hr = sf2.GetDetailsEx(relPidl.Ptr, ref pKey, out retVal);
                    if (hr != ShellAPI.S_OK)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }
                }
                return(retVal);
            });

            propKey = pKey;
            return(output);
        }
        public static object GetProperty(ShellFolder2 sf2, FileInfoEx file, ref PropertyKey propKey)
        {
            PIDL pidlLookup = file.PIDLRel;

            if (pidlLookup != null)
            {
                try
                {
                    object retVal;
                    int    hr = sf2.GetDetailsEx(pidlLookup.Ptr, ref propKey, out retVal);
                    if (hr != ShellAPI.S_OK)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    return(retVal);
                }
                finally
                {
                    pidlLookup.Free();
                }
            }
            return(null);
        }
Example #9
0
        //#region IStream/IStorage

        //public static bool GetIStream(ShellItem item, out IntPtr streamPtr, out IStream stream)
        //{
        //    if (item.ParentItem.ShellFolder.BindToStorage(
        //                item.PIDLRel.Ptr,
        //                IntPtr.Zero,
        //                ref ShellAPI.IID_IStream,
        //                out streamPtr) == ShellAPI.S_OK)
        //    {
        //        stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
        //        return true;
        //    }
        //    else
        //    {
        //        stream = null;
        //        streamPtr = IntPtr.Zero;
        //        return false;
        //    }
        //}

        //public static bool GetIStorage(ShellItem item, out IntPtr storagePtr, out IStorage storage)
        //{
        //    if (item.ParentItem.ShellFolder.BindToStorage(
        //                item.PIDLRel.Ptr,
        //                IntPtr.Zero,
        //                ref ShellAPI.IID_IStorage,
        //                out storagePtr) == ShellAPI.S_OK)
        //    {
        //        storage = (IStorage)Marshal.GetTypedObjectForIUnknown(storagePtr, typeof(IStorage));
        //        return true;
        //    }
        //    else
        //    {
        //        storage = null;
        //        storagePtr = IntPtr.Zero;
        //        return false;
        //    }
        //}

        //#endregion

        #region Drag/Drop

        /// <summary>
        /// This method will use the GetUIObjectOf method of IShellFolder to obtain the IDataObject of a
        /// ShellItem.
        /// </summary>
        /// <param name="item">The item for which to obtain the IDataObject</param>
        /// <param name="dataObjectPtr">A pointer to the returned IDataObject</param>
        /// <returns>the IDataObject the ShellItem</returns>
        public static IntPtr GetIDataObject(FileSystemInfoEx[] items)
        {
            DirectoryInfoEx parent = items[0].Parent != null ? items[0].Parent : DirectoryInfoEx.DesktopDirectory;
            IntPtr          retVal = IntPtr.Zero;

            items.RequestPIDL((pidls, ptrs) =>
            {
                IntPtr dataObjectPtr;
                //0.15: Fixed ShellFolder not freed.
                using (ShellFolder2 parentShellFolder = parent.ShellFolder)
                    if (parentShellFolder.GetUIObjectOf(
                            IntPtr.Zero,
                            (uint)ptrs.Length,
                            ptrs,
                            ref ShellAPI.IID_IDataObject,
                            IntPtr.Zero,
                            out dataObjectPtr) == ShellAPI.S_OK)
                    {
                        retVal = dataObjectPtr;
                    }
            });

            return(retVal);
        }
 public static CollumnInfo[] GetCollumnInfo(DirectoryInfoEx dir)
 {
     using (ShellFolder2 sf2 = dir.ShellFolder)
         return(GetCollumnInfo(sf2));
 }
        public string Popup(FileSystemInfoEx[] items, Point pt)
        {
            if (items.Length > 0 && !_contextMenuVisible)
            {
                //0.15: Fixed ShellFolder not freed correctly.
                try
                {
                    using (ShellFolder2 parentShellFolder = items[0].Parent != null ?
                                                            items[0].Parent.ShellFolder :
                                                            DirectoryInfoEx.DesktopDirectory.ShellFolder)
                        try
                        {
                            _contextMenuVisible = true;

                            ///Debug.WriteLine(items[0].Parent.FullName);

                            IntPtr        ptrContextMenu     = IntPtr.Zero;
                            IntPtr        ptrContextMenu2    = IntPtr.Zero;
                            IntPtr        PtrContextMenu3    = IntPtr.Zero;
                            IntPtr        contextMenu        = IntPtr.Zero;
                            List <IntPtr> menuPtrConstructed = new List <IntPtr>();
                            List <IntPtr> imgPtrConstructed  = new List <IntPtr>();

                            PIDL[] pidls = IOTools.GetPIDL(items, true);

                            if (ContextMenuHelper.GetIContextMenu(parentShellFolder, IOTools.GetPIDLPtr(pidls),
                                                                  out ptrContextMenu, out _iContextMenu))
                            {
                                try
                                {
                                    queryMenuItemsEventArgs = new QueryMenuItemsEventArgs(items);
                                    if (OnQueryMenuItems != null)
                                    {
                                        OnQueryMenuItems(this, queryMenuItemsEventArgs);
                                    }

                                    contextMenu = ShellAPI.CreatePopupMenu();

                                    if (queryMenuItemsEventArgs.QueryContextMenu)
                                    {
                                        _iContextMenu.QueryContextMenu(contextMenu, 0, ShellAPI.CMD_FIRST,
                                                                       ShellAPI.CMD_LAST, ShellAPI.CMF.EXPLORE | ShellAPI.CMF.CANRENAME |
                                                                       ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellAPI.CMF.EXTENDEDVERBS : 0));
                                    }

                                    #region obsolute
                                    //for (uint i = 0; i < queryMenuItemsEventArgs.ExtraMenuItems.Length; i++)
                                    //{
                                    //    string caption = queryMenuItemsEventArgs.ExtraMenuItems[i];
                                    //    if (caption != "---")
                                    //    {
                                    //        ShellAPI.InsertMenu(contextMenu, i, ShellAPI.MFT.BYPOSITION,
                                    //            ShellAPI.CMD_LAST + i + 1, caption);
                                    //        if (queryMenuItemsEventArgs.DefaultItem == i)
                                    //            ShellAPI.SetMenuDefaultItem(contextMenu, i, true);
                                    //    }
                                    //    else ShellAPI.InsertMenu(contextMenu, i, ShellAPI.MFT.BYPOSITION |
                                    //        ShellAPI.MFT.SEPARATOR, 0, "-");
                                    //}
                                    #endregion

                                    //0.11: Added ContextMenuWrapper OnQueryMenuItems event now support multilevel directory. (e.g. @"Tools\Add")
                                    ContextMenuHelperEx.ConstructCustomMenu(contextMenu, queryMenuItemsEventArgs.ExtraMenuItems, out menuPtrConstructed, out imgPtrConstructed);

                                    if (queryMenuItemsEventArgs.QueryContextMenu2)
                                    {
                                        try
                                        {
                                            Marshal.QueryInterface(ptrContextMenu, ref ShellAPI.IID_IContextMenu2,
                                                                   out ptrContextMenu2);
                                            _iContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(
                                                ptrContextMenu2, typeof(IContextMenu2));
                                        }
                                        catch (Exception) { }
                                    }

                                    if (queryMenuItemsEventArgs.QueryContextMenu3)
                                    {
                                        try
                                        {
                                            Marshal.QueryInterface(ptrContextMenu, ref ShellAPI.IID_IContextMenu3,
                                                                   out PtrContextMenu3);

                                            _iContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(
                                                PtrContextMenu3, typeof(IContextMenu3));
                                        }
                                        catch (Exception) { }
                                    }


                                    uint GMDI_USEDISABLED = 0x0001;
                                    //uint GMDI_GOINTOPOPUPS = 0x0002;
                                    uint intDefaultItem = (uint)ShellAPI.GetMenuDefaultItem(contextMenu, false, GMDI_USEDISABLED);

                                    string strDefaultCommand = intDefaultItem >= ShellAPI.CMD_FIRST ?
                                                               ContextMenuHelper.GetCommandString(_iContextMenu, intDefaultItem - ShellAPI.CMD_FIRST, true) : null;



                                    if (queryMenuItemsEventArgs.QueryContextMenu) //No need to Disable if query is not carried out in first place.
                                    {
                                        //0.11: Added queryMenuItemsEventArgs.GrayedItems / HiddenItems
                                        ContextMenuHelperEx.DisableMenuItems(contextMenu, _iContextMenu,
                                                                             queryMenuItemsEventArgs.GrayedItems, ContextMenuHelperEx.DisabledMethods.Gray);
                                        ContextMenuHelperEx.DisableMenuItems(contextMenu, _iContextMenu,
                                                                             queryMenuItemsEventArgs.HiddenItems, ContextMenuHelperEx.DisabledMethods.Remove);
                                    }

                                    //0.17: Added DefaultItem and DefaultCommand in BeforePopup
                                    bool cont = true;
                                    if (OnBeforePopup != null)
                                    {
                                        BeforePopupEventArgs args = new BeforePopupEventArgs
                                                                        (contextMenu, _iContextMenu, intDefaultItem - ShellAPI.CMD_FIRST, strDefaultCommand);
                                        OnBeforePopup(this, args);
                                        cont = args.ContinuePopup;
                                    }

                                    if (cont)
                                    {
                                        //0.18 Fixed Context menu disappear in some case. (By cwharmon)
                                        //http://www.codeproject.com/KB/files/DirectoryInfoEx.aspx#xx3475961xx
                                        ShellAPI.SetForegroundWindow(this.Handle);

                                        uint selected = ShellAPI.TrackPopupMenuEx(contextMenu, ShellAPI.TPM.RETURNCMD,
                                                                                  pt.X, pt.Y, this.Handle, IntPtr.Zero);

                                        uint msg = 0;
                                        ShellAPI.PostMessage(this.Handle, msg, IntPtr.Zero, IntPtr.Zero);

                                        if (OnMouseHover != null)
                                        {
                                            OnMouseHover(this, new MouseHoverEventArgs("", "", 0));
                                        }

                                        if (selected >= ShellAPI.CMD_LAST)
                                        {
                                            return(removeCheckedSymbol(queryMenuItemsEventArgs.ExtraMenuItems[selected - ShellAPI.CMD_LAST]));
                                        }

                                        if (selected >= ShellAPI.CMD_FIRST)
                                        {
                                            string command = ContextMenuHelper.GetCommandString(_iContextMenu,
                                                                                                selected - ShellAPI.CMD_FIRST, true);
                                            if (command == null)
                                            {
                                                return(null);
                                            }

                                            if (OnBeforeInvokeCommand != null)
                                            {
                                                InvokeCommandEventArgs args =
                                                    new InvokeCommandEventArgs(command, "", selected, items);
                                                OnBeforeInvokeCommand(this, args);
                                                if (!args.ContinueInvoke)
                                                {
                                                    return(command);
                                                }
                                            }

                                            if (command == "rename")
                                            {
                                                return("rename");
                                            }
                                            else
                                            {
                                                //if (items.Length == 1 && items[0] is DirectoryInfoEx)
                                                ContextMenuHelper.InvokeCommand(_iContextMenu,
                                                                                selected - ShellAPI.CMD_FIRST,
                                                                                (items[0].Parent != null) ? items[0].Parent.FullName :
                                                                                items[0].FullName, pt);
                                                //else
                                                //ContextMenuHelper.InvokeCommand(items[0].Parent,
                                                //    IOTools.GetPIDLPtr(items, true), selected - ShellAPI.CMD_FIRST,
                                                //    pt);
                                            }
                                        }
                                    }
                                }
                                finally
                                {
                                    IOTools.FreePIDL(pidls);

                                    if (_iContextMenu != null)
                                    {
                                        Marshal.ReleaseComObject(_iContextMenu);
                                        _iContextMenu = null;
                                    }

                                    if (_iContextMenu2 != null)
                                    {
                                        Marshal.ReleaseComObject(_iContextMenu2);
                                        _iContextMenu2 = null;
                                    }

                                    if (_iContextMenu3 != null)
                                    {
                                        Marshal.ReleaseComObject(_iContextMenu3);
                                        _iContextMenu3 = null;
                                    }

                                    foreach (IntPtr menuPtr in menuPtrConstructed)
                                    {
                                        ShellAPI.DestroyMenu(menuPtr);
                                    }
                                    menuPtrConstructed.Clear();

                                    if (contextMenu != null)
                                    {
                                        ShellAPI.DestroyMenu(contextMenu);
                                    }

                                    if (ptrContextMenu != IntPtr.Zero)
                                    {
                                        Marshal.Release(ptrContextMenu);
                                    }

                                    if (ptrContextMenu2 != IntPtr.Zero)
                                    {
                                        Marshal.Release(ptrContextMenu2);
                                    }

                                    if (PtrContextMenu3 != IntPtr.Zero)
                                    {
                                        Marshal.Release(PtrContextMenu3);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            _contextMenuVisible = false;
                        }
                }
                catch { }
            }

            return(null);
        }
        private static CollumnInfo[] GetCollumnInfo(ShellFolder2 sf2)
        {
            List<CollumnInfo> retVal = new List<CollumnInfo>();
            uint i = 1;

            while (i < 500)
            {
                ShellDll.SHCOLSTATEF state;
                int hr = sf2.GetDefaultColumnState(i, out state);
                if (hr == -2147316575) //COMException, Out of Bounds
                    break;

                PropertyKey propKey;
                hr = sf2.MapColumnToSCID(i, out propKey);
                if (hr == -2147316575) //COMException, Out of Bounds
                    break;

                retVal.Add(new CollumnInfo() { ColumnIndex = i, Flags = state, PropertyKey = propKey });

                i++;
            }
            return retVal.ToArray();
        }
        public static object GetProperty(ShellFolder2 sf2, FileInfoEx file, ref PropertyKey propKey)
        {
            PIDL pidlLookup = file.PIDLRel;
            if (pidlLookup != null)
            {
                try
                {
                    object retVal;
                    int hr = sf2.GetDetailsEx(pidlLookup.Ptr, ref propKey, out retVal);
                    if (hr != ShellAPI.S_OK)
                        Marshal.ThrowExceptionForHR(hr);

                    return retVal;
                }
                finally
                {
                    pidlLookup.Free();
                }
            }
            return null;
        }