public static IntPtr SHParseDisplayName(string pszName, IBindCtx pbc, SFGAOF sfgaoIn, out SFGAOF psfgaoOut) { IntPtr pidl; SHParseDisplayName(pszName, pbc, out pidl, sfgaoIn, out psfgaoOut); return(pidl); }
/// <summary> /// Constructor. Create a sub-item shell item object. /// </summary> /// <param name="shDesktop">IShellFolder interface of the Desktop</param> /// <param name="pIDL">The fully qualified PIDL for this shell item</param> /// <param name="shParent">The ShellItem object for this item's parent</param> public ShellItem(IShellFolder shDesktop, IntPtr pIDL, ShellItem shParent) { // We need the Desktop shell item to exist first. if (m_bHaveRootShell == false) { throw new Exception("The root shell item must be created before creating a sub-item"); } // Create the FQ PIDL for this new item. m_pIDL = NativeShellApi.ILCombine(shParent.PIDL, pIDL); // Get the properties of this item. SFGAOF uFlags = SFGAOF.SFGAO_FOLDER | SFGAOF.SFGAO_HASSUBFOLDER; // Here we get some basic attributes. shDesktop.GetAttributesOf(1, out m_pIDL, out uFlags); IsFolder = Convert.ToBoolean(uFlags & SFGAOF.SFGAO_FOLDER); HasSubFolder = Convert.ToBoolean(uFlags & SFGAOF.SFGAO_HASSUBFOLDER); // Now we want to get extended attributes such as the icon index etc. SHFILEINFO shInfo = new SHFILEINFO(); SHGFI vFlags = SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_PIDL | SHGFI.SHGFI_DISPLAYNAME; NativeShellApi.SHGetFileInfo(m_pIDL, 0, out shInfo, (uint)Marshal.SizeOf(shInfo), vFlags); DisplayName = shInfo.szDisplayName; IconIndex = shInfo.iIcon; Path = GetPath(); // Create the IShellFolder interface for this item. if (IsFolder) { uint hRes = shParent.m_shShellFolder.BindToObject(pIDL, IntPtr.Zero, ref NativeShellApi.IID_IShellFolder, out m_shShellFolder); if (hRes != 0) { Marshal.ThrowExceptionForHR((int)hRes); } } }
internal static extern HRESULT SHParseDisplayName([In, MarshalAs(UnmanagedType.LPWStr)] string pszName, IntPtr pbc, out IntPtr ppidl, SFGAOF sfgaoIn, out SFGAOF psfgaoOut);
static extern void SHParseDisplayName(string pszName, IBindCtx pbc, out IntPtr ppidl, SFGAOF sfgaoIn, out SFGAOF psfgaoOut);
private void PopupContextMenu(string fromPath, int x, int y) { IntPtr ppv; ShellAPI.SHGetDesktopFolder(out ppv); IShellFolder isf = (IShellFolder)Marshal.GetObjectForIUnknown(ppv); if (isf == null) { return; } IntPtr pidl = IntPtr.Zero; int eaten; SFGAOF attribs = default(SFGAOF); isf.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, fromPath, out eaten, out pidl, ref attribs); IntPtr pidlChild = IntPtr.Zero; ShellAPI.SHBindToParent(pidl, typeof(IShellFolder).GUID, out ppv, ref pidlChild); IShellFolder parentFolder = (IShellFolder)Marshal.GetObjectForIUnknown(ppv); IntPtr[] apidl = new IntPtr[1] { pidlChild }; parentFolder.GetUIObjectOf(IntPtr.Zero, 1, apidl, typeof(IContextMenu).GUID, 0, out ppv); IContextMenu ctxMenuFirst = (IContextMenu)Marshal.GetObjectForIUnknown(ppv); var ctx2Guid = typeof(IContextMenu2).GUID; Marshal.QueryInterface(ppv, ref ctx2Guid, out ppv); IContextMenu2 ctxMenu = (IContextMenu2)Marshal.GetObjectForIUnknown(ppv); _ctxMenu = ctxMenu; IntPtr hMenu = ShellAPI.CreatePopupMenu(); ctxMenu.QueryContextMenu(hMenu, 0, 1, 0x7fff, CMF.CMF_NORMAL); int menuCount = ShellAPI.GetMenuItemCount(hMenu); for (int i = 0; i < menuCount; ++i) { StringBuilder strbuf = new StringBuilder(256); int res = ShellAPI.GetMenuString(hMenu, (uint)i, strbuf, strbuf.Capacity, (int)MF.MF_BYPOSITION); if (res > 0) { Debug.WriteLine(string.Format("{0:d2} {1}", i, strbuf.ToString())); } } const uint TPM_LEFTALIGN = 0; const uint TPM_RIGHTBUTTON = 0x0002; const uint TPM_RETURNCMD = 0x0100; uint flags = TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD; var hwndSource = PresentationSource.FromVisual(this) as HwndSource; ShellAPI.TrackPopupMenu(hMenu, flags, x, y, 0, hwndSource.Handle, IntPtr.Zero); ShellAPI.DestroyMenu(hMenu); _ctxMenu = null; }