private void DragEnterOver(DragEventArgs e, DragType type) { var dropData = new FilesDropData(e.Data); if (dropData.HasFiles) { var filesEventArgs = new FilesDragEventArgs(dropData, new Point(e.X, e.Y), e.AllowedEffect, e.KeyState); filesEventArgs.Effect = e.Effect; switch (type) { case DragType.Enter: OnDragEnter(filesEventArgs); break; case DragType.Over: OnDragOver(filesEventArgs); break; } e.Effect = filesEventArgs.Effect; } else { e.Effect = DragDropEffects.None; } }
private void _dragDropManager_DragOver(object sender, FilesDragEventArgs e) { e.Effect = DragDropEffects.None; if (Root == null) { return; } Debug.Assert(_dragging); var node = _treeView.GetNodeAt(_treeView.PointToClient(new Point(e.X, e.Y))); _treeView.SelectedNode = node ?? _beforeDragSelection; if (node == null) { return; } var matches = BuildMatches((string)node.Tag, e); if (matches == null) { return; } var effectiveEffect = DragDropEffects.None; foreach (var match in matches) { DragDropEffects effect; if (match.Kind.IsMove()) { effect = DragDropEffects.Move; } else { effect = DragDropEffects.Copy; } if (effect != DragDropEffects.None) { if (effectiveEffect == DragDropEffects.None) { effectiveEffect = effect; } else { effectiveEffect = effectiveEffect & effect; } } } e.Effect = effectiveEffect & e.AllowedEffect; }
private void _dragDropManager_DragEnter(object sender, FilesDragEventArgs e) { if (_dragging) { return; } _dragging = true; _beforeDragSelection = _treeView.SelectedNode; }
private void _dragDropManager_DragEnter(object sender, FilesDragEventArgs e) { e.Effect = DragDropEffects.None; if (Directory == null) { return; } var matches = BuildMatches(e); if (matches == null) { return; } var effectiveEffect = DragDropEffects.None; foreach (var match in matches) { DragDropEffects effect; if (match.Kind.IsDirectory()) { effect = DragDropEffects.None; } else if (match.Kind.IsMove()) { effect = DragDropEffects.Move; } else { effect = DragDropEffects.Copy; } if (effect != DragDropEffects.None) { if (effectiveEffect == DragDropEffects.None) { effectiveEffect = effect; } else { effectiveEffect = effectiveEffect & effect; } } } e.Effect = effectiveEffect & e.AllowedEffect; }
private void _dragDropManager_DragDrop(object sender, FilesDragEventArgs e) { EndDragDrop(); var node = _treeView.GetNodeAt(_treeView.PointToClient(new Point(e.X, e.Y))); if (node == null) { return; } string targetPath = (string)node.Tag; var matches = BuildMatches(targetPath, e); if (matches == null) { return; } try { foreach (var match in matches) { string target = Path.Combine(targetPath, Path.GetFileName(match.Path)); if (match.Kind.IsFile() && File.Exists(target)) { var result = TaskDialogEx.Show( this, "The destination already has a file with this name. Do you want to overwrite the existing file?", FindForm().Text, TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel, TaskDialogIcon.Warning ); if (result == DialogResult.No) { continue; } if (result == DialogResult.Cancel) { return; } File.Delete(target); } switch (match.Kind) { case DropMatchKind.DirectoryMove: IODirectory.Move(match.Path, target); break; case DropMatchKind.DirectoryCopy: PathUtil.CopyDirectory(match.Path, target); break; case DropMatchKind.FileMove: File.Move(match.Path, target); break; case DropMatchKind.FileCopy: File.Copy(match.Path, target); break; case DropMatchKind.FileVirtual: File.WriteAllBytes(target, e.DropData.GetFileData(match.Index)); break; } } } catch { TaskDialogEx.Show(this, "Could not complete the operation", FindForm().Text, TaskDialogCommonButtons.OK, TaskDialogIcon.Error); } }
private List <DropMatch> BuildMatches(string targetPath, FilesDragEventArgs e) { return(DropMatch.FromDropData(Root, targetPath, FileBrowserManager, e)); }
public static List <DropMatch> FromDropData(string rootPath, string targetPath, FileBrowserManager fileBrowserManager, FilesDragEventArgs e) { if (rootPath == null) { throw new ArgumentNullException(nameof(rootPath)); } if (targetPath == null) { throw new ArgumentNullException(nameof(targetPath)); } if (e == null) { throw new ArgumentNullException(nameof(e)); } FilesDropData dropData = e.DropData; if (dropData == null) { throw new ArgumentNullException(nameof(dropData)); } var matches = new List <DropMatch>(); for (int i = 0; i < dropData.FileNames.Length; i++) { string fileName = dropData.FileNames[i]; if (!IOPath.IsPathRooted(fileName)) { // Normal drag/drop operations have the full path. Drag/drop operations // without a full path generally are Ole objects that don't point to // physical files, like drag/dropping from an Outlook email or a Windows // compressed folder. To get the data, matches.Add(new DropMatch(fileName, i, DropMatchKind.FileVirtual)); continue; } var fileInfo = new FileInfo(fileName); if (fileInfo.Attributes.IsHidden()) { continue; } bool contained = PathUtil.ContainsPath(rootPath, fileInfo.FullName) != PathContains.Not; bool ctrlKeyPressed = (e.KeyState & 8) != 0; bool shiftKeyPressed = (e.KeyState & 4) != 0; if (PathUtil.ContainsPath(targetPath, fileInfo.DirectoryName) == PathContains.Equals) { continue; } if (fileInfo.Attributes.IsDirectory()) { if (Directory.Exists(IOPath.Combine(targetPath, fileInfo.Name))) { continue; } if (PathUtil.ContainsPath(fileInfo.FullName, targetPath) != PathContains.Not) { continue; } var kind = contained ? DropMatchKind.DirectoryMove : DropMatchKind.DirectoryCopy; if (ctrlKeyPressed && kind == DropMatchKind.DirectoryMove) { kind = DropMatchKind.DirectoryCopy; } else if (shiftKeyPressed && kind == DropMatchKind.DirectoryCopy) { kind = DropMatchKind.DirectoryMove; } matches.Add(new DropMatch(fileInfo.FullName, i, kind)); } else if (fileBrowserManager != null && fileBrowserManager.Matches(fileInfo.FullName)) { var kind = contained ? DropMatchKind.FileMove : DropMatchKind.FileCopy; if (ctrlKeyPressed && kind == DropMatchKind.FileMove) { kind = DropMatchKind.FileCopy; } else if (shiftKeyPressed && kind == DropMatchKind.FileCopy) { kind = DropMatchKind.FileMove; } matches.Add(new DropMatch(fileInfo.FullName, i, kind)); } } return(matches); }
protected virtual void OnDragOver(FilesDragEventArgs e) { DragOver?.Invoke(this, e); }
protected virtual void OnDragDrop(FilesDragEventArgs e) { DragDrop?.Invoke(this, e); }
private List <DropMatch> BuildMatches(FilesDragEventArgs e) { return(DropMatch.FromDropData(Directory, Directory, FileBrowserManager, e)); }