// handle drag & drop
        protected override void OnDragDropTarget(DragDropTargetEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
            if (e.Type == DragDropTargetEventType.DragDrop)
            {
                if (e.DataObject[ShellDataObjectFormat.CFSTR_SHELLIDLIST] != null)
                {
                    // you can use other formats but this one, when present is the only that contains full PIDLs (to possible virtual items, not only file system items)
                    foreach (var idl in e.DataObject.ItemsIdLists)
                    {
                        // note we only handle file paths here but we could also support IStream, etc.
                        var path = idl.GetFileSystemPath();
                        if (path != null && IOUtilities.FileExists(path))
                        {
                            var targetPath = Path.Combine(FileSystemPath, Path.GetFileName(path));
                            IOUtilities.FileOverwrite(path, targetPath);

                            // tell whoever needs to know we've added a file here
                            NotifyUpdate();
                        }
                    }
                    return;
                }

                if (e.DataObject[ShellDataObjectFormat.CFSTR_FILEDESCRIPTORW] != null)
                {
                    // this format can be seen when drag & dropping attachments from outlook (files) or from chrome (data) for example
                    if (e.Type == DragDropTargetEventType.DragDrop)
                    {
                        for (int i = 0; i < e.DataObject.FileDescriptors.Count; i++)
                        {
                            // file content can be a temp file path or a byte[]
                            if (i < e.DataObject.FileContentsPaths.Count)
                            {
                                var tempFilePath = e.DataObject.FileContentsPaths[i];
                                if (tempFilePath != null && IOUtilities.FileExists(tempFilePath))
                                {
                                    var targetPath = Path.Combine(FileSystemPath, e.DataObject.FileDescriptors[i].cFileName);

                                    // temp files can be moved if used only once
                                    IOUtilities.FileMove(tempFilePath, targetPath);

                                    // tell whoever needs to know we've added a file here
                                    NotifyUpdate();
                                }
                            }
                            else if (i < e.DataObject.FileContentsDatas.Count)
                            {
                                var targetPath = Path.Combine(FileSystemPath, e.DataObject.FileDescriptors[i].cFileName);

                                File.WriteAllBytes(targetPath, e.DataObject.FileContentsDatas[i]);
                                // tell whoever needs to know we've added a file here
                                NotifyUpdate();
                            }
                        }
                    }
                    return;
                }
            }
        }
        private List <string> GetPaths(DragDropTargetEventArgs e)
        {
            var list = new List <string>();

            if (e.DataObject[ShellDataObjectFormat.CFSTR_SHELLIDLIST]?.ConvertedData is IEnumerable <ShellItemIdList> idls)
            {
                foreach (var idl in idls)
                {
                    string path;
                    var    item = Root.GetItem(idl);
                    if (item != null)
                    {
                        // this comes from ourselves
                        path = item.FileSystemPath;
                    }
                    else
                    {
                        // check it's a file system pidl
                        path = idl.GetFileSystemPath();
                    }

                    if (path != null)
                    {
                        list.Add(path);
                    }
                }
            }
            return(list);
        }
 protected override void OnDragDropTarget(DragDropTargetEventArgs e)
 {
     Console.WriteLine("OnDragDropTarget " + e.Type);
     e.Effect = DragDropEffects.Copy;
     foreach (var data in e.DataObject)
     {
         Console.WriteLine(" format: " + data.Name + " " + data.ConvertedData);
     }
 }
Beispiel #4
0
 private void OnDragDropService_PreviewDropTarget(object sender, DragDropTargetEventArgs e)
 {
     if (this.tool3Drop.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
     {
         if (e.DropTarget != this.toolWindow3.TabStrip)
         {
             e.DropTarget = null;
         }
     }
 }
        protected async override void OnDragDropTarget(DragDropTargetEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
            if (e.Type == DragDropTargetEventType.DragDrop)
            {
                // you can use other formats but this one is the only one that contains PIDLs
                var list = string.Join(Environment.NewLine, e.DataObject.ItemsIdLists.Select(id => id.GetName(Core.WindowsShell.SIGDN.SIGDN_NORMALDISPLAY)));

                await WindowsUtilities.DoModelessAsync(() =>
                {
                    MessageBox.Show(null, "UAC level is " + DiagnosticsInformation.GetTokenElevationType(), "Registry Folder");
                });
            }
        }
        protected override void OnDragDropTarget(DragDropTargetEventArgs e)
        {
            e.HResult = ShellUtilities.S_OK;
            var paths = GetPaths(e);

            if (paths.Count > 0)
            {
                e.Effect = DragDropEffects.All;
            }

            if (e.Type == DragDropTargetEventType.DragDrop)
            {
                // file operation events need an STA thread
                WindowsUtilities.DoModelessAsync(() =>
                {
                    using (var fo = new FileOperation(true))
                    {
                        fo.PostCopyItem += (sender, e2) =>
                        {
                            // we could add some logic here
                        };

                        if (paths.Count == 1)
                        {
                            fo.CopyItem(paths[0], FileSystemPath, null);
                        }
                        else
                        {
                            fo.CopyItems(paths, FileSystemPath);
                        }
                        fo.SetOperationFlags(FOF.FOF_ALLOWUNDO | FOF.FOF_NOCONFIRMMKDIR | FOF.FOF_RENAMEONCOLLISION);
                        fo.PerformOperations();
                        NotifyUpdate();
                    }
                });
            }
        }
Beispiel #7
0
 protected override void OnDragDropTarget(DragDropTargetEventArgs e)
 {
     e.Effect = DragDropEffects.Copy;
 }
Beispiel #8
0
        protected override void OnDragDropTarget(DragDropTargetEventArgs e)
        {
            // set the effect based upon KeyState
            // adapted from https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.drageventargs.keystate (we don't support link in that sample)
            if ((e.KeyState & 4) == 4 && e.AllowedEffect.HasFlag(DragDropEffects.Move))
            {
                // SHIFT state for move.
                e.Effect = DragDropEffects.Move;
            }
            else if ((e.KeyState & 8) == 8 && e.AllowedEffect.HasFlag(DragDropEffects.Copy))
            {
                // CTL state for copy.
                e.Effect = DragDropEffects.Copy;
            }
            else if (e.AllowedEffect.HasFlag(DragDropEffects.Move))
            {
                // by default, the drop action should be move, if allowed.
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.Copy;
            }

            // usually, the most complete information is from the "CFSTR_SHELLIDLIST" or ""Shell IDList Array" clipboard format
            // this is reflected into the ItemsIdLists property or e's DataObject
            // you can browse each item in that list

            // prepare from drop to another folder
            if (e.Type == DragDropTargetEventType.Begin)
            {
                // make sure we're trusted https://docs.microsoft.com/en-us/windows/win32/shell/clipboard#cfstr_untrusteddragdrop
                e.AddFormat("UntrustedDragDrop", BitConverter.GetBytes(0));

                // make sure items are present locally
                // note if the drop action is too fast, items may not be here yet (user will have to press "Retry")
                // copies could be made synchronously to avoid that
                foreach (var idList in e.DataObject.ItemsIdLists)
                {
                    // check it's a WebShellItem or WebShellFolder
                    var si = GetItem(idList.Last);
                    if (si == null || si.FileSystemPath == null)
                    {
                        continue;
                    }

                    if (si is not IObjectWithApiItem apiItem)
                    {
                        continue;
                    }

                    // this is a callback event, no need to wait
                    // we do this recursively, if there are folders
                    Task.Run(() => apiItem.ApiItem.EnsureLocalAsync(si.FileSystemPath, true));
                }
                return;
            }

            // someone Dragged & Dropped something on this folder, or Copied and Pasted something on his folder
            if (e.Type == DragDropTargetEventType.DragDrop)
            {
                // here, we do things in another thread, don't lock the user
                Task.Run(UpdloadItems);

                async Task UpdloadItems()
                {
                    foreach (var idList in e.DataObject.ItemsIdLists)
                    {
                        // you can also use GetFileSystemPath() or GetPath() method
                        // but items may not be physical (just like us...)

                        // so, we use the shell API
                        var item = Item.FromIDList(idList);

                        WebItem newItem;
                        // detect a call from our own extension
                        // so we can user server calls instead of transferring streams
                        var requestItem = new WebItem();
                        var sourceId    = item.GetPropertyValue <Guid>(ServerIdPk);
                        if (sourceId != Guid.Empty)
                        {
                            requestItem.Id = sourceId;
                            newItem        = await requestItem.MoveAsync(ApiItem.Id, new MoveOptions { Copy = e.Effect.HasFlag(DragDropEffects.Copy) }).ConfigureAwait(false);
                        }
                        else
                        {
                            using (var stream = item.OpenRead())
                            {
                                requestItem.ParentId   = ApiItem.Id;
                                requestItem.Name       = ApiItem.GetNewName(item.SIGDN_NORMALDISPLAY);
                                requestItem.Attributes = item.IsFileSystemFolder ? FileAttributes.Directory : FileAttributes.Normal;
                                var date = item.DateAccessed;
                                if (date.HasValue)
                                {
                                    requestItem.LastAccessTimeUtc = date.Value.ToUniversalTime();
                                }

                                date = item.DateCreated;
                                if (date.HasValue)
                                {
                                    requestItem.CreationTimeUtc = date.Value.ToUniversalTime();
                                }

                                date = item.DateModified;
                                if (date.HasValue)
                                {
                                    requestItem.LastWriteTimeUtc = date.Value.ToUniversalTime();
                                }

                                newItem = await requestItem.UploadAsync(stream).ConfigureAwait(false);
                            }
                        }
                        NotifyUpdate();
                    }
                }
            }
        }