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); }
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); }