예제 #1
0
            /// <summary>Gets the icon for the item using the specified characteristics.</summary>
            /// <param name="psf">The IShellFolder from which to request the IExtractIcon instance.</param>
            /// <param name="pidl">The PIDL of the item within <paramref name="psf"/>.</param>
            /// <param name="imgSz">The width, in pixels, of the icon.</param>
            /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param>
            /// <returns>The result of function.</returns>
            public static HRESULT LoadIconFromExtractIcon(IShellFolder psf, PIDL pidl, ref uint imgSz, out SafeHICON hico)
            {
                hico = default;
                HRESULT hr = psf.GetUIObjectOf((IntPtr)pidl, out IExtractIconW ieiw);

                if (hr.Succeeded)
                {
                    try
                    {
                        return(LoadIconFromExtractIcon(ieiw, ref imgSz, out hico));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(ieiw);
                    }
                }
                else if ((hr = psf.GetUIObjectOf((IntPtr)pidl, out IExtractIconA iei)).Succeeded)
                {
                    try
                    {
                        return(LoadIconFromExtractIcon(iei, ref imgSz, out hico));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(iei);
                    }
                }
                return(hr);
            }
예제 #2
0
        public void GetContextMenus(string path)
        {
            IntPtr       desktopPtr;
            IShellFolder desktop = ShellAPI.GetDesktopFolder(out desktopPtr);

            IntPtr       ownerHwnd = IntPtr.Zero;
            IShellFolder Root;
            string       FolderPath = Directory.GetParent(path).FullName;
            IntPtr       Pidl       = IntPtr.Zero;
            IShellFolder parent;
            uint         i, j = 0;

            desktop.ParseDisplayName(ownerHwnd, IntPtr.Zero, FolderPath, out i, out Pidl, ref j);
            desktop.BindToObject(Pidl, IntPtr.Zero, ref Guids.IID_IShellFolder, out Root);
            Marshal.ReleaseComObject(desktop);

            IEnumIDList fileEnum      = null;
            IEnumIDList folderEnum    = null;
            IntPtr      fileEnumPtr   = IntPtr.Zero;
            IntPtr      folderEnumPtr = IntPtr.Zero;
            IntPtr      pidlSub;
            int         celtFetched;

            if (Root.EnumObjects(ownerHwnd, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN, out fileEnumPtr) == ShellAPI.S_OK)
            {
                fileEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(fileEnumPtr);
                while (fileEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE)
                {
                    string name = ShellAPI.GetNameByPIDL(pidlSub);
                }
            }

            if (Root.EnumObjects(ownerHwnd, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out folderEnumPtr) == ShellAPI.S_OK)
            {
                folderEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(folderEnumPtr);
                while (folderEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE)
                {
                    string name = ShellAPI.GetNameByPIDL(pidlSub);
                    if (Path.GetFileName(path) == name)
                    {
                        IntPtr       PIDL    = pidlSub;
                        IShellFolder IParent = Root;
                        IntPtr[]     pidls   = new IntPtr[1];
                        pidls[0] = PIDL;

                        //get IContextMenu interface
                        IntPtr iContextMenuPtr = IntPtr.Zero;
                        iContextMenuPtr = IParent.GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length,
                                                                pidls, ref Guids.IID_IContextMenu, out iContextMenuPtr);
                        IContextMenu iContextMenu = (IContextMenu)Marshal.GetObjectForIUnknown(iContextMenuPtr);

                        IntPtr contextMenu = ShellAPI.CreatePopupMenu();
                        iContextMenu.QueryContextMenu(contextMenu, 0, ShellAPI.CMD_FIRST, ShellAPI.CMD_LAST, CMF.NORMAL | CMF.EXPLORE);
                        ParseMenu(contextMenu);
                    }
                }
            }

            Marshal.ReleaseComObject(Root);
        }
        /// <summary>Gets the interfaces to the context menu</summary>
        /// <param name="oParentFolder">Parent folder</param>
        /// <param name="arrPIDLs">PIDLs</param>
        /// <returns>true if it got the interfaces, otherwise false</returns>
        private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs, out IntPtr ctxMenuPtr)
        {
            int nResult = oParentFolder.GetUIObjectOf(
                IntPtr.Zero,
                (uint)arrPIDLs.Length,
                arrPIDLs,
                ref IID_IContextMenu,
                IntPtr.Zero,
                out ctxMenuPtr);

            if (S_OK == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof(IContextMenu));

                /*IntPtr pUnknownContextMenu2 = IntPtr.Zero;
                 * if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu2, out pUnknownContextMenu2))
                 * {
                 *  _oContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu2, typeof(IContextMenu2));
                 * }
                 * IntPtr pUnknownContextMenu3 = IntPtr.Zero;
                 * if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu3, out pUnknownContextMenu3))
                 * {
                 *  _oContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu3, typeof(IContextMenu3));
                 * }*/

                return(true);
            }
            else
            {
                ctxMenuPtr    = IntPtr.Zero;
                _oContextMenu = null;
                return(false);
            }
        }
예제 #4
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);
        }
 public static ExtractIconWrapper FromShellFolder(IShellFolder folder, IntPtr pidl)
 {
     if (folder == null)
     {
         throw new ArgumentNullException("folder");
     }
     if (pidl == IntPtr.Zero)
     {
         throw new ArgumentException("pidl == IntPtr.Zero", "pidl");
     }
     try
     {
         object obj2;
         IntPtr[] apidl = new IntPtr[] { pidl };
         folder.GetUIObjectOf(IntPtr.Zero, 1, apidl, typeof(IExtractIconW).GUID, IntPtr.Zero, out obj2);
         if (obj2 != null)
         {
             return new ExtractIconWrapper((IExtractIconW) obj2);
         }
     }
     catch (FileNotFoundException)
     {
     }
     return null;
 }
예제 #6
0
    /// <summary>Gets the interfaces to the context menu</summary>
    /// <param name="oParentFolder">Parent folder</param>
    /// <param name="arrPIDLs">PIDLs</param>
    /// <returns>true if it got the interfaces, otherwise false</returns>
    private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs)
    {
        IntPtr pUnknownContextMenu = IntPtr.Zero;

        int nResult = oParentFolder.GetUIObjectOf(
            IntPtr.Zero,
            (uint)arrPIDLs.Length,
            arrPIDLs,
            ref IID_IContextMenu,
            IntPtr.Zero,
            out pUnknownContextMenu);

        if (S_OK == nResult)
        {
            _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu, typeof(IContextMenu));

            IntPtr pUnknownContextMenu2 = IntPtr.Zero;
            if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu2, out pUnknownContextMenu2))
            {
                _oContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu2, typeof(IContextMenu2));
            }
            IntPtr pUnknownContextMenu3 = IntPtr.Zero;
            if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu3, out pUnknownContextMenu3))
            {
                _oContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu3, typeof(IContextMenu3));
            }

            return(true);
        }
        else
        {
            return(false);
        }
    }
예제 #7
0
        public IExtractIconPWFlags GetShield()
        {
            IExtractIcon  iextract     = null;
            IShellFolder  ishellfolder = null;
            StringBuilder str          = null;
            IntPtr        result;

            try {
                var  guid = new Guid("000214fa-0000-0000-c000-000000000046");
                uint res  = 0;
                ishellfolder = this.Parent.GetIShellFolder();
                var pidls = new IntPtr[1];
                pidls[0] = Shell32.ILFindLastID(this.PIDL);
                ishellfolder.GetUIObjectOf(IntPtr.Zero, 1, pidls, ref guid, res, out result);
                iextract = (IExtractIcon)Marshal.GetTypedObjectForIUnknown(result, typeof(IExtractIcon));
                str      = new StringBuilder(512);
                var index = -1;
                IExtractIconPWFlags flags;
                iextract.GetIconLocation(IExtractIconUFlags.GIL_CHECKSHIELD, str, 512, out index, out flags);
                pidls = null;
                return(flags);
            } catch {
                return(0);
            }
        }
예제 #8
0
        public static bool GetIContextMenu(
            IShellFolder parent,
            IntPtr[] pidls,
            out IntPtr icontextMenuPtr,
            out IContextMenu iContextMenu)
        {
            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    (uint)pidls.Length,
                    pidls,
                    ref ShellFolders.IID_IContextMenu,
                    IntPtr.Zero,
                    out icontextMenuPtr) == ShellFolders.S_OK)
            {
                iContextMenu =
                    (IContextMenu)Marshal.GetTypedObjectForIUnknown(
                        icontextMenuPtr, typeof(IContextMenu));

                return(true);
            }
            else
            {
                icontextMenuPtr = IntPtr.Zero;
                iContextMenu    = null;

                return(false);
            }
        }
예제 #9
0
 /// <summary>Gets an object that can be used to carry out actions on the specified file objects or folders.</summary>
 /// <typeparam name="TInterface">The interface to retrieve, typically IShellView.</typeparam>
 /// <param name="parentWindow">The owner window that the client should specify if it displays a dialog box or message box..</param>
 /// <param name="children">The file objects or subfolders relative to the parent folder for which to get the interface.</param>
 /// <returns>The interface pointer requested.</returns>
 public TInterface GetChildrenUIObjects <TInterface>(HWND parentWindow, params ShellItem[] children) where TInterface : class
 {
     if (children is null || children.Length == 0 || children.Any(i => i is null || !IsChild(i)))
     {
         throw new ArgumentException("At least one child ShellItem instances is null or is not a child of this folder.");
     }
     return(iShellFolder.GetUIObjectOf <TInterface>(parentWindow, Array.ConvertAll(children, i => (IntPtr)i.PIDL.LastId)));
 }
예제 #10
0
 /// <summary>Gets an object that can be used to carry out actions on the specified file objects or folders.</summary>
 /// <typeparam name="TInterface">The interface to retrieve, typically IShellView.</typeparam>
 /// <param name="parentWindow">The owner window that the client should specify if it displays a dialog box or message box..</param>
 /// <param name="children">The file objects or subfolders relative to the parent folder for which to get the interface.</param>
 /// <returns>The interface pointer requested.</returns>
 public TInterface GetChildrenUIObjects <TInterface>(System.Windows.Forms.IWin32Window parentWindow, params ShellItem[] children) where TInterface : class
 {
     if (children is null || children.Length == 0 || children.Any(i => i is null || !IsChild(i)))
     {
         throw new ArgumentException("At least one child ShellItem instances is null or is not a child of this folder.");
     }
     return(iShellFolder.GetUIObjectOf(IWin2Ptr(parentWindow), (uint)children.Length, Array.ConvertAll(children, i => (IntPtr)i.PIDL.LastId), typeof(TInterface).GUID) as TInterface);
 }
예제 #11
0
        private bool GetThumbnail(string file, IntPtr pidl, IShellFolder item, int width, int height)
        {
            IntPtr        hBmp         = IntPtr.Zero;
            IExtractImage extractImage = null;

            try
            {
                string pidlPath = PathFromPidl(pidl);
                if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                {
                    IUnknown iunk            = null;
                    int      prgf            = 0;
                    Guid     iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                    item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk);
                    extractImage = (IExtractImage)iunk;

                    if (extractImage != null)
                    {
                        SIZE sz = new SIZE();
                        sz.cx = width;
                        sz.cy = height;
                        StringBuilder location             = new StringBuilder(260, 260);
                        int           priority             = 0;
                        int           requestedColourDepth = 32;
                        EIEIFLAG      flags  = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                        int           uFlags = (int)flags;

                        extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);

                        extractImage.Extract(out hBmp);
                        if (hBmp != IntPtr.Zero)
                        {
                            thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp);
                        }

                        Marshal.ReleaseComObject(extractImage);
                        extractImage = null;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (hBmp != IntPtr.Zero)
                {
                    UnmanagedMethods.DeleteObject(hBmp);
                }
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                }
                throw ex;
            }
        }
예제 #12
0
        private bool _getThumbNail(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr        hBmp         = IntPtr.Zero;
            IExtractImage extractImage = null;

            IntPtr[] pidl_array = new IntPtr[] { pidl };
            try
            {
                string pidlPath = PathFromPidl(pidl);
                if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                {
                    object iunk            = null;
                    Guid   iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                    item.GetUIObjectOf(IntPtr.Zero, 1, pidl_array, ref iidExtractImage, IntPtr.Zero, out iunk);
                    extractImage = (IExtractImage)iunk;
                    if (extractImage != null)
                    {
                        Console.WriteLine("Got an IExtractImage object!");
                        SIZE sz = new SIZE();
                        sz.cx = DesiredSize.Width;
                        sz.cy = DesiredSize.Height;
                        StringBuilder location             = new StringBuilder(260, 260);
                        int           priority             = 0;
                        int           requestedColourDepth = 32;
                        EIEIFLAG      flags  = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                        int           uFlags = (int)flags;
                        extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);
                        extractImage.Extract(ref hBmp);
                        if (hBmp != IntPtr.Zero)
                        {
                            _thumbNail = Bitmap.FromHbitmap(hBmp);
                        }
                        Marshal.ReleaseComObject(extractImage);
                        extractImage = null;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (hBmp != IntPtr.Zero)
                {
                    UnmanagedMethods.DeleteObject(hBmp);
                }
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                }



                throw ex;
            }
        }
예제 #13
0
        public bool InitFolder(IShellFolder folder, IntPtr itemPidl)
        {
            var relativePidl = ILShell32.ILFindLastID(itemPidl);

            if (comInterface != null && messageWindow != null)
            {
                return(true);
            }

            IntPtr result = IntPtr.Zero;

            IntPtr[] apidls = new IntPtr[] { itemPidl };

            var refGuid = typeof(IContextMenu).GUID;

            try
            {
                //PUIDLIST_RELATIVE ILFindChild(
                //  _In_ PCIDLIST_ABSOLUTE pidlParent,
                //  _In_ PCIDLIST_ABSOLUTE pidlChild
                //);

                PidlHandle handle   = new PidlHandle(relativePidl);
                var        itemPath = handle.Path;

                var relPidls = new IntPtr[] { relativePidl };

                folder.GetUIObjectOf(IntPtr.Zero,
                                     (uint)relPidls.Length,
                                     relPidls, // [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
                                     ref refGuid, 0, out result);

                if (result != IntPtr.Zero)
                {
                    comInterface = (IContextMenu)
                                   Marshal.GetTypedObjectForIUnknown(result,
                                                                     typeof(IContextMenu));

                    comInterface2 = comInterface as IContextMenu2;
                    comInterface3 = comInterface as IContextMenu3;
                    messageWindow = new MessageWindow(this);
                }
                else
                {
                    var handle2 = new PidlHandle(itemPidl);
                }
            }
            catch (Exception ex)
            {
                // Value does not fall within the expected range.
                this.LastError = ex;
            }

            return(result != IntPtr.Zero);
        }
예제 #14
0
        private static IContextMenu CreateContextMenu(IShellItem shellItem, IShellFolder shellFolder)
        {
            var ids = new IntPtr[1] {
                ComHelpers.GetID(shellItem)
            };

            IntPtr res;

            shellFolder.GetUIObjectOf(IntPtr.Zero,
                                      1, ids,
                                      typeof(IContextMenu).GUID, 0, out res);
            return((IContextMenu)Marshal.GetTypedObjectForIUnknown(res, typeof(IContextMenu)));
        }
예제 #15
0
        public IExtractIconPWFlags GetIconType()
        {
            if (this.Extension == ".exe" || this.Extension == ".com" || this.Extension == ".bat" || this.Extension == ".msi")
            {
                return(IExtractIconPWFlags.GIL_PERINSTANCE);
            }

            if (this.Parent == null)
            {
                return(0);
            }

            if (this.IsFolder)
            {
                IExtractIcon  iextract     = null;
                IShellFolder  ishellfolder = null;
                StringBuilder str          = null;
                IntPtr        result;

                try {
                    var  guid = new Guid("000214fa-0000-0000-c000-000000000046");
                    uint res  = 0;
                    ishellfolder = this.Parent.GetIShellFolder();
                    var pidls = new IntPtr[1] {
                        Shell32.ILFindLastID(this.PIDL)
                    };

                    ishellfolder.GetUIObjectOf(IntPtr.Zero, 1, pidls, ref guid, res, out result);

                    if (result == IntPtr.Zero)
                    {
                        pidls = null;
                        return(IExtractIconPWFlags.GIL_PERCLASS);
                    }
                    iextract = (IExtractIcon)Marshal.GetTypedObjectForIUnknown(result, typeof(IExtractIcon));
                    str      = new StringBuilder(512);
                    var index = -1;
                    IExtractIconPWFlags flags;
                    iextract.GetIconLocation(IExtractIconUFlags.GIL_ASYNC, str, 512, out index, out flags);
                    return(flags);
                } catch (Exception) {
                    return(0);
                }
            }
            else
            {
                return(IExtractIconPWFlags.GIL_PERCLASS);
            }
        }
예제 #16
0
        private static bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr PIDLs, out IntPtr ctxMenuPtr)
        {
            var arrPidLs = new IntPtr[1];
            arrPidLs[0] = PIDLs;
            var nResult = oParentFolder.GetUIObjectOf(IntPtr.Zero, (uint) arrPidLs.Length, arrPidLs, ref _iidIContextMenu, IntPtr.Zero, out ctxMenuPtr);
            if (SOk == nResult)
            {
                _oContextMenu = (IContextMenu) Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof (IContextMenu));

                return true;
            }
            ctxMenuPtr = IntPtr.Zero;
            _oContextMenu = null;
            return false;
        }
예제 #17
0
    //==========================================================
    //renvoie une instance de l'interface d'extraction d'icône pour item
    //==========================================================
    //IN item : instance de IShellFolder depuis laquelle on extrait l'extracteur d'icône
    //IN pidl : pointeur vers un ITEMLIST contenant le nom de fichier dont on veut l'icône
    //renvoie une instance de IExtractIcon
    //==========================================================
    private IExtractIcon getIcon(IShellFolder item, IntPtr pidl)
    {
        int  prgf             = 0;
        Guid uuidIExtractIcon = new Guid("000214FA-0000-0000-C000-000000000046");

        try {
            if (pidl != IntPtr.Zero)
            {
                return((IExtractIcon)item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref uuidIExtractIcon, out prgf));
            }
            else
            {
                return((IExtractIcon)item.CreateViewObject(IntPtr.Zero, ref uuidIExtractIcon));
            }
        } catch { return(null); }
    }
예제 #18
0
        public static DragDropEffects DoDragDrop(string path, Control control)
        {
            DragDropEffects none;

            if ((path == null) || (path.Length == 0))
            {
                return(DragDropEffects.None);
            }
            IShellFolder ppv  = null;
            object       obj2 = null;
            IntPtr       zero = IntPtr.Zero;

            try {
                IntPtr ptr2;
                zero = PInvoke.ILCreateFromPath(path);
                if (((zero == IntPtr.Zero) || (PInvoke.SHBindToParent(zero, ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr2) != 0)) || (ppv == null))
                {
                    return(DragDropEffects.None);
                }
                IntPtr[] apidl       = new IntPtr[] { ptr2 };
                uint     rgfReserved = 0;
                Guid     riid        = ExplorerGUIDs.IID_IDataObject;
                ppv.GetUIObjectOf(IntPtr.Zero, (uint)apidl.Length, apidl, ref riid, ref rgfReserved, out obj2);
                if (obj2 != null)
                {
                    return(control.DoDragDrop(obj2, DragDropEffects.Link | DragDropEffects.Move | DragDropEffects.Copy));
                }
                none = DragDropEffects.None;
            }
            finally {
                if (obj2 != null)
                {
                    Marshal.ReleaseComObject(obj2);
                    obj2 = null;
                }
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                    ppv = null;
                }
                if (zero != IntPtr.Zero)
                {
                    PInvoke.CoTaskMemFree(zero);
                }
            }
            return(none);
        }
예제 #19
0
        private static bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr PIDLs, out IntPtr ctxMenuPtr)
        {
            var arrPidLs = new IntPtr[1];

            arrPidLs[0] = PIDLs;
            var nResult = oParentFolder.GetUIObjectOf(IntPtr.Zero, (uint)arrPidLs.Length, arrPidLs, ref _iidIContextMenu, IntPtr.Zero, out ctxMenuPtr);

            if (SOk == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof(IContextMenu));

                return(true);
            }
            ctxMenuPtr    = IntPtr.Zero;
            _oContextMenu = null;
            return(false);
        }
예제 #20
0
        private static Icon ExtractIcon(IShellFolder parentFolder, IntPtr pidl)
        {
            Guid   extractIconGuid = typeof(IExtractIcon).GUID;
            IntPtr extractIconPtr;

            if (
                parentFolder.GetUIObjectOf(IntPtr.Zero, 1, new[] { pidl }, ref extractIconGuid, IntPtr.Zero,
                                           out extractIconPtr) != SOk)
            {
                return(null);
            }

            var iconExtractor = (IExtractIcon)Marshal.GetTypedObjectForIUnknown(extractIconPtr, typeof(IExtractIcon));

            try
            {
                var location = new StringBuilder(MaxPath, MaxPath);
                int iconIndex;
                var flags = ExtractIconuFlags.GIL_FORSHELL;
                ExtractIconpwFlags pwFlags;

                if (iconExtractor.GetIconLocation(flags, location, location.Capacity, out iconIndex, out pwFlags) != SOk)
                {
                    return(null);
                }

                string path = location.ToString();
                IntPtr largeIconHandle;
                IntPtr smallIconHandle;
                uint   iconSize = 32 + (16 << 16);

                if (iconExtractor.Extract(path, (uint)iconIndex, out largeIconHandle, out smallIconHandle, iconSize) != SOk)
                {
                    return(null);
                }

                smallIconHandle.DestroyIconPointer();
                return(largeIconHandle.ExtractIconAndDestroyIconPointer());
            }
            finally
            {
                Marshal.Release(extractIconPtr);
                Marshal.ReleaseComObject(iconExtractor);
            }
        }
예제 #21
0
    //==========================================================
    //renvoie une instance de l'interface d'extraction de miniature pour item
    //==========================================================
    //IN item : instance de IShellFolder depuis laquelle on extrait l'extracteur de miniature
    //IN pidl : pointeur vers un ITEMLIST contenant le nom de fichier dont on veut la miniature
    //renvoie une instance de IExtractImage
    //==========================================================
    private IExtractImage getThumbnail(IShellFolder item, IntPtr pidl)
    {
        int  prgf = 0;
        Guid uuidIExtractImage = new Guid("BB2E617C-0920-11D1-9A0B-00C04FC2D6C1");

        try {
            // If we have a file's pidl
            if (pidl != IntPtr.Zero)
            {
                // Extract the file's thumbnail
                return((IExtractImage)item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref uuidIExtractImage, out prgf));
            }
            else
            {
                // Or use the parent folder
                return((IExtractImage)item.CreateViewObject(IntPtr.Zero, ref uuidIExtractImage));
            }
        } catch { return(null); }
    }
예제 #22
0
        private void GenerateIExtractImage(string path)
        {
            try {
                // we get the desktop shell then the PIDL for the file's folder.
                // Once we have the PIDL then get a reference to the folder (BindToObject) then we can get the PIDL for the file.
                //Now get the IExtractImage interface (GETUIObjectOf) from which we can cast to the IExtractImage object
                SHGetDesktopFolder(out _desktopFolder);

                int pdwAttributes = 0; // not required
                int pchEaten      = 0; // not required
                _hResult = _desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetDirectoryName(path), out pchEaten, out _folderPidl, out pdwAttributes);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                _hResult = _desktopFolder.BindToObject(_folderPidl, IntPtr.Zero, ref _iidShellFolder, ref _folder);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                _hResult = _folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(path), out pchEaten, out _filePidl, out pdwAttributes);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                IUnknown unk = null;
                _hResult = _folder.GetUIObjectOf(IntPtr.Zero, 1, ref _filePidl, ref _iidExtractImage, out _reserved, ref unk);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                // Now cast the unknown as the extractImage object
                _extractImage = (IExtractImage)unk;
            } catch (FileNotFoundException) {
            } catch (COMException) {
            } catch (Exception) {
            }
        }
예제 #23
0
        /// <summary>Gets the interfaces to the context menu</summary>
        /// <param name="oParentFolder">Parent folder</param>
        /// <param name="arrPIDLs">PIDLs</param>
        /// <returns>true if it got the interfaces, otherwise false</returns>
        private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs, out IntPtr ctxMenuPtr)
        {
            var nResult = oParentFolder.GetUIObjectOf(
                IntPtr.Zero,
                (uint)arrPIDLs.Length,
                arrPIDLs,
                ref IID_IContextMenu,
                IntPtr.Zero,
                out ctxMenuPtr);

            if (S_OK == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof(IContextMenu));

                return(true);
            }
            ctxMenuPtr    = IntPtr.Zero;
            _oContextMenu = null;
            return(false);
        }
예제 #24
0
 public static string GetShellInfoTipText(IntPtr pIDL, bool fAllowSlow)
 {
     if (pIDL != IntPtr.Zero)
     {
         IShellFolder ppv = null;
         IQueryInfo   o   = null;
         try {
             IntPtr ptr;
             if (PInvoke.SHBindToParent(pIDL, ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr) == 0)
             {
                 Guid     riid        = ExplorerGUIDs.IID_IQueryInfo;
                 IntPtr[] apidl       = new IntPtr[] { ptr };
                 uint     rgfReserved = 0;
                 object   obj2;
                 if (ppv.GetUIObjectOf(IntPtr.Zero, 1, apidl, ref riid, ref rgfReserved, out obj2) == 0)
                 {
                     string str;
                     o = obj2 as IQueryInfo;
                     if ((o != null) && (o.GetInfoTip(fAllowSlow ? 8 : 0, out str) == 0))
                     {
                         return(str);
                     }
                 }
             }
         }
         catch (Exception exception) {
             QTUtility2.MakeErrorLog(exception, null);
         }
         finally {
             if (ppv != null)
             {
                 Marshal.ReleaseComObject(ppv);
             }
             if (o != null)
             {
                 Marshal.ReleaseComObject(o);
             }
         }
     }
     return(null);
 }
예제 #25
0
        private IUnknown GetUIObject(IShellFolder shellFolder, string filename, Guid uuid)
        {
            IUnknown iunk = null;

            if (System.IO.File.Exists(filename))
            {
                IntPtr pidl      = IntPtr.Zero;
                int    cParsed   = 0;
                int    pdwAttrib = 0;
                shellFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(filename), out cParsed, out pidl, out pdwAttrib);
                if (pidl != IntPtr.Zero)
                {
                    int prgf = 0;
                    shellFolder.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref uuid, out prgf, ref iunk);
                }
            }
            else
            {
                shellFolder.CreateViewObject(IntPtr.Zero, ref uuid, ref iunk);
            }
            return(iunk);
        }
        /// <summary>Gets the interfaces to the context menu</summary>
        /// <param name="oParentFolder">Parent folder</param>
        /// <param name="arrPIDLs">PIDLs</param>
        /// <returns>true if it got the interfaces, otherwise false</returns>
        private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs, out IntPtr ctxMenuPtr)
        {
            int nResult = oParentFolder.GetUIObjectOf(
                IntPtr.Zero,
                (uint)arrPIDLs.Length,
                arrPIDLs,
                ref IID_IContextMenu,
                IntPtr.Zero,
                out ctxMenuPtr);

            if (S_OK == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof(IContextMenu));

                return true;
            }
            else
            {
                ctxMenuPtr = IntPtr.Zero;
                _oContextMenu = null;
                return false;
            }
        }
예제 #27
0
        public int DragDrop(System.Runtime.InteropServices.ComTypes.IDataObject pDataObj, int grfKeyState, BandObjectLib.POINT pt, ref DragDropEffects pdwEffect)
        {
            try {
                if (this.DragFileOver != null)
                {
                    DragEventArgs e = new DragEventArgs(null, grfKeyState, pt.x, pt.y, DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll, pdwEffect);
                    this.DragFileOver(null, e);
                    pdwEffect = e.Effect;
                }
                else
                {
                    pdwEffect = DragDropEffects.Copy;
                }
                if (pdwEffect != DragDropEffects.None)
                {
                    if (this.DragFileDrop != null)
                    {
                        IntPtr ptr;
                        byte[] buffer;
                        switch (this.DragFileDrop(out ptr, out buffer))
                        {
                        case -1:
                            return(0);

                        case 0: {
                            IShellFolder ppv   = null;
                            object       obj2  = null;
                            Guid         riid  = ExplorerGUIDs.IID_IShellFolder;
                            Guid         guid2 = ExplorerGUIDs.IID_IDropTarget;
                            using (IDLWrapper wrapper = new IDLWrapper(buffer)) {
                                if (wrapper.Available && wrapper.IsDropTarget)
                                {
                                    try {
                                        IntPtr ptr2;
                                        if (PInvoke.SHBindToParent(wrapper.PIDL, riid, out ppv, out ptr2) == 0)
                                        {
                                            uint     rgfReserved = 0;
                                            IntPtr[] apidl       = new IntPtr[] { ptr2 };
                                            if (ppv.GetUIObjectOf(ptr, 1, apidl, ref guid2, ref rgfReserved, out obj2) == 0)
                                            {
                                                _IDropTarget target = obj2 as _IDropTarget;
                                                if (target != null)
                                                {
                                                    DragDropEffects effects = pdwEffect;
                                                    if (target.DragEnter(pDataObj, this.iLastKeyState, pt, ref effects) == 0)
                                                    {
                                                        effects = pdwEffect;
                                                        if (target.DragOver(this.iLastKeyState, pt, ref effects) == 0)
                                                        {
                                                            if ((this.iLastKeyState & 2) != 0)
                                                            {
                                                                pdwEffect = DragDropEffects.Link | DragDropEffects.Move | DragDropEffects.Copy;
                                                            }
                                                            return(target.DragDrop(pDataObj, this.iLastKeyState, pt, ref pdwEffect));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch {
                                    }
                                    finally {
                                        if (ppv != null)
                                        {
                                            Marshal.ReleaseComObject(ppv);
                                        }
                                        if (obj2 != null)
                                        {
                                            Marshal.ReleaseComObject(obj2);
                                        }
                                        if (this.DragDropEnd != null)
                                        {
                                            this.DragDropEnd(this, EventArgs.Empty);
                                        }
                                    }
                                }
                            }
                            return(0);
                        }
                        }
                    }
                    FORMATETC format = new FORMATETC();
                    format.cfFormat = 15;
                    format.ptd      = IntPtr.Zero;
                    format.dwAspect = DVASPECT.DVASPECT_CONTENT;
                    format.lindex   = -1;
                    format.tymed    = TYMED.TYMED_HGLOBAL;
                    STGMEDIUM medium = new STGMEDIUM();
                    try {
                        pDataObj.GetData(ref format, out medium);
                        PInvoke.SendMessage(this.hwnd, 0x233, medium.unionmember, IntPtr.Zero);
                    }
                    catch {
                    }
                    finally {
                        PInvoke.ReleaseStgMedium(ref medium);
                    }
                }
            }
            finally {
                if (pDataObj != null)
                {
                    Marshal.FinalReleaseComObject(pDataObj);
                }
            }
            return(0);
        }
 private static IContextMenu GetContextMenu(IWin32Window owner, IShellFolder parent, string[] fileNames)
 {
     IContextMenu menu;
     IntPtr[] apidl = new IntPtr[fileNames.Length];
     try
     {
         for (int i = 0; i < fileNames.Length; i++)
         {
             apidl[i] = parent.ParseDisplayName(owner.Handle, fileNames[i]);
         }
         menu = parent.GetUIObjectOf<IContextMenu>(owner.Handle, apidl);
     }
     finally
     {
         foreach (IntPtr ptr in apidl)
         {
             if (ptr != IntPtr.Zero)
             {
                 Marshal.FreeCoTaskMem(ptr);
             }
         }
     }
     return menu;
 }
예제 #29
0
파일: Helper.cs 프로젝트: turky-9/yu
        public static void ShowContextMenu(IntPtr handle, IShellFolder desktop, string parent, List <FileSystemInfo> lst, double x, double y, ref IContextMenu2 newContextMenu2, ref IntPtr newSubmenuPtr)
        {
            if (lst == null)
            {
                ShowContextMenuFolder(handle, desktop, parent, lst, x, y, ref newContextMenu2, ref newSubmenuPtr);
                return;
            }

            IntPtr        PPopup = IntPtr.Zero, PIDLParent = IntPtr.Zero, PSHParent = IntPtr.Zero;
            IntPtr        PContext = IntPtr.Zero, PContext2 = IntPtr.Zero, PContext3 = IntPtr.Zero;
            List <IntPtr> ChildrenList = null;
            IContextMenu  CContext     = null;
            IContextMenu2 CContext2    = null;
            IContextMenu3 CContext3    = null;
            IShellFolder  SHParent     = null;

            try
            {
                //親フォルダの PIDL を取得する
                uint  fcharcnt = 0;
                SFGAO fattr    = SFGAO.BROWSABLE;
                if (desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, parent, ref fcharcnt, out PIDLParent, ref fattr) == Shell32Wrapper.S_OK)
                {
                    //親フォルダのシェルフォルダのポインタを取得する

                    if (desktop.BindToObject(PIDLParent, IntPtr.Zero, GUIDs.IID_IShellFolder, out PSHParent) == Shell32Wrapper.S_OK)
                    {
                        //親フォルダのIShellFolder を取得する
                        SHParent = (IShellFolder)Marshal.GetTypedObjectForIUnknown(PSHParent, typeof(IShellFolder));

                        ChildrenList = new List <IntPtr>();
                        //対象ファイルの PIDL (親のシェルフォルダからの相対 PIDL)を取得する
                        foreach (var files in lst)
                        {
                            IntPtr PFile;
                            if (SHParent.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, files.Name, ref fcharcnt, out PFile, ref fattr) == Shell32Wrapper.S_OK)
                            {
                                ChildrenList.Add(PFile);
                            }
                        }

                        //対象ファイルの IContextMenu へのポインタを取得する
                        IntPtr[] children = ChildrenList.ToArray();
                        if (SHParent.GetUIObjectOf(IntPtr.Zero, (uint)children.Length, children, GUIDs.IID_IContextMenu, IntPtr.Zero, out PContext) == Shell32Wrapper.S_OK)
                        {
                            //対象ファイルの IContextMenu を取得する
                            CContext = (IContextMenu)Marshal.GetTypedObjectForIUnknown(PContext, typeof(IContextMenu));

                            //対象ファイルの IContextMenu2, IContextMenu3 のポインタを取得する
                            Marshal.QueryInterface(PContext, ref GUIDs.IID_IContextMenu2, out PContext2);
                            Marshal.QueryInterface(PContext, ref GUIDs.IID_IContextMenu3, out PContext3);

                            CContext2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(PContext2, typeof(IContextMenu2));
                            CContext3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(PContext3, typeof(IContextMenu3));

                            //ポップアップメニューを作成する
                            PPopup = User32Wrapper.CreatePopupMenu();

                            //ポップアップメニューに、コンテキストメニュー IContextMenu を追加する
                            CMF ContextMenuFlag = CMF.EXPLORE | CMF.CANRENAME;
                            CContext.QueryContextMenu(PPopup, 0, Shell32Wrapper.CMD_FIRST, Shell32Wrapper.CMD_LAST, ContextMenuFlag);

                            //ポップアップメニューを表示する
                            //呼び出しをブロックします
                            uint selected = User32Wrapper.TrackPopupMenuEx(PPopup, TPM.RETURNCMD, (int)x, (int)y, handle, IntPtr.Zero);
                            if (selected >= Shell32Wrapper.CMD_FIRST)
                            {
                                uint cmdidx = selected - Shell32Wrapper.CMD_FIRST;
                                Helper.InvokeCommand(CContext, cmdidx, parent, new Point(x, y));
                            }
                        }
                    }
                }
            }
            #region finally
            finally
            {
                if (PPopup != null)
                {
                    User32Wrapper.DestroyMenu(PPopup);
                }

                if (CContext3 != null)
                {
                    Marshal.FinalReleaseComObject(CContext3);
                    CContext3 = null;
                }

                if (CContext2 != null)
                {
                    Marshal.FinalReleaseComObject(CContext2);
                    CContext2 = null;
                }

                if (CContext != null)
                {
                    Marshal.FinalReleaseComObject(CContext);
                    CContext = null;
                }

                if (ChildrenList != null)
                {
                    foreach (var child in ChildrenList)
                    {
                        Marshal.FreeCoTaskMem(child);
                    }
                    ChildrenList = null;
                }

                if (SHParent != null)
                {
                    Marshal.FinalReleaseComObject(SHParent);
                    SHParent = null;
                }

                if (PIDLParent != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(PIDLParent);
                }

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

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

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

                if (PContext3 != IntPtr.Zero)
                {
                    Marshal.Release(PContext3);
                }
            }
            #endregion
        }
예제 #30
0
            private bool GetThumbnail(string file, IntPtr pidl, IShellFolder item, int width, int height)
            {
                IntPtr hBmp = IntPtr.Zero;
                IExtractImage extractImage = null;

                try
                {
                    string pidlPath = PathFromPidl(pidl);
                    if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                    {
                        IUnknown iunk = null;
                        int prgf = 0;
                        Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                        item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk);
                        extractImage = (IExtractImage)iunk;

                        if (extractImage != null)
                        {
                            SIZE sz = new SIZE();
                            sz.cx = width;
                            sz.cy = height;
                            StringBuilder location = new StringBuilder(260, 260);
                            int priority = 0;
                            int requestedColourDepth = 32;
                            EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                            int uFlags = (int)flags;

                            extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);

                            extractImage.Extract(out hBmp);
                            if (hBmp != IntPtr.Zero)
                            {
                                thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp);
                            }

                            Marshal.ReleaseComObject(extractImage);
                            extractImage = null;
                        }
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    if (hBmp != IntPtr.Zero)
                    {
                        UnmanagedMethods.DeleteObject(hBmp);
                    }
                    if (extractImage != null)
                    {
                        Marshal.ReleaseComObject(extractImage);
                    }
                    throw ex;
                }
            }
예제 #31
0
        private bool GetThumbnailHelper(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr bitmapPointer = IntPtr.Zero;

            IExtractImage extractImage = null;

            try
            {
                string pidlPath = this.PathFromPidl(pidl);

                if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                {
                    IUnknown iunk = null;
                    int prgf = 0;
                    Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                    item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, ref prgf, ref iunk);
                    extractImage = (IExtractImage)iunk;
                    if (extractImage != null)
                    {
                        Console.WriteLine("Got an IExtractImage object - " + file);
                        SIZE sz = new SIZE();
                        sz.HorizontalSize = this.DesiredSize.Width;
                        sz.VerticalSize = this.DesiredSize.Height;
                        StringBuilder location = new StringBuilder(260, 260);
                        int priority = 0;
                        int requestedColourDepth = 32;
                        EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                        int nameFlags = (int)flags;
                        extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref nameFlags);
                        extractImage.Extract(ref bitmapPointer);

                        if (bitmapPointer != IntPtr.Zero)
                        {
                            this.thumbNail = Bitmap.FromHbitmap(bitmapPointer);
                        }

                        Marshal.ReleaseComObject(extractImage);

                        extractImage = null;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                if (bitmapPointer != IntPtr.Zero)
                {
                    UnmanagedMethods.DeleteObject(bitmapPointer);
                }

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

                throw ex;
            }
        }
        private bool GetThumbnail(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr bmp = IntPtr.Zero;
            IExtractImage extractImage = null;

            try
            {
                string pidlPath = PathFromPidl(pidl);
                if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                {
                    // we have the item:
                    IUnknown iunk = null;
                    int prgf;
                    Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                    item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk);
                    extractImage = (IExtractImage)iunk;

                    if (extractImage != null)
                    {
                        //Got an IExtractImage object!
                        ShellAPI.SIZE size = new ShellAPI.SIZE { cx = this.DesiredSize.Width, cy = this.DesiredSize.Height };
                        StringBuilder location = new StringBuilder(260, 260);
                        int priority = 0;
                        const int requestedColourDepth = 32;
                        const IEIFLAG flags = IEIFLAG.IEIFLAG_ASPECT | IEIFLAG.IEIFLAG_SCREEN;
                        int uFlags = (int)flags;

                        extractImage.GetLocation(location, location.Capacity, ref priority, ref size, requestedColourDepth, ref uFlags);

                        extractImage.Extract(out bmp);
                        if (bmp != IntPtr.Zero)
                        {
                            // create the image object:
                            this.ThumbNail = Image.FromHbitmap(bmp);
                            // is thumbNail owned by the Bitmap?
                        }

                        Marshal.ReleaseComObject(extractImage);
                        extractImage = null;
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                if (bmp != IntPtr.Zero)
                {
                    UnManagedMethods.DeleteObject(bmp);
                }
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                }

                throw;
            }
        }
예제 #33
0
        public static int PopUpSystemContextMenu(IDLWrapper idlw, Point pntShow, ref IContextMenu2 pIContextMenu2, IntPtr hwndParent, bool fCanRemove)
        {
            IShellFolder ppv = null;
            int          num5;

            try {
                IntPtr ptr;
                if ((!idlw.Available || (PInvoke.SHBindToParent(idlw.PIDL, ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr) != 0)) || (ppv == null))
                {
                    return(-1);
                }
                IntPtr[] apidl       = new IntPtr[] { ptr };
                uint     rgfReserved = 0;
                Guid     riid        = ExplorerGUIDs.IID_IContextMenu;
                object   obj2;
                ppv.GetUIObjectOf(IntPtr.Zero, (uint)apidl.Length, apidl, ref riid, ref rgfReserved, out obj2);
                if (pIContextMenu2 != null)
                {
                    Marshal.ReleaseComObject(pIContextMenu2);
                    pIContextMenu2 = null;
                }
                pIContextMenu2 = obj2 as IContextMenu2;
                if (pIContextMenu2 == null)
                {
                    return(-2);
                }
                ContextMenu menu   = new ContextMenu();
                uint        uFlags = 0;
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    uFlags |= 0x100;
                }
                pIContextMenu2.QueryContextMenu(menu.Handle, 0, 1, 0xffff, uFlags);
                if (fCanRemove)
                {
                    PInvoke.AppendMenu(menu.Handle, 0x800, IntPtr.Zero, null);
                    PInvoke.AppendMenu(menu.Handle, 0, new IntPtr(0xffff), QTUtility.ResMain[0x19]);
                }
                if ((idlw.HasPath && (idlw.Path.Length > 3)) && (idlw.IsFileSystemFolder || idlw.IsFileSystemFile))
                {
                    if (!fCanRemove)
                    {
                        PInvoke.AppendMenu(menu.Handle, 0x800, IntPtr.Zero, null);
                    }
                    PInvoke.AppendMenu(menu.Handle, 0, new IntPtr(0xfffe), QTUtility.ResMain[0x1a]);
                }
                uint num3 = PInvoke.TrackPopupMenu(menu.Handle, 0x100, pntShow.X, pntShow.Y, 0, hwndParent, IntPtr.Zero);
                int  num4 = -3;
                switch (num3)
                {
                case 0:
                    num4 = 0xfffd;
                    break;

                case 0xffff:
                    menu.Dispose();
                    return(0xffff);

                case 0xfffe:
                    if (idlw.HasPath)
                    {
                        try {
                            string directoryName = Path.GetDirectoryName(idlw.Path);
                            if (Directory.Exists(directoryName))
                            {
                                IntPtr currentHandle = QTUtility.instanceManager.CurrentHandle;
                                if (PInvoke.IsWindow(currentHandle))
                                {
                                    QTUtility2.SendCOPYDATASTRUCT(currentHandle, (IntPtr)0x10, directoryName, IntPtr.Zero);
                                    num4 = 0xfffe;
                                }
                                else
                                {
                                    Process.Start(directoryName);
                                    num4 = -4;
                                }
                            }
                        }
                        catch {
                            SystemSounds.Asterisk.Play();
                        }
                    }
                    break;

                default: {
                    CMINVOKECOMMANDINFO structure = new CMINVOKECOMMANDINFO();
                    structure.cbSize       = Marshal.SizeOf(structure);
                    structure.fMask        = 0;
                    structure.hwnd         = hwndParent;
                    structure.lpVerb       = (IntPtr)((num3 - 1) & 0xffff);
                    structure.lpParameters = IntPtr.Zero;
                    structure.lpDirectory  = IntPtr.Zero;
                    structure.nShow        = 1;
                    structure.dwHotKey     = 0;
                    structure.hIcon        = IntPtr.Zero;
                    num4 = pIContextMenu2.InvokeCommand(ref structure);
                    break;
                }
                }
                menu.Dispose();
                num5 = num4;
            }
            catch {
                num5 = -1;
            }
            finally {
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                }
            }
            return(num5);
        }
        private bool getThumbnail(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr        hBmp         = IntPtr.Zero;
            IExtractImage extractImage = null;

            try
            {
                string pidlPath = PathFromPidl(pidl);
                if (Path.GetFileName(pidlPath).ToUpperInvariant().Equals(Path.GetFileName(file).ToUpperInvariant()))
                {
                    // we have the item:
                    IUnknown iunk            = null;
                    int      prgf            = 0;
                    Guid     iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                    item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk);
                    extractImage = (IExtractImage)iunk;

                    if (extractImage != null)
                    {
                        //Got an IExtractImage object!
                        SIZE sz = new SIZE();
                        sz.cx = desiredSize.Width;
                        sz.cy = desiredSize.Height;
                        StringBuilder location             = new StringBuilder(260, 260);
                        int           priority             = 0;
                        int           requestedColourDepth = 32;
                        //EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                        EIEIFLAG flags  = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_QUALITY;
                        int      uFlags = (int)flags;

                        // E.g. for PDFs on Vista...
                        try
                        {
                            extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth,
                                                     ref uFlags);
                        }
                        catch (Exception) {}

                        extractImage.Extract(out hBmp);
                        if (hBmp != IntPtr.Zero)
                        {
                            // create the image object:
                            thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp);
                            // is thumbNail owned by the Bitmap?
                        }

                        Marshal.ReleaseComObject(extractImage);
                        extractImage = null;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (hBmp != IntPtr.Zero)
                {
                    UnManagedMethods.DeleteObject(hBmp);
                }
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                }
                throw ex;
            }
        }
예제 #35
0
        /// <summary>
        /// This method will use the GetUIObjectOf method of IShellFolder to obtain the IDropTarget of a
        /// ShellItem. 
        /// </summary>
        /// <param name="item">The item for which to obtain the IDropTarget</param>
        /// <param name="dropTargetPtr">A pointer to the returned IDropTarget</param>
        /// <returns>the IDropTarget from the ShellItem</returns>
        public static bool GetIDropTarget(IntPtr[] pidls, IShellFolder parent, out IntPtr dropTargetPtr, out ShellDll.IDropTarget dropTarget)
        {
            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    1,
                    pidls,
                    ref ShellAPI.IID_IDropTarget,
                    IntPtr.Zero,
                    out dropTargetPtr) == ShellAPI.S_OK)
            {
                dropTarget =
                    (ShellDll.IDropTarget)Marshal.GetTypedObjectForIUnknown(dropTargetPtr, typeof(ShellDll.IDropTarget));

                return true;
            }
            else
            {
                dropTarget = null;
                dropTargetPtr = IntPtr.Zero;
                return false;
            }
        }
        /// <summary>
        /// Retrieves the IContextMenu for specific items
        /// </summary>
        /// <param name="parent">the parent IShellFolder which contains the items</param>
        /// <param name="pidls">the pidls of the items for which to retrieve the IContextMenu</param>
        /// <param name="icontextMenuPtr">the pointer to the IContextMenu</param>
        /// <param name="iContextMenu">the IContextMenu for the items</param>
        /// <returns>true if the IContextMenu has been retrieved succesfully, false otherwise</returns>
        public static bool GetIContextMenu(
            IShellFolder parent,
            IntPtr[] pidls,
            out IntPtr iContextMenuPtr,
            out IContextMenu iContextMenu)
        {
            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        (uint)pidls.Length,
                        pidls,
                        ref ShellAPI.IID_IContextMenu,
                        IntPtr.Zero,
                        out iContextMenuPtr) == ShellAPI.S_OK)
            {
                iContextMenu =
                    (IContextMenu)Marshal.GetTypedObjectForIUnknown(
                        iContextMenuPtr, typeof(IContextMenu));

                return true;
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu = null;

                return false;
            }
        }
예제 #37
0
    private bool getThumbnail(string file, IntPtr pidl, IShellFolder item)
    {
      IntPtr hBmp = IntPtr.Zero;
      IExtractImage extractImage = null;

      try
      {
        string pidlPath = PathFromPidl(pidl);
        if (Path.GetFileName(pidlPath).ToUpperInvariant().Equals(Path.GetFileName(file).ToUpperInvariant()))
        {
          // we have the item:
          IUnknown iunk = null;
          int prgf = 0;
          Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
          item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk);
          extractImage = (IExtractImage)iunk;

          if (extractImage != null)
          {
            //Got an IExtractImage object!
            SIZE sz = new SIZE();
            sz.cx = desiredSize.Width;
            sz.cy = desiredSize.Height;
            StringBuilder location = new StringBuilder(260, 260);
            int priority = 0;
            int requestedColourDepth = 32;
            //EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
            EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_QUALITY;
            int uFlags = (int)flags;

            // E.g. for PDFs on Vista...
            try
            {
              extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth,
                                       ref uFlags);
            }
            catch (Exception) {}

            extractImage.Extract(out hBmp);
            if (hBmp != IntPtr.Zero)
            {
              // create the image object:
              thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp);
              // is thumbNail owned by the Bitmap?
            }

            Marshal.ReleaseComObject(extractImage);
            extractImage = null;
          }
          return true;
        }
        else
        {
          return false;
        }
      }
      catch (Exception ex)
      {
        if (hBmp != IntPtr.Zero)
        {
          UnManagedMethods.DeleteObject(hBmp);
        }
        if (extractImage != null)
        {
          Marshal.ReleaseComObject(extractImage);
        }
        throw ex;
      }
    }
예제 #38
0
 /// <summary>
 /// This method will use the GetUIObjectOf method of IShellFolder to obtain the IDataObject of a
 /// ShellItem. 
 /// </summary>
 /// <returns>the IDataObject the ShellItem</returns>
 public static IntPtr GetIDataObject(IntPtr[] pidls, IShellFolder parent)
 {
     IntPtr dataObjectPtr;
     if (parent.GetUIObjectOf(
             IntPtr.Zero,
             (uint)pidls.Length,
             pidls,
             ref ShellAPI.IID_IDataObject,
             IntPtr.Zero,
             out dataObjectPtr) == ShellAPI.S_OK)
     {
         return dataObjectPtr;
     }
     else
     {
         return IntPtr.Zero;
     }
 }
        /// <summary>Gets the interfaces to the context menu</summary>
        /// <param name="oParentFolder">Parent folder</param>
        /// <param name="arrPIDLs">PIDLs</param>
        /// <returns>true if it got the interfaces, otherwise false</returns>
        private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs)
        {
            IntPtr pUnknownContextMenu = IntPtr.Zero;

            int nResult = oParentFolder.GetUIObjectOf(
                IntPtr.Zero,
                (uint)arrPIDLs.Length,
                arrPIDLs,
                ref IID_IContextMenu,
                IntPtr.Zero,
                out pUnknownContextMenu);

            if (S_OK == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu, typeof(IContextMenu));

                IntPtr pUnknownContextMenu2 = IntPtr.Zero;
                if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu2, out pUnknownContextMenu2))
                {
                    _oContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu2, typeof(IContextMenu2));
                }
                IntPtr pUnknownContextMenu3 = IntPtr.Zero;
                if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu3, out pUnknownContextMenu3))
                {
                    _oContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu3, typeof(IContextMenu3));
                }

                return true;
            }
            else
            {
                return false;
            }
        }
예제 #40
0
        public static DragDropEffects DoDragDrop(List <string> lstPaths, Control control, bool fSameDirecotry)
        {
            DragDropEffects none;

            if ((lstPaths == null) || (lstPaths.Count == 0))
            {
                return(DragDropEffects.None);
            }
            IShellFolder  ppv  = null;
            object        obj2 = null;
            List <IntPtr> list = new List <IntPtr>();

            try {
                IntPtr[] ptrArray;
                if (fSameDirecotry)
                {
                    IntPtr ptr2;
                    IntPtr item = PInvoke.ILCreateFromPath(lstPaths[0]);
                    list.Add(item);
                    if ((PInvoke.SHBindToParent(item, ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr2) != 0) || (ppv == null))
                    {
                        return(DragDropEffects.None);
                    }
                    List <IntPtr> list2 = new List <IntPtr>();
                    if (ptr2 != IntPtr.Zero)
                    {
                        list2.Add(ptr2);
                    }
                    for (int i = 1; i < lstPaths.Count; i++)
                    {
                        IntPtr ptr3 = PInvoke.ILCreateFromPath(lstPaths[i]);
                        if (ptr3 != IntPtr.Zero)
                        {
                            list.Add(ptr3);
                            IntPtr ptr4 = PInvoke.ILFindLastID(ptr3);
                            if (ptr4 != IntPtr.Zero)
                            {
                                list2.Add(ptr4);
                            }
                        }
                    }
                    if (list2.Count == 0)
                    {
                        return(DragDropEffects.None);
                    }
                    ptrArray = list2.ToArray();
                }
                else
                {
                    list.AddRange(lstPaths.Select(PInvoke.ILCreateFromPath).Where(ptr5 => ptr5 != IntPtr.Zero));
                    PInvoke.SHGetDesktopFolder(out ppv);
                    if ((list.Count == 0) || (ppv == null))
                    {
                        return(DragDropEffects.None);
                    }
                    ptrArray = list.ToArray();
                }
                uint rgfReserved = 0;
                Guid riid        = ExplorerGUIDs.IID_IDataObject;
                ppv.GetUIObjectOf(IntPtr.Zero, (uint)ptrArray.Length, ptrArray, ref riid, ref rgfReserved, out obj2);
                if (obj2 != null)
                {
                    return(control.DoDragDrop(obj2, DragDropEffects.Link | DragDropEffects.Move | DragDropEffects.Copy));
                }
                none = DragDropEffects.None;
            }
            finally {
                if (obj2 != null)
                {
                    Marshal.ReleaseComObject(obj2);
                }
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                }
                foreach (IntPtr ptr6 in list)
                {
                    if (ptr6 != IntPtr.Zero)
                    {
                        PInvoke.CoTaskMemFree(ptr6);
                    }
                }
            }
            return(none);
        }
예제 #41
0
        public IExtractIconPWFlags GetIconType()
        {
            if (this.IsFolder)
            {
                IExtractIcon  iextract     = null;
                IShellFolder  ishellfolder = null;
                StringBuilder str          = null;
                IntPtr        result;

                try {
                    var  guid = new Guid("000214fa-0000-0000-c000-000000000046");
                    uint res  = 0;
                    ishellfolder = this.Parent.GetIShellFolder();
                    var pidls = new IntPtr[1] {
                        Shell32.ILFindLastID(this.PIDL)
                    };

                    ishellfolder.GetUIObjectOf(IntPtr.Zero, 1, pidls, ref guid, res, out result);

                    if (result == IntPtr.Zero)
                    {
                        pidls = null;
                        return(IExtractIconPWFlags.GIL_PERCLASS);
                    }
                    iextract = (IExtractIcon)Marshal.GetTypedObjectForIUnknown(result, typeof(IExtractIcon));
                    str      = new StringBuilder(512);
                    var index = -1;
                    IExtractIconPWFlags flags;
                    iextract.GetIconLocation(0, str, 512, out index, out flags);
                    Marshal.ReleaseComObject(ishellfolder);
                    Marshal.ReleaseComObject(iextract);
                    return(flags);
                } catch (Exception) {
                    return(0);
                }
            }
            else
            {
                var value = this.GetPropertyValue(SystemProperties.PerceivedType, typeof(PerceivedType))?.Value;
                if (value != null)
                {
                    var perceivedType = (PerceivedType)value;
                    if (perceivedType == PerceivedType.Application)
                    {
                        return(IExtractIconPWFlags.GIL_PERINSTANCE);
                    }
                    else
                    {
                        return(IExtractIconPWFlags.GIL_PERCLASS);
                    }
                }
            }
            return(IExtractIconPWFlags.GIL_PERCLASS);
            //if (this.Extension.ToLowerInvariant() == ".exe" || this.Extension.ToLowerInvariant() == ".com" || this.Extension.ToLowerInvariant() == ".bat" || this.Extension.ToLowerInvariant() == ".msi" || this.Extension.ToLowerInvariant() == ".jar") {
            //  return IExtractIconPWFlags.GIL_PERINSTANCE;
            //}
            ////return IExtractIconPWFlags.GIL_PERCLASS;
            //if (this.Parent == null) {
            //  return 0;
            //}

            ////if (this.IsFolder) {
            //  IExtractIcon iextract = null;
            //  IShellFolder ishellfolder = null;
            //  StringBuilder str = null;
            //  IntPtr result;

            //  try {
            //    var guid = new Guid("000214fa-0000-0000-c000-000000000046");
            //    uint res = 0;
            //    ishellfolder = this.Parent.GetIShellFolder();
            //    var pidls = new IntPtr[1] { Shell32.ILFindLastID(this.PIDL) };

            //    ishellfolder.GetUIObjectOf(IntPtr.Zero, 1, pidls, ref guid, res, out result);

            //    if (result == IntPtr.Zero) {
            //      pidls = null;
            //      return IExtractIconPWFlags.GIL_PERCLASS;
            //    }
            //    iextract = (IExtractIcon)Marshal.GetTypedObjectForIUnknown(result, typeof(IExtractIcon));
            //    str = new StringBuilder(512);
            //    var index = -1;
            //    IExtractIconPWFlags flags;
            //    iextract.GetIconLocation(IExtractIconUFlags.GIL_ASYNC, str, 512, out index, out flags);
            //    return flags;
            //  } catch (Exception) {
            //    return 0;
            //  }
            ////} else {
            ////  return IExtractIconPWFlags.GIL_PERCLASS;
            ////}
        }
예제 #42
0
        public static int PopUpSystemContextMenu(List <string> lstPaths, Point pntShow, ref IContextMenu2 pIContextMenu2, IntPtr hwndParent)
        {
            IShellFolder  ppv  = null;
            List <IntPtr> list = new List <IntPtr>();

            try {
                IntPtr        ptr;
                List <IntPtr> list2 = new List <IntPtr>();
                foreach (IntPtr item in lstPaths.Select(PInvoke.ILCreateFromPath).Where(item => item != IntPtr.Zero))
                {
                    list.Add(item);
                    list2.Add(PInvoke.ILFindLastID(item));
                }
                if ((list.Count == 0) || (PInvoke.SHBindToParent(list[0], ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr) != 0))
                {
                    return(-1);
                }
                IntPtr[] apidl       = list2.ToArray();
                uint     rgfReserved = 0;
                Guid     riid        = ExplorerGUIDs.IID_IContextMenu;
                object   obj2;
                ppv.GetUIObjectOf(IntPtr.Zero, (uint)apidl.Length, apidl, ref riid, ref rgfReserved, out obj2);
                if (pIContextMenu2 != null)
                {
                    Marshal.ReleaseComObject(pIContextMenu2);
                    pIContextMenu2 = null;
                }
                pIContextMenu2 = obj2 as IContextMenu2;
                if (pIContextMenu2 == null)
                {
                    return(-1);
                }
                using (ContextMenu menu = new ContextMenu()) {
                    uint uFlags = 0;
                    if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                    {
                        uFlags |= 0x100;
                    }
                    pIContextMenu2.QueryContextMenu(menu.Handle, 0, 1, 0xffff, uFlags);
                    uint num3 = PInvoke.TrackPopupMenu(menu.Handle, 0x100, pntShow.X, pntShow.Y, 0, hwndParent, IntPtr.Zero);
                    if (num3 != 0)
                    {
                        CMINVOKECOMMANDINFO structure = new CMINVOKECOMMANDINFO();
                        structure.cbSize       = Marshal.SizeOf(structure);
                        structure.fMask        = 0;
                        structure.hwnd         = hwndParent;
                        structure.lpVerb       = (IntPtr)((num3 - 1) & 0xffff);
                        structure.lpParameters = IntPtr.Zero;
                        structure.lpDirectory  = IntPtr.Zero;
                        structure.nShow        = 1;
                        structure.dwHotKey     = 0;
                        structure.hIcon        = IntPtr.Zero;
                        pIContextMenu2.InvokeCommand(ref structure);
                        return(0);
                    }
                }
            }
            catch {
            }
            finally {
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                }
                foreach (IntPtr ptr3 in list)
                {
                    if (ptr3 != IntPtr.Zero)
                    {
                        PInvoke.CoTaskMemFree(ptr3);
                    }
                }
            }
            return(-1);
        }
        private bool _getThumbNail(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr hBmp = IntPtr.Zero;
            IExtractImage extractImage = null;
            IntPtr[] pidl_array = new IntPtr[] { pidl };
            try
            {
            string pidlPath = PathFromPidl(pidl);
            if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
            {
                object iunk = null;
                Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                item.GetUIObjectOf(IntPtr.Zero, 1, pidl_array, ref iidExtractImage, IntPtr.Zero, out iunk);
                extractImage = (IExtractImage)iunk;
                if (extractImage != null)
                {
                    Console.WriteLine("Got an IExtractImage object!");
                    SIZE sz = new SIZE();
                    sz.cx = DesiredSize.Width;
                    sz.cy = DesiredSize.Height;
                    StringBuilder location = new StringBuilder(260, 260);
                    int priority = 0;
                    int requestedColourDepth = 32;
                    EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                    int uFlags = (int)flags;
                    extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);
                    extractImage.Extract(ref hBmp);
                    if (hBmp != IntPtr.Zero)
                    {
                        _thumbNail = Bitmap.FromHbitmap(hBmp);
                    }
                    Marshal.ReleaseComObject(extractImage);
                    extractImage = null;
                }
                return true;
            }
            else
            {
                return false;
            }
            }
            catch (Exception ex)
            {
            if (hBmp != IntPtr.Zero)
            {
                UnmanagedMethods.DeleteObject(hBmp);
            }
            if (extractImage != null)
            {
                Marshal.ReleaseComObject(extractImage);
            }

            throw ex;
            }
        }
예제 #44
0
        private bool getThumbNail(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr hBmp = IntPtr.Zero;
            IExtractImage extractImage = null;
            try
            {
                var pidlPath = pathFromPidl(pidl);
                if (!Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                {
                    return false;
                }

                IUnknown iunk = null;
                var prgf = 0;
                var iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, ref prgf, ref iunk);
                extractImage = (IExtractImage)iunk;
                if (extractImage == null)
                {
                    return true;
                }

                var sz = new Size { cx = DesiredSize.Width, cy = DesiredSize.Height };
                var location = new StringBuilder(260, 260);
                var priority = 0;
                const int requestedColourDepth = 32;
                const EIEIFLAG flags =
                    EIEIFLAG.IeiflagScreen | EIEIFLAG.IeiflagAsync | EIEIFLAG.IeiflagQuality | EIEIFLAG.IeiflagCache;
                var uFlags = (int)flags;
                try
                {
                    extractImage.GetLocation(
                        location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);
                }
                catch { }

                extractImage.Extract(ref hBmp);

                if (hBmp != IntPtr.Zero)
                {
                    ThumbNail = Image.FromHbitmap(hBmp);
                }

                Marshal.ReleaseComObject(extractImage);
                extractImage = null;
                return true;
            }
            finally
            {
                if (hBmp != IntPtr.Zero)
                {
                    UnmanagedMethods.DeleteObject(hBmp);
                }
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                }
            }
        }