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);
        }
示例#2
0
        public int DragDrop(IDataObject pDataObj, int grfKeyState, Point pt, ref DragDropEffects pdwEffect)
        {
            try {
                if (DragFileOver != null)
                {
                    DragEventArgs e = new DragEventArgs(null, grfKeyState, pt.X, pt.Y, DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll, pdwEffect);
                    DragFileOver(null, e);
                    pdwEffect = e.Effect;
                }
                else
                {
                    pdwEffect = DragDropEffects.Copy;
                }
                if (pdwEffect != DragDropEffects.None)
                {
                    if (DragFileDrop != null)
                    {
                        IntPtr ptr;
                        byte[] buffer;
                        switch (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, iLastKeyState, pt, ref effects) == 0)
                                                    {
                                                        effects = pdwEffect;
                                                        if (target.DragOver(iLastKeyState, pt, ref effects) == 0)
                                                        {
                                                            if ((iLastKeyState & 2) != 0)
                                                            {
                                                                pdwEffect = DragDropEffects.Link | DragDropEffects.Move | DragDropEffects.Copy;
                                                            }
                                                            return(target.DragDrop(pDataObj, iLastKeyState, pt, ref pdwEffect));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch {
                                    }
                                    finally {
                                        if (ppv != null)
                                        {
                                            Marshal.ReleaseComObject(ppv);
                                        }
                                        if (obj2 != null)
                                        {
                                            Marshal.ReleaseComObject(obj2);
                                        }
                                        if (DragDropEnd != null)
                                        {
                                            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(hwnd, 0x233, medium.unionmember, IntPtr.Zero);
                    }
                    catch {
                    }
                    finally {
                        PInvoke.ReleaseStgMedium(ref medium);
                    }
                }
            }
            finally {
                if (pDataObj != null)
                {
                    Marshal.FinalReleaseComObject(pDataObj);
                }
            }
            return(0);
        }
示例#3
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        = null;
                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);
                    ppv = null;
                }
            }
            return(num5);
        }
示例#4
0
 public DropTargetWrapper(Control ctrl)
 {
     hwnd = ctrl.Handle;
     PInvoke.RegisterDragDrop(hwnd, this);
 }
示例#5
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 (string str in lstPaths)
                {
                    IntPtr item = PInvoke.ILCreateFromPath(str);
                    if (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        = null;
                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);
        }