TreeNode InsertNode(TreeNode parent, ShellItem folder, string displayName) { ShellItem parentFolder = (ShellItem)parent.Tag; IntPtr folderRelPidl = Shell32.ILFindLastID(folder.Pidl); TreeNode result = null; foreach (TreeNode child in parent.Nodes) { ShellItem childFolder = (ShellItem)child.Tag; IntPtr childRelPidl = Shell32.ILFindLastID(childFolder.Pidl); short compare = parentFolder.GetIShellFolder().CompareIDs(0, folderRelPidl, childRelPidl); if (compare < 0) { result = parent.Nodes.Insert(child.Index, displayName); break; } } if (result == null) { result = parent.Nodes.Add(displayName); } return(result); }
/// <summary> /// Selects all items in the <see cref="ShellView"/>. /// </summary> public void SelectAll() { foreach (ShellItem item in ShellItem) { m_ComInterface.SelectItem(Shell32.ILFindLastID(item.Pidl), SVSI.SVSI_SELECT); } }
/// <summary> /// This method uses the GetUIObjectOf method of IShellFolder to obtain the IDropTarget of a /// CShItem. /// </summary> /// <param name="item">The item for which to obtain the IDropTarget</param> /// <param name="dropTarget">The IDropTarget interface of the input Folder</param> /// <returns>true if successful in obtaining the IDropTarget Interface.</returns> /// <remarks>The original FileBrowser version of this returned the IntPtr which points to /// the interface. This is not needed since GetTypedObjectForIUnknown manages that IntPtr. /// For all purposes, the CShItem.GetDropTargetOf routine is more efficient and provides /// the same interface.</remarks> public static bool GetIDropTarget(ShellItem item, ref FlShell.Interop.IDropTarget dropTarget) { IntPtr dropTargetPtr = IntPtr.Zero; ShellItem parent = item.Parent; if (parent == null) { parent = item; } IShellFolder folder; if (item == ShellItem.Desktop) { folder = item.GetIShellFolder(); } else { folder = item.Parent.GetIShellFolder(); } IntPtr relpidl = Shell32.ILFindLastID(item.Pidl); if (parent.GetIShellFolder().GetUIObjectOf(IntPtr.Zero, 1, new IntPtr[] { relpidl }, Ole32.IID_IDropTarget, 0, out dropTargetPtr) == 0) { dropTarget = (FlShell.Interop.IDropTarget)Marshal.GetTypedObjectForIUnknown(dropTargetPtr, typeof(FlShell.Interop.IDropTarget)); return(true); } else { dropTarget = null; dropTargetPtr = IntPtr.Zero; return(false); } }
HResult IShellBrowser.BrowseObject(IntPtr pidl, SBSP wFlags) { IntPtr result = IntPtr.Zero; if ((wFlags & SBSP.SBSP_RELATIVE) != 0) { ShellItem child = new ShellItem(m_ShellView.CurrentFolder, pidl); } else if ((wFlags & SBSP.SBSP_PARENT) != 0) { m_ShellView.NavigateParent(); } else if ((wFlags & SBSP.SBSP_NAVIGATEBACK) != 0) { m_ShellView.NavigateBack(); } else if ((wFlags & SBSP.SBSP_NAVIGATEFORWARD) != 0) { m_ShellView.NavigateForward(); } else if ((wFlags & SBSP.SBSP_SAMEBROWSER) != 0) { ShellItem child = new ShellItem(pidl); m_ShellView.Navigate(child); //child.Dispose(); } else { m_ShellView.Navigate(new ShellItem(ShellItem.Desktop, Shell32.ILFindLastID(pidl))); } return(HResult.S_OK); }
public IntPtr GetDisplayName(SIGDN sigdnName) { if (sigdnName == SIGDN.FILESYSPATH) { StringBuilder result = new StringBuilder(512); if (!Shell32.SHGetPathFromIDList(m_Pidl, result)) { throw new ArgumentException(); } return(Marshal.StringToHGlobalUni(result.ToString())); } else { IShellFolder parentFolder = GetParent().GetIShellFolder(); IntPtr childPidl = Shell32.ILFindLastID(m_Pidl); StringBuilder builder = new StringBuilder(512); STRRET strret = new STRRET(); parentFolder.GetDisplayNameOf(childPidl, (SHGNO)((int)sigdnName & 0xffff), out strret); ShlWapi.StrRetToBuf(ref strret, childPidl, builder, (uint)builder.Capacity); return(Marshal.StringToHGlobalUni(builder.ToString())); } }
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); } }
public static IntPtr GetIDataObject(ShellItem[] items) { ShellItem parent; if (items[0].Parent != null) { parent = items[0].Parent; } else { parent = items[0]; } IntPtr[] pidls = new IntPtr[items.Length]; int i = 0; do { pidls[i] = Shell32.ILFindLastID(items[i].Pidl); i += 1; }while (i < items.Length); IntPtr dataObjectPtr = IntPtr.Zero; if (parent.GetIShellFolder().GetUIObjectOf(IntPtr.Zero, (uint)(pidls.Length), pidls, Ole32.IID_IDataObject, 0, out dataObjectPtr) == (int)HResult.S_OK) { return(dataObjectPtr); } else { return(IntPtr.Zero); } }
public HResult GetAttributes(SFGAO sfgaoMask, out SFGAO psfgaoAttribs) { IShellFolder parentFolder = GetParent().GetIShellFolder(); SFGAO result = sfgaoMask; parentFolder.GetAttributesOf(1, new IntPtr[] { Shell32.ILFindLastID(Pidl) }, ref result); psfgaoAttribs = result & sfgaoMask; return(HResult.S_OK); }
public SFGAO GetAttributes(SFGAO sfgaoMask) { var parentFolder = GetParent().GetIShellFolder(); var result = sfgaoMask; parentFolder.GetAttributesOf(1, new[] { Shell32.ILFindLastID(Pidl) }, ref result); return(result & sfgaoMask); }
public int Compare(IShellItem psi, SICHINT hint) { var other = (ShellItemImpl)psi; var myParent = GetParent(); var theirParent = other.GetParent(); if (Shell32.ILIsEqual(myParent.Pidl, theirParent.Pidl)) { return(myParent.GetIShellFolder().CompareIDs((SHCIDS)hint, Shell32.ILFindLastID(Pidl), Shell32.ILFindLastID(other.Pidl))); } return(1); }
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); } }
public int Compare(IShellItem psi, SICHINT hint) { ShellItemImpl other = (ShellItemImpl)psi; ShellItemImpl myParent = GetParent(); ShellItemImpl theirParent = other.GetParent(); if (Shell32.ILIsEqual(myParent.m_Pidl, theirParent.m_Pidl)) { return(myParent.GetIShellFolder().CompareIDs((SHCIDS)hint, Shell32.ILFindLastID(m_Pidl), Shell32.ILFindLastID(other.m_Pidl))); } else { return(1); } }
void Initialize(IListItemEx[] items) { this._Items = items; IntPtr[] pidls = new IntPtr[items.Length]; IListItemEx parent = null; for (int n = 0; n < items.Length; ++n) { pidls[n] = Shell32.ILFindLastID(items[n].PIDL); if (parent == null) { if (items[n].ParsingName.Equals(ShellItem.Desktop.ParsingName)) { parent = FileSystemListItem.ToFileSystemItem(IntPtr.Zero, ShellItem.Desktop.Pidl); } else { parent = items[n].Parent; } } else if (!items[n].Parent.Equals(parent)) { throw new Exception("All shell items must have the same parent"); } } if (items.Length == 0) { var desktop = KnownFolders.Desktop as ShellItem; var ishellViewPtr = desktop.GetIShellFolder().CreateViewObject(IntPtr.Zero, typeof(IShellView).GUID); var view = Marshal.GetObjectForIUnknown(ishellViewPtr) as IShellView; view.GetItemObject(SVGIO.SVGIO_BACKGROUND, typeof(IContextMenu).GUID, out _Result); Marshal.ReleaseComObject(view); } else { parent.GetIShellFolder().GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length, pidls, typeof(IContextMenu).GUID, 0, out _Result); } m_ComInterface = (IContextMenu)Marshal.GetTypedObjectForIUnknown(_Result, typeof(IContextMenu)); m_ComInterface2 = m_ComInterface as IContextMenu2; m_ComInterface3 = m_ComInterface as IContextMenu3; m_MessageWindow = new MessageWindow(this); }
void Initialize(ShellItem[] items) { IntPtr[] pidls = new IntPtr[items.Length]; ShellItem parent = null; IntPtr result; for (int n = 0; n < items.Length; ++n) { pidls[n] = Shell32.ILFindLastID(items[n].Pidl); if (parent == null) { if (items[n] == ShellItem.Desktop) { parent = ShellItem.Desktop; } else { parent = items[n].Parent; } } else { if (items[n].Parent != parent) { throw new Exception("All shell items must have the same parent"); } } } parent.GetIShellFolder().GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length, pidls, typeof(IContextMenu).GUID, 0, out result); m_ComInterface = (IContextMenu) Marshal.GetTypedObjectForIUnknown(result, typeof(IContextMenu)); m_ComInterface2 = m_ComInterface as IContextMenu2; m_ComInterface3 = m_ComInterface as IContextMenu3; m_MessageWindow = new MessageWindow(this); }
private void m_TreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { TreeNode node = e.Node; if (node.Parent == null) { return; } ShellItem parentFolder = (ShellItem)node.Parent.Tag; ShellItem item = (ShellItem)node.Tag; string NewName = e.Label; IntPtr newPidl = IntPtr.Zero; if (NewName != null) { try { uint res = parentFolder.GetIShellFolder().SetNameOf(m_TreeView.Handle, Shell32.ILFindLastID(item.Pidl), NewName, SHGDN.NORMAL, out newPidl); } catch (COMException ex) { // Ignore the exception raised when the user cancels // a delete operation. if (ex.ErrorCode != unchecked ((int)0x800704C7) && ex.ErrorCode != unchecked ((int)0x80270000)) { throw; } e.CancelEdit = true; } } }
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; ////} }
/// <summary> /// If this method returns true then the caller must call ReleaseMenu /// </summary> /// <param name="hwnd">The handle to the control to host the ContextMenu</param> /// <param name="items">The items for which to show the ContextMenu. These items must be in the same folder.</param> /// <param name="pt">The point where the ContextMenu should appear</param> /// <param name="allowrename">Set if (the ContextMenu should contain the Rename command where appropriate</param> /// <param name="cmi">The command information for the users selection</param> /// <returns></returns> /// <remarks></remarks> public bool ShowMenu(IntPtr hwnd, ShellItem[] items, System.Drawing.Point pt, bool allowRename, ref CMInvokeCommandInfoEx cmi) { bool bShowMenu = false; Debug.Assert(items.Length > 0); IntPtr comContextMenu = User32.CreatePopupMenu(); IntPtr[] pidls = new System.IntPtr[items.Length]; int i; IShellFolder folder = null; if (items[0] == ShellItem.Desktop) { folder = items[0].GetIShellFolder(); } else { folder = items[0].Parent.GetIShellFolder(); } for (i = 0; i <= items.Length - 1; i++) { if (!items[i].CanRename) { allowRename = false; } pidls[i] = Shell32.ILFindLastID(items[i].Pidl); } int prgf = 0; IntPtr pIcontext = IntPtr.Zero; int HR = folder.GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length, pidls, Shell32.IID_IContextMenu, (uint)prgf, out pIcontext); if (HR != (int)HResult.S_OK) { #if DEBUG Marshal.ThrowExceptionForHR(HR); #endif //GoTo FAIL; } winMenu = (IContextMenu)Marshal.GetObjectForIUnknown(pIcontext); IntPtr p = IntPtr.Zero; Marshal.QueryInterface(pIcontext, ref Shell32.IID_IContextMenu2, out p); if (p != IntPtr.Zero) { winMenu2 = (IContextMenu2)Marshal.GetObjectForIUnknown(p); } Marshal.QueryInterface(pIcontext, ref Shell32.IID_IContextMenu3, out p); if (p != IntPtr.Zero) { winMenu3 = (IContextMenu3)Marshal.GetObjectForIUnknown(p); } if (!pIcontext.Equals(IntPtr.Zero)) { pIcontext = IntPtr.Zero; } //Check item count - should always be 0 but check just in case uint startIndex = User32.GetMenuItemCount(comContextMenu); //Fill the context menu CMF flags = (CMF.NORMAL | CMF.EXPLORE); if (allowRename) { flags = flags | CMF.CANRENAME; } if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) { flags = flags | CMF.EXTENDEDVERBS; } int idCount = (int)winMenu.QueryContextMenu(comContextMenu, startIndex, min, max, flags); int cmd = User32.TrackPopupMenuEx(comContextMenu, TPM.TPM_RETURNCMD, pt.X, pt.Y, hwnd, IntPtr.Zero); if (cmd >= min && cmd <= idCount) { cmi = new CMInvokeCommandInfoEx(); cmi.cbSize = Marshal.SizeOf(cmi); cmi.lpVerb = (IntPtr)(cmd - min); cmi.lpVerbW = (IntPtr)(cmd - min); cmi.nShow = (int)SW.SHOWNORMAL; cmi.fMask = (int)(CMIC.UNICODE | CMIC.PTINVOKE); cmi.ptInvoke = new System.Drawing.Point(pt.X, pt.Y); bShowMenu = true; } else { //FAIL: if (winMenu != null) { Marshal.ReleaseComObject(winMenu); winMenu = null; } bShowMenu = false; } if (winMenu2 != null) { Marshal.ReleaseComObject(winMenu2); winMenu2 = null; } if (winMenu3 != null) { Marshal.ReleaseComObject(winMenu3); winMenu3 = null; } if (!comContextMenu.Equals(IntPtr.Zero)) { Marshal.Release(comContextMenu); comContextMenu = IntPtr.Zero; } return(bShowMenu); }