public static Icon GetIcon(int index, bool small) { IntPtr iconPtr; if (small) { iconPtr = NativeShellAPI.ImageList_GetIcon(smallImageListHandle, index, NativeShellAPI.ILD.NORMAL); } else { iconPtr = NativeShellAPI.ImageList_GetIcon(largeImageListHandle, index, NativeShellAPI.ILD.NORMAL); } if (iconPtr != IntPtr.Zero) { Icon icon = Icon.FromHandle(iconPtr); Icon retVal = (Icon)icon.Clone(); NativeShellAPI.DestroyIcon(iconPtr); return(retVal); } else { return(null); } }
public static void Write(IntPtr pidl) { StringBuilder path = new StringBuilder(256); NativeShellAPI.SHGetPathFromIDList(pidl, path); Console.Out.WriteLine("Pidl: {0}", path); }
private void InitVars() { IntPtr tempPidl; NativeShellAPI.SHFILEINFO info; //My Computer info = new NativeShellAPI.SHFILEINFO(); tempPidl = IntPtr.Zero; NativeShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, NativeShellAPI.CSIDL.DRIVES, out tempPidl); NativeShellAPI.SHGetFileInfo(tempPidl, 0, ref info, NativeShellAPI.cbFileInfo, NativeShellAPI.SHGFI.PIDL | NativeShellAPI.SHGFI.DISPLAYNAME | NativeShellAPI.SHGFI.TYPENAME); sysfolderName = info.szTypeName; mycompName = info.szDisplayName; Marshal.FreeCoTaskMem(tempPidl); // //Dekstop tempPidl = IntPtr.Zero; NativeShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, NativeShellAPI.CSIDL.DESKTOP, out tempPidl); IntPtr desktopFolderPtr; NativeShellAPI.SHGetDesktopFolder(out desktopFolderPtr); desktopItem = new ShellItem(this, tempPidl, desktopFolderPtr); // //My Documents uint pchEaten = 0; NativeShellAPI.SFGAO pdwAttributes = 0; desktopItem.ShellFolder.ParseDisplayName( IntPtr.Zero, IntPtr.Zero, "::{450d8fba-ad25-11d0-98a8-0800361b1103}", ref pchEaten, out tempPidl, ref pdwAttributes); info = new NativeShellAPI.SHFILEINFO(); NativeShellAPI.SHGetFileInfo(tempPidl, 0, ref info, NativeShellAPI.cbFileInfo, NativeShellAPI.SHGFI.PIDL | NativeShellAPI.SHGFI.DISPLAYNAME); mydocsName = info.szDisplayName; Marshal.FreeCoTaskMem(tempPidl); StringBuilder path = new StringBuilder(NativeShellAPI.MAX_PATH); NativeShellAPI.SHGetFolderPath( IntPtr.Zero, NativeShellAPI.CSIDL.PERSONAL, IntPtr.Zero, NativeShellAPI.SHGFP.TYPE_CURRENT, path); mydocsPath = path.ToString(); // }
static ShellImageList() { imageTable = new Hashtable(); NativeShellAPI.SHGFI flag = NativeShellAPI.SHGFI.USEFILEATTRIBUTES | NativeShellAPI.SHGFI.SYSICONINDEX | NativeShellAPI.SHGFI.SMALLICON; NativeShellAPI.SHFILEINFO shfiSmall = new NativeShellAPI.SHFILEINFO(); smallImageListHandle = NativeShellAPI.SHGetFileInfo(".txt", NativeShellAPI.FILE_ATTRIBUTE.NORMAL, ref shfiSmall, NativeShellAPI.cbFileInfo, flag); flag = NativeShellAPI.SHGFI.USEFILEATTRIBUTES | NativeShellAPI.SHGFI.SYSICONINDEX | NativeShellAPI.SHGFI.LARGEICON; NativeShellAPI.SHFILEINFO shfiLarge = new NativeShellAPI.SHFILEINFO(); largeImageListHandle = NativeShellAPI.SHGetFileInfo(".txt", NativeShellAPI.FILE_ATTRIBUTE.NORMAL, ref shfiLarge, NativeShellAPI.cbFileInfo, flag); }
/// <summary> /// Registers the TreeView for drag/drop operations and uses this class as the IDropTarget /// </summary> /// <param name="br">The browser for which to support the drop</param> public BrowserTVDropWrapper(ShellBrowser br) { this.br = br; treeViewHandle = br.FolderView.Handle; NativeShellAPI.RegisterDragDrop(treeViewHandle, this); br.FolderView.HandleCreated += new EventHandler(FolderView_HandleCreated); br.FolderView.HandleDestroyed += new EventHandler(FolderView_HandleDestroyed); ShellHelper.GetIDropTargetHelper(out dropHelperPtr, out dropHelper); }
/// <summary> /// The native window's original handle is released /// and the handle of the TextBox is assigned to it. /// </summary> public ComboEditWindow(BrowserComboBox owner) { this.owner = owner; NativeShellAPI.COMBOBOXINFO info = new NativeShellAPI.COMBOBOXINFO(); info.cbSize = Marshal.SizeOf(typeof(NativeShellAPI.COMBOBOXINFO)); NativeShellAPI.GetComboBoxInfo(owner.ComboBox.Handle, ref info); if (!this.Handle.Equals(IntPtr.Zero)) { this.ReleaseHandle(); } this.AssignHandle(info.hwndEdit); }
/// <summary> /// Set the margin of the TextBox to make room for the icon /// </summary> /// <param name="margin">The margin to set</param> private void SetMargin(int margin) { if (this.margin != margin) { this.margin = margin; if (owner == null) { return; } NativeShellAPI.SendMessage( this.Handle, NativeShellAPI.WM.SETMARGINS, LEFTMARGIN, new IntPtr(margin)); } }
/// <summary> /// This method initialises the dragging of a TreeNode /// </summary> void ItemDrag(object sender, ItemDragEventArgs e) { ReleaseCom(); startButton = e.Button; ShellItem item = (ShellItem)((TreeNode)e.Item).Tag; dataObjectPtr = ShellHelper.GetIDataObject(new ShellItem[] { item }); if (dataObjectPtr != IntPtr.Zero) { DragDropEffects effects; OnDragStart(new DragEnterEventArgs((item.ParentItem != null ? item.ParentItem : item), br.FolderView)); NativeShellAPI.DoDragDrop(dataObjectPtr, this, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move, out effects); OnDragEnd(new EventArgs()); } }
public ShellBrowserUpdater(ShellBrowserComponent br) { this.br = br; CreateHandle(new CreateParams()); NativeShellAPI.SHChangeNotifyEntry entry = new NativeShellAPI.SHChangeNotifyEntry(); entry.pIdl = br.DesktopItem.PIDLRel.Ptr; entry.Recursively = true; notifyId = NativeShellAPI.SHChangeNotifyRegister( this.Handle, NativeShellAPI.SHCNRF.InterruptLevel | NativeShellAPI.SHCNRF.ShellLevel, NativeShellAPI.SHCNE.ALLEVENTS | NativeShellAPI.SHCNE.INTERRUPT, NativeShellAPI.WM.SH_NOTIFY, 1, new NativeShellAPI.SHChangeNotifyEntry[] { entry }); }
public static bool GetIDropTargetHelper(out IntPtr helperPtr, out IDropTargetHelper dropHelper) { if (NativeShellAPI.CoCreateInstance( ref NativeShellAPI.CLSID_DragDropHelper, IntPtr.Zero, NativeShellAPI.CLSCTX.INPROC_SERVER, ref NativeShellAPI.IID_IDropTargetHelper, out helperPtr) == NativeShellAPI.S_OK) { dropHelper = (IDropTargetHelper)Marshal.GetTypedObjectForIUnknown(helperPtr, typeof(IDropTargetHelper)); return(true); } else { dropHelper = null; helperPtr = IntPtr.Zero; return(false); } }
public override bool Equals(object obj) { try { if (obj is IntPtr) { return(NativeShellAPI.ILIsEqual(this.Ptr, (IntPtr)obj)); } if (obj is PIDL) { return(NativeShellAPI.ILIsEqual(this.Ptr, ((PIDL)obj).Ptr)); } else { return(false); } } catch (Exception) { return(false); } }
/// <summary> /// This method initialises the dragging of a ListViewItem /// </summary> void ItemDrag(object sender, ItemDragEventArgs e) { ReleaseCom(); startButton = e.Button; ShellItem[] items = new ShellItem[br.FileView.SelectedItems.Count]; for (int i = 0; i < br.FileView.SelectedItems.Count; i++) { items[i] = (ShellItem)br.FileView.SelectedItems[i].Tag; } dataObjectPtr = ShellHelper.GetIDataObject(items); if (dataObjectPtr != IntPtr.Zero) { DragDropEffects effects; OnDragStart(new DragEnterEventArgs((items[0].ParentItem != null ? items[0].ParentItem : items[0]), br.FileView)); NativeShellAPI.DoDragDrop(dataObjectPtr, this, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move, out effects); OnDragEnd(new EventArgs()); } }
void FolderView_HandleDestroyed(object sender, EventArgs e) { NativeShellAPI.RevokeDragDrop(br.FolderView.Handle); }
void FolderView_HandleCreated(object sender, EventArgs e) { treeViewHandle = br.FolderView.Handle; NativeShellAPI.RegisterDragDrop(treeViewHandle, this); }
internal static void SetSmallImageList(TreeView treeView) { NativeShellAPI.SendMessage(treeView.Handle, NativeShellAPI.WM.TVM_SETIMAGELIST, TVSIL_NORMAL, smallImageListHandle); }
void FileView_HandleDestroyed(object sender, EventArgs e) { NativeShellAPI.RevokeDragDrop(listViewHandle); }
void FileView_HandleCreated(object sender, EventArgs e) { listViewHandle = br.FileView.Handle; NativeShellAPI.RegisterDragDrop(listViewHandle, this); }
internal static void SetSmallImageList(ListView listView) { NativeShellAPI.SendMessage(listView.Handle, NativeShellAPI.WM.LVM_SETIMAGELIST, TVSIL_SMALL, smallImageListHandle); }
internal static void SetLargeImageList(ListView listView) { NativeShellAPI.SendMessage(listView.Handle, NativeShellAPI.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 NativeShellAPI.SHGFI dwflag = NativeShellAPI.SHGFI.SYSICONINDEX | NativeShellAPI.SHGFI.PIDL | NativeShellAPI.SHGFI.ICON; NativeShellAPI.FILE_ATTRIBUTE dwAttr = 0; //build Key into HashTable for this Item int Key = index * 256; if (item.IsLink) { Key = Key | 1; dwflag = dwflag | NativeShellAPI.SHGFI.LINKOVERLAY; HasOverlay = true; } if (item.IsShared) { Key = Key | 2; dwflag = dwflag | NativeShellAPI.SHGFI.ADDOVERLAYS; HasOverlay = true; } if (SelectedIcon) { Key = Key | 4; dwflag = dwflag | NativeShellAPI.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 | NativeShellAPI.SHGFI.USEFILEATTRIBUTES; dwAttr = dwAttr | NativeShellAPI.FILE_ATTRIBUTE.NORMAL; } PIDL pidlFull = item.PIDLFull; NativeShellAPI.SHFILEINFO shfiSmall = new NativeShellAPI.SHFILEINFO(); NativeShellAPI.SHGetFileInfo(pidlFull.Ptr, dwAttr, ref shfiSmall, NativeShellAPI.cbFileInfo, dwflag | NativeShellAPI.SHGFI.SMALLICON); NativeShellAPI.SHFILEINFO shfiLarge = new NativeShellAPI.SHFILEINFO(); NativeShellAPI.SHGetFileInfo(pidlFull.Ptr, dwAttr, ref shfiLarge, NativeShellAPI.cbFileInfo, dwflag | NativeShellAPI.SHGFI.LARGEICON); Marshal.FreeCoTaskMem(pidlFull.Ptr); lock (imageTable) { rVal = NativeShellAPI.ImageList_ReplaceIcon(smallImageListHandle, -1, shfiSmall.hIcon); NativeShellAPI.ImageList_ReplaceIcon(largeImageListHandle, -1, shfiLarge.hIcon); } NativeShellAPI.DestroyIcon(shfiSmall.hIcon); NativeShellAPI.DestroyIcon(shfiLarge.hIcon); imageTable[Key] = rVal; } if (SelectedIcon) { item.SelectedImageIndex = rVal; } else { item.ImageIndex = rVal; } }
protected override void WndProc(ref Message m) { if (m.Msg == (int)NativeShellAPI.WM.SH_NOTIFY) { NativeShellAPI.SHNOTIFYSTRUCT shNotify = (NativeShellAPI.SHNOTIFYSTRUCT)Marshal.PtrToStructure(m.WParam, typeof(NativeShellAPI.SHNOTIFYSTRUCT)); //Console.Out.WriteLine("Event: {0}", (ShellAPI.SHCNE)m.LParam); //if (shNotify.dwItem1 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem1); //if (shNotify.dwItem2 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem2); switch ((NativeShellAPI.SHCNE)m.LParam) { #region File Changes case NativeShellAPI.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)) { NativeShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); parentItem.AddItem(new ShellItem(br, parentItem, relative)); } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case NativeShellAPI.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 NativeShellAPI.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 NativeShellAPI.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 NativeShellAPI.SHCNE.MKDIR: case NativeShellAPI.SHCNE.DRIVEADD: case NativeShellAPI.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)) { NativeShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); IntPtr shellFolderPtr; if (parentItem.ShellFolder.BindToObject( relative, IntPtr.Zero, ref NativeShellAPI.IID_IShellFolder, out shellFolderPtr) == NativeShellAPI.S_OK) { parentItem.AddItem(new ShellItem(br, parentItem, relative, shellFolderPtr)); } else { Marshal.FreeCoTaskMem(relative); } } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case NativeShellAPI.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 NativeShellAPI.SHCNE.RMDIR: case NativeShellAPI.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 NativeShellAPI.SHCNE.UPDATEDIR: case NativeShellAPI.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 NativeShellAPI.SHCNE.MEDIAINSERTED: case NativeShellAPI.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 NativeShellAPI.SHCNE.ASSOCCHANGED: #region Update Images { } #endregion break; case NativeShellAPI.SHCNE.NETSHARE: case NativeShellAPI.SHCNE.NETUNSHARE: break; case NativeShellAPI.SHCNE.UPDATEIMAGE: UpdateRecycleBin(); break; #endregion } } base.WndProc(ref m); }
public static DragDropEffects CanDropClipboard(ShellItem item) { IntPtr dataObject; NativeShellAPI.OleGetClipboard(out dataObject); IntPtr targetPtr; IDropTarget target; DragDropEffects retVal = DragDropEffects.None; if (GetIDropTarget(item, out targetPtr, out target)) { #region Check Copy DragDropEffects effects = DragDropEffects.Copy; if (target.DragEnter( dataObject, NativeShellAPI.MK.CONTROL, new NativeShellAPI.POINT(0, 0), ref effects) == NativeShellAPI.S_OK) { if (effects == DragDropEffects.Copy) { retVal |= DragDropEffects.Copy; } target.DragLeave(); } #endregion #region Check Move effects = DragDropEffects.Move; if (target.DragEnter( dataObject, NativeShellAPI.MK.SHIFT, new NativeShellAPI.POINT(0, 0), ref effects) == NativeShellAPI.S_OK) { if (effects == DragDropEffects.Move) { retVal |= DragDropEffects.Move; } target.DragLeave(); } #endregion #region Check Lick effects = DragDropEffects.Link; if (target.DragEnter( dataObject, NativeShellAPI.MK.ALT, new NativeShellAPI.POINT(0, 0), ref effects) == NativeShellAPI.S_OK) { if (effects == DragDropEffects.Link) { retVal |= DragDropEffects.Link; } target.DragLeave(); } #endregion Marshal.ReleaseComObject(target); Marshal.Release(targetPtr); } return(retVal); }