//http://blogs.msdn.com/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx
        static void StartDrag(object sender, MouseEventArgs e)
        {
            UIElement control = sender as UIElement;

            draggingControl = control;
            try
            {
                control.QueryContinueDrag += new QueryContinueDragEventHandler(OnQueryContinueDrag);

                //control.AllowDrop = false;
                FileSystemInfoEx[] selectionList = getSelectedItems(sender);

                if (selectionList.Length > 0)
                {
                    dataObj = new DataObjectEx(selectionList);
                    dataObj.SetData(DataFormats.FileDrop, dataObj.PathnameList());
                    dataObj.SetData(DataObjectEx.DataFormats_EntryString, dataObj.PathnameList());
                    dataObj.SetData(ShellClipboardFormats.CFSTR_PREFERREDDROPEFFECT, DragDropEffects.Copy);
                    dataObj.SetData(ShellClipboardFormats.CFSTR_INDRAGLOOP, 1);
                    DragDropEffects de = System.Windows.DragDrop.DoDragDrop(control, dataObj, DragDropEffects.Copy);
                }
            }
            finally
            {
                draggingControl = null;
                SetIsDragOver(null);
            }
        }
Beispiel #2
0
        public static void DoDragAndDrop(Control dragSource, IEnumerable <DragFileInfo> files)
        {
            var dataObject      = new DataObjectEx();
            var fileDescriptors = new List <FileDescriptor>();
            var i = 0;

            foreach (var file in files)
            {
                dataObject.SetFileContents(file.Stream, i);
                fileDescriptors.Add(new FileDescriptor
                {
                    cFileName     = file.FileName,
                    nFileSizeHigh = (uint)(file.Stream.Length >> 32),
                    nFileSizeLow  = (uint)(file.Stream.Length & 0xFFFFFFFF),
                    dwFlags       = FileDescriptorFlags.FileSize | FileDescriptorFlags.ProgressUi
                });
                ++i;
            }

            var fileGroupDescriptor = new FileGroupDescriptor(fileDescriptors.Count);
            var descriptorSize      = Marshal.SizeOf(fileGroupDescriptor) +
                                      Marshal.SizeOf <FileDescriptor>() * fileDescriptors.Count;

            using (var descriptorStream = new MemoryStream(descriptorSize))
            {
                StructToArray(fileGroupDescriptor, descriptorStream);
                foreach (var descriptor in fileDescriptors)
                {
                    StructToArray(descriptor, descriptorStream);
                }
                dataObject.SetData("FileGroupDescriptorW", descriptorStream);

                dragSource.DoDragDrop(dataObject, DragDropEffects.Copy);
            }
        }
        private static void OnQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            UIElement uiSender = sender as UIElement;

            //ESC pressed
            if (e.EscapePressed)
            {
                e.Action           = DragAction.Cancel;
                uiSender.AllowDrop = true;
                return;
            }
            //Drop!
            if (e.KeyStates == DragDropKeyStates.None)
            {
                dataObj.SetData(ShellClipboardFormats.CFSTR_INDRAGLOOP, 0);
                e.Action           = DragAction.Drop;
                uiSender.AllowDrop = true;
                return;
            }

            e.Action = DragAction.Continue;
            //base.OnQueryContinueDrag(e);
        }