/// <summary> /// Selects a path from a value from the SpecialFolders enumeration. /// </summary> /// <param name="specialFolder">The SpecialFolder to select</param> /// <returns>The TreeNode of the directory which was selected, this will be null if the directory /// doesn't exist</returns> public TreeNode SelectPath(SpecialFolders specialFolder, bool expandNode) { StringBuilder path = new StringBuilder(256); IntPtr pidl = IntPtr.Zero; if (specialFolder == SpecialFolders.Desktop) { return(SelectPath("Desktop", expandNode)); } else if (ShellAPI.SHGetFolderPath( IntPtr.Zero, (ShellAPI.CSIDL)specialFolder, IntPtr.Zero, ShellAPI.SHGFP.TYPE_CURRENT, path) == ShellAPI.S_OK) { path.Replace(ShellBrowser.MyDocumentsPath, ShellBrowser.MyDocumentsName); return(SelectPath(path.ToString(), expandNode)); } else { #region Get Pidl if (specialFolder == SpecialFolders.MyDocuments) { uint pchEaten = 0; ShellAPI.SFGAO pdwAttributes = 0; ShellBrowser.DesktopItem.ShellFolder.ParseDisplayName( IntPtr.Zero, IntPtr.Zero, "::{450d8fba-ad25-11d0-98a8-0800361b1103}", ref pchEaten, out pidl, ref pdwAttributes); } else { ShellAPI.SHGetSpecialFolderLocation( IntPtr.Zero, (ShellAPI.CSIDL)specialFolder, out pidl); } #endregion #region Make Path if (pidl != IntPtr.Zero) { IntPtr strr = Marshal.AllocCoTaskMem(ShellAPI.MAX_PATH * 2 + 4); Marshal.WriteInt32(strr, 0, 0); StringBuilder buf = new StringBuilder(ShellAPI.MAX_PATH); if (ShellBrowser.DesktopItem.ShellFolder.GetDisplayNameOf( pidl, ShellAPI.SHGNO.FORADDRESSBAR | ShellAPI.SHGNO.FORPARSING, strr) == ShellAPI.S_OK) { ShellAPI.StrRetToBuf(strr, pidl, buf, ShellAPI.MAX_PATH); } Marshal.FreeCoTaskMem(pidl); Marshal.FreeCoTaskMem(strr); if (!string.IsNullOrEmpty(buf.ToString())) { return(SelectPath(buf.ToString(), expandNode)); } else { return(null); } } else { return(null); } #endregion } }
internal static void SetLargeImageList(ListView listView) { ShellAPI.SendMessage(listView.Handle, ShellAPI.WM.LVM_SETIMAGELIST, TVSIL_NORMAL, largeImageListHandle); }
internal static void SetIconIndex(ShellItem item, int index, bool SelectedIcon) { bool HasOverlay = false; //true if it's an overlay int rVal = 0; //The returned Index ShellAPI.SHGFI dwflag = ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.ICON; ShellAPI.FILE_ATTRIBUTE dwAttr = 0; //build Key into HashTable for this Item int Key = index * 256; if (item.IsLink) { Key = Key | 1; dwflag = dwflag | ShellAPI.SHGFI.LINKOVERLAY; HasOverlay = true; } if (item.IsShared) { Key = Key | 2; dwflag = dwflag | ShellAPI.SHGFI.ADDOVERLAYS; HasOverlay = true; } if (SelectedIcon) { Key = Key | 4; dwflag = dwflag | ShellAPI.SHGFI.OPENICON; HasOverlay = true; //not really an overlay, but handled the same } if (imageTable.ContainsKey(Key)) { rVal = (int)imageTable[Key]; } else if (!HasOverlay && !item.IsHidden) //for non-overlay icons, we already have { rVal = (int)System.Math.Floor((double)Key / 256); // the right index -- put in table imageTable[Key] = rVal; } else //don't have iconindex for an overlay, get it. { if (item.IsFileSystem & !item.IsDisk & !item.IsFolder) { dwflag = dwflag | ShellAPI.SHGFI.USEFILEATTRIBUTES; dwAttr = dwAttr | ShellAPI.FILE_ATTRIBUTE.NORMAL; } PIDL pidlFull = item.PIDLFull; ShellAPI.SHFILEINFO shfiSmall = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(pidlFull.Ptr, dwAttr, ref shfiSmall, ShellAPI.cbFileInfo, dwflag | ShellAPI.SHGFI.SMALLICON); ShellAPI.SHFILEINFO shfiLarge = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(pidlFull.Ptr, dwAttr, ref shfiLarge, ShellAPI.cbFileInfo, dwflag | ShellAPI.SHGFI.LARGEICON); Marshal.FreeCoTaskMem(pidlFull.Ptr); lock (imageTable) { rVal = ShellAPI.ImageList_ReplaceIcon(smallImageListHandle, -1, shfiSmall.hIcon); ShellAPI.ImageList_ReplaceIcon(largeImageListHandle, -1, shfiLarge.hIcon); } ShellAPI.DestroyIcon(shfiSmall.hIcon); ShellAPI.DestroyIcon(shfiLarge.hIcon); imageTable[Key] = rVal; } if (SelectedIcon) { item.SelectedImageIndex = rVal; } else { item.ImageIndex = rVal; } }
internal static void SetSmallImageList(TreeView treeView) { ShellAPI.SendMessage(treeView.Handle, ShellAPI.WM.TVM_SETIMAGELIST, TVSIL_NORMAL, smallImageListHandle); }
internal static void SetSmallImageList(ListView listView) { ShellAPI.SendMessage(listView.Handle, ShellAPI.WM.LVM_SETIMAGELIST, TVSIL_SMALL, smallImageListHandle); }
void FolderView_HandleDestroyed(object sender, EventArgs e) { ShellAPI.RevokeDragDrop(br.FolderView.Handle); }
public string Popup(FileSystemInfoEx[] items, Point pt) { if (items.Length > 0 && !_contextMenuVisible) { //0.15: Fixed ShellFolder not freed correctly. try { using (ShellFolder2 parentShellFolder = items[0].Parent != null ? items[0].Parent.ShellFolder : DirectoryInfoEx.DesktopDirectory.ShellFolder) try { _contextMenuVisible = true; ///Debug.WriteLine(items[0].Parent.FullName); IntPtr ptrContextMenu = IntPtr.Zero; IntPtr ptrContextMenu2 = IntPtr.Zero; IntPtr PtrContextMenu3 = IntPtr.Zero; IntPtr contextMenu = IntPtr.Zero; List <IntPtr> menuPtrConstructed = new List <IntPtr>(); List <IntPtr> imgPtrConstructed = new List <IntPtr>(); PIDL[] pidls = IOTools.GetPIDL(items, true); if (ContextMenuHelper.GetIContextMenu(parentShellFolder, IOTools.GetPIDLPtr(pidls), out ptrContextMenu, out _iContextMenu)) { try { queryMenuItemsEventArgs = new QueryMenuItemsEventArgs(items); if (OnQueryMenuItems != null) { OnQueryMenuItems(this, queryMenuItemsEventArgs); } contextMenu = ShellAPI.CreatePopupMenu(); if (queryMenuItemsEventArgs.QueryContextMenu) { _iContextMenu.QueryContextMenu(contextMenu, 0, ShellAPI.CMD_FIRST, ShellAPI.CMD_LAST, ShellAPI.CMF.EXPLORE | ShellAPI.CMF.CANRENAME | ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellAPI.CMF.EXTENDEDVERBS : 0)); } #region obsolute //for (uint i = 0; i < queryMenuItemsEventArgs.ExtraMenuItems.Length; i++) //{ // string caption = queryMenuItemsEventArgs.ExtraMenuItems[i]; // if (caption != "---") // { // ShellAPI.InsertMenu(contextMenu, i, ShellAPI.MFT.BYPOSITION, // ShellAPI.CMD_LAST + i + 1, caption); // if (queryMenuItemsEventArgs.DefaultItem == i) // ShellAPI.SetMenuDefaultItem(contextMenu, i, true); // } // else ShellAPI.InsertMenu(contextMenu, i, ShellAPI.MFT.BYPOSITION | // ShellAPI.MFT.SEPARATOR, 0, "-"); //} #endregion //0.11: Added ContextMenuWrapper OnQueryMenuItems event now support multilevel directory. (e.g. @"Tools\Add") ContextMenuHelperEx.ConstructCustomMenu(contextMenu, queryMenuItemsEventArgs.ExtraMenuItems, out menuPtrConstructed, out imgPtrConstructed); if (queryMenuItemsEventArgs.QueryContextMenu2) { try { Marshal.QueryInterface(ptrContextMenu, ref ShellAPI.IID_IContextMenu2, out ptrContextMenu2); _iContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown( ptrContextMenu2, typeof(IContextMenu2)); } catch (Exception) { } } if (queryMenuItemsEventArgs.QueryContextMenu3) { try { Marshal.QueryInterface(ptrContextMenu, ref ShellAPI.IID_IContextMenu3, out PtrContextMenu3); _iContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown( PtrContextMenu3, typeof(IContextMenu3)); } catch (Exception) { } } uint GMDI_USEDISABLED = 0x0001; //uint GMDI_GOINTOPOPUPS = 0x0002; uint intDefaultItem = (uint)ShellAPI.GetMenuDefaultItem(contextMenu, false, GMDI_USEDISABLED); string strDefaultCommand = intDefaultItem >= ShellAPI.CMD_FIRST ? ContextMenuHelper.GetCommandString(_iContextMenu, intDefaultItem - ShellAPI.CMD_FIRST, true) : null; if (queryMenuItemsEventArgs.QueryContextMenu) //No need to Disable if query is not carried out in first place. { //0.11: Added queryMenuItemsEventArgs.GrayedItems / HiddenItems ContextMenuHelperEx.DisableMenuItems(contextMenu, _iContextMenu, queryMenuItemsEventArgs.GrayedItems, ContextMenuHelperEx.DisabledMethods.Gray); ContextMenuHelperEx.DisableMenuItems(contextMenu, _iContextMenu, queryMenuItemsEventArgs.HiddenItems, ContextMenuHelperEx.DisabledMethods.Remove); } //0.17: Added DefaultItem and DefaultCommand in BeforePopup bool cont = true; if (OnBeforePopup != null) { BeforePopupEventArgs args = new BeforePopupEventArgs (contextMenu, _iContextMenu, intDefaultItem - ShellAPI.CMD_FIRST, strDefaultCommand); OnBeforePopup(this, args); cont = args.ContinuePopup; } if (cont) { //0.18 Fixed Context menu disappear in some case. (By cwharmon) //http://www.codeproject.com/KB/files/DirectoryInfoEx.aspx#xx3475961xx ShellAPI.SetForegroundWindow(this.Handle); uint selected = ShellAPI.TrackPopupMenuEx(contextMenu, ShellAPI.TPM.RETURNCMD, pt.X, pt.Y, this.Handle, IntPtr.Zero); uint msg = 0; ShellAPI.PostMessage(this.Handle, msg, IntPtr.Zero, IntPtr.Zero); if (OnMouseHover != null) { OnMouseHover(this, new MouseHoverEventArgs("", "", 0)); } if (selected >= ShellAPI.CMD_LAST) { return(removeCheckedSymbol(queryMenuItemsEventArgs.ExtraMenuItems[selected - ShellAPI.CMD_LAST])); } if (selected >= ShellAPI.CMD_FIRST) { string command = ContextMenuHelper.GetCommandString(_iContextMenu, selected - ShellAPI.CMD_FIRST, true); if (command == null) { return(null); } if (OnBeforeInvokeCommand != null) { InvokeCommandEventArgs args = new InvokeCommandEventArgs(command, "", selected, items); OnBeforeInvokeCommand(this, args); if (!args.ContinueInvoke) { return(command); } } if (command == "rename") { return("rename"); } else { //if (items.Length == 1 && items[0] is DirectoryInfoEx) ContextMenuHelper.InvokeCommand(_iContextMenu, selected - ShellAPI.CMD_FIRST, (items[0].Parent != null) ? items[0].Parent.FullName : items[0].FullName, pt); //else //ContextMenuHelper.InvokeCommand(items[0].Parent, // IOTools.GetPIDLPtr(items, true), selected - ShellAPI.CMD_FIRST, // pt); } } } } finally { IOTools.FreePIDL(pidls); if (_iContextMenu != null) { Marshal.ReleaseComObject(_iContextMenu); _iContextMenu = null; } if (_iContextMenu2 != null) { Marshal.ReleaseComObject(_iContextMenu2); _iContextMenu2 = null; } if (_iContextMenu3 != null) { Marshal.ReleaseComObject(_iContextMenu3); _iContextMenu3 = null; } foreach (IntPtr menuPtr in menuPtrConstructed) { ShellAPI.DestroyMenu(menuPtr); } menuPtrConstructed.Clear(); if (contextMenu != null) { ShellAPI.DestroyMenu(contextMenu); } if (ptrContextMenu != IntPtr.Zero) { Marshal.Release(ptrContextMenu); } if (ptrContextMenu2 != IntPtr.Zero) { Marshal.Release(ptrContextMenu2); } if (PtrContextMenu3 != IntPtr.Zero) { Marshal.Release(PtrContextMenu3); } } } } finally { _contextMenuVisible = false; } } catch { } } return(null); }
void FolderView_HandleCreated(object sender, EventArgs e) { treeViewHandle = br.FolderView.Handle; ShellAPI.RegisterDragDrop(treeViewHandle, this); }
void FileView_HandleDestroyed(object sender, EventArgs e) { ShellAPI.RevokeDragDrop(listViewHandle); }
void FileView_HandleCreated(object sender, EventArgs e) { listViewHandle = br.FileView.Handle; ShellAPI.RegisterDragDrop(listViewHandle, this); }
public int ShowContextMenu(Point pointScreen, string[] AddedItems) { IntPtr intPtr = IntPtr.Zero; IntPtr zero = IntPtr.Zero; IntPtr zero2 = IntPtr.Zero; IntPtr zero3 = IntPtr.Zero; int num = -1; int result; try { if (null == this._arrPIDLs) { this.ReleaseAll(); result = -2; } else if (!this.GetContextMenuInterfaces(this._oParentFolder, this._arrPIDLs, out zero)) { this.ReleaseAll(); result = -2; } else { intPtr = ShellContextMenu.CreatePopupMenu(); int num2 = this._oContextMenu.QueryContextMenu(intPtr, 0u, 1u, 30000u, ShellContextMenu.CMF.EXPLORE | (((Control.ModifierKeys & Keys.Shift) != Keys.None) ? ShellContextMenu.CMF.EXTENDEDVERBS : ShellContextMenu.CMF.NORMAL)); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu2, out zero2); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu3, out zero3); this._oContextMenu2 = (ShellContextMenu.IContextMenu2)Marshal.GetTypedObjectForIUnknown(zero2, typeof(ShellContextMenu.IContextMenu2)); this._oContextMenu3 = (ShellContextMenu.IContextMenu3)Marshal.GetTypedObjectForIUnknown(zero3, typeof(ShellContextMenu.IContextMenu3)); for (int i = 0; i < AddedItems.Length; i++) { ShellAPI.InsertMenu(intPtr, (uint)i, ShellAPI.MFT.BYPOSITION, (uint)(30001 + i), AddedItems[i]); } ShellAPI.InsertMenu(intPtr, (uint)AddedItems.Length, ShellAPI.MFT.SEPARATOR | ShellAPI.MFT.BYPOSITION, (uint)(30001 + AddedItems.Length), "-"); uint num3 = ShellContextMenu.TrackPopupMenuEx(intPtr, ShellContextMenu.TPM.RETURNCMD, pointScreen.X, pointScreen.Y, base.Handle, IntPtr.Zero); ShellContextMenu.DestroyMenu(intPtr); intPtr = IntPtr.Zero; if (AddedItems.Length > 0 && num3 >= 30001u && (ulong)num3 <= (ulong)((long)(AddedItems.Length - 1 + 30001))) { num = (int)(num3 - 30001u); } else if (num3 != 0u) { this.InvokeCommand(this._oContextMenu, num3, this._strParentFolder, pointScreen); } result = num; } } catch { Console.WriteLine("Error in Context Menu generation"); result = -1; } finally { if (intPtr != IntPtr.Zero) { ShellContextMenu.DestroyMenu(intPtr); } if (zero != IntPtr.Zero) { Marshal.Release(zero); } if (zero2 != IntPtr.Zero) { Marshal.Release(zero2); } if (zero3 != IntPtr.Zero) { Marshal.Release(zero3); } this.ReleaseAll(); } return(result); }
protected override void WndProc(ref Message m) { if (m.Msg == (int)ShellAPI.WM.SH_NOTIFY) { ShellAPI.SHNOTIFYSTRUCT shNotify = (ShellAPI.SHNOTIFYSTRUCT)Marshal.PtrToStructure(m.WParam, typeof(ShellAPI.SHNOTIFYSTRUCT)); switch ((ShellAPI.SHCNE)m.LParam) { //#region File Changes //case ShellAPI.SHCNE.CREATE: // #region Create Item // { // if (!PIDL.IsEmpty(shNotify.dwItem1)) // { // IntPtr parent, child, relative; // PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); // PIDL parentPIDL = new PIDL(parent, false); // ShellItem parentItem = br.GetShellItem(parentPIDL); // if (parentItem != null && parentItem.FilesExpanded && !parentItem.SubFiles.Contains(child)) // { // ShellAPI.SHGetRealIDL( // parentItem.ShellFolder, // child, // out relative); // parentItem.AddItem(new ShellItem(br, parentItem, relative)); // } // Marshal.FreeCoTaskMem(child); // parentPIDL.Free(); // } // } // #endregion // break; //case ShellAPI.SHCNE.RENAMEITEM: // #region Rename Item // { // if (!PIDL.IsEmpty(shNotify.dwItem1) && !PIDL.IsEmpty(shNotify.dwItem2)) // { // ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); // if (item != null) // item.Update(shNotify.dwItem2, ShellItemUpdateType.Renamed); // } // } // #endregion // break; //case ShellAPI.SHCNE.DELETE: // #region Delete Item // { // if (!PIDL.IsEmpty(shNotify.dwItem1)) // { // IntPtr parent, child; // PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); // PIDL parentPIDL = new PIDL(parent, false); // ShellItem parentItem = br.GetShellItem(parentPIDL); // if (parentItem != null && parentItem.SubFiles.Contains(child)) // parentItem.RemoveItem(parentItem.SubFiles[child]); // Marshal.FreeCoTaskMem(child); // parentPIDL.Free(); // } // } // #endregion // break; //case ShellAPI.SHCNE.UPDATEITEM: // #region Update Item // { // if (!PIDL.IsEmpty(shNotify.dwItem1)) // { // ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); // if (item != null) // { // Console.Out.WriteLine("Item: {0}", item); // item.Update(IntPtr.Zero, ShellItemUpdateType.Updated); // item.Update(IntPtr.Zero, ShellItemUpdateType.IconChange); // } // } // } // #endregion // break; //#endregion #region Folder Changes case ShellAPI.SHCNE.MKDIR: case ShellAPI.SHCNE.DRIVEADD: case ShellAPI.SHCNE.DRIVEADDGUI: #region Make Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child, relative; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.FoldersExpanded && !parentItem.SubFolders.Contains(child)) { ShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); IntPtr shellFolderPtr; if (parentItem.ShellFolder.BindToObject( relative, IntPtr.Zero, ref ShellAPI.IID_IShellFolder, out shellFolderPtr) == ShellAPI.S_OK) { parentItem.AddItem(new ShellItem(br, parentItem, relative, shellFolderPtr)); } else { Marshal.FreeCoTaskMem(relative); } } Marshal.FreeCoTaskMem(child); //parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEFOLDER: //#region Rename Directory //{ // if (!PIDL.IsEmpty(shNotify.dwItem1) && !PIDL.IsEmpty(shNotify.dwItem2)) // { // ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, false)); // if (item != null) // { // //Console.Out.WriteLine("Update: {0}", item); // item.Update(shNotify.dwItem2, ShellItemUpdateType.Renamed); // } // } //} //#endregion break; case ShellAPI.SHCNE.RMDIR: case ShellAPI.SHCNE.DRIVEREMOVED: //#region Remove Directory //{ // if (!PIDL.IsEmpty(shNotify.dwItem1)) // { // IntPtr parent, child; // PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); // PIDL parentPIDL = new PIDL(parent, false); // ShellItem parentItem = br.GetShellItem(parentPIDL); // if (parentItem != null && parentItem.SubFolders.Contains(child)) // parentItem.RemoveItem(parentItem.SubFolders[child]); // Marshal.FreeCoTaskMem(child); // parentPIDL.Free(); // } //} //#endregion break; case ShellAPI.SHCNE.UPDATEDIR: case ShellAPI.SHCNE.ATTRIBUTES: #region Update Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { item.Update(IntPtr.Zero, ShellItemUpdateType.Updated); item.Update(IntPtr.Zero, ShellItemUpdateType.IconChange); } } } #endregion break; case ShellAPI.SHCNE.MEDIAINSERTED: case ShellAPI.SHCNE.MEDIAREMOVED: #region Media Change { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { item.Update(IntPtr.Zero, ShellItemUpdateType.MediaChange); } } } #endregion break; #endregion #region Other Changes case ShellAPI.SHCNE.ASSOCCHANGED: break; case ShellAPI.SHCNE.NETSHARE: case ShellAPI.SHCNE.NETUNSHARE: break; case ShellAPI.SHCNE.UPDATEIMAGE: UpdateRecycleBin(); break; #endregion } } base.WndProc(ref m); }