|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionRecently, I needed to implement drag and drop with the Windows Explorer, but all of the examples I could find were not quite what I wanted. I wanted bi-directional, and in C#. So I wrote one. This sample project lists a folder full of files, and lets you drag and drop them into Explorer. You can also drag from Explorer into the sample, and you can use the Shift and Ctrl keys to modify the action, just like in Explorer. You can also use the right click options for cut and paste. I discovered that it is not possible to receive notifications when someone pastes your files into another folder (thereby cutting them out of the folder you are displaying) to allow you to update the view. So, I implemented a simple file watcher to watch the displayed folder and refresh it if it changes. BackgroundI found a couple of examples on MSDN for drag and drop, but not using files, but they were still useful (Performing Drag-and-Drop Operations and Control.DoDragDrop Method). Also, there was a snippet for doing cut & paste, which was useful: How to Cut Files to the Clipboard Programmatically in C#? Using the codeThe sample is fairly self explanatory, I’ve tried to cut out any code that wasn’t showing stuff that was relevant. To start a drag operation into Explorer, we implement the /// <summary>
/// Called when we start dragging an item out of our listview
/// </summary>
private void listView1_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
string[] files = GetSelection();
if(files != null)
{
DoDragDrop(new DataObject(DataFormats.FileDrop, files),
DragDropEffects.Copy |
DragDropEffects.Move /* |
DragDropEffects.Link */);
RefreshView();
}
}
Then, during the drag operation, if the drag happens over your window, your /// <summary>
/// Called when someone drags something over our listview
/// </summary>
private void listView1_DragOver(object sender,
System.Windows.Forms.DragEventArgs e)
{
// Determine whether file data exists in the drop data. If not, then
// the drop effect reflects that the drop cannot occur.
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.None;
return;
}
// Set the effect based upon the KeyState.
if ((e.KeyState & SHIFT) == SHIFT &&
(e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
e.Effect = DragDropEffects.Move;
}
else if ((e.KeyState & CTRL) == CTRL &&
(e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
e.Effect = DragDropEffects.Copy;
}
else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{
// By default, the drop action should be move, if allowed.
e.Effect = DragDropEffects.Move;
// Implement the rather strange behaviour of explorer that if the disk
// is different, then default to a COPY operation
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 0 && !files[0].ToUpper().StartsWith(homeDisk) &&
// Probably better ways to do this
(e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
// This is an example of how to get the item under the mouse
Point pt = listView1.PointToClient(new Point(e.X, e.Y));
ListViewItem itemUnder = listView1.GetItemAt(pt.X, pt.Y);
}
Then, when the user actually releases the mouse, the /// <summary>
/// Somebody dropped something on our listview - perform the action
/// </summary>
private void listView1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
// Can only drop files, so check
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
{
return;
}
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
string dest = homeFolder + "\\" + Path.GetFileName(file);
bool isFolder = Directory.Exists(file);
bool isFile = File.Exists(file);
if (!isFolder && !isFile)
// Ignore if it doesn't exist
continue;
try
{
switch(e.Effect)
{
case DragDropEffects.Copy:
if(isFile)
// TODO: Need to handle folders
File.Copy(file, dest, false);
break;
case DragDropEffects.Move:
if (isFile)
File.Move(file, dest);
break;
case DragDropEffects.Link:
// TODO: Need to handle links
break;
}
}
catch(IOException ex)
{
MessageBox.Show(this, "Failed to perform the" +
" specified operation:\n\n" + ex.Message,
"File operation failed", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
}
RefreshView();
}
That's all there is to it! Well, actually, there is a little more. If you want to use custom cursors, you can implement Cut and paste turned out to be quite simple as well. On the cut or copy operation, you just put the filenames on the clipboard using our trusty /// <summary>
/// Write files to clipboard (from
/// http://blogs.wdevs.com/idecember/
/// archive/2005/10/27/10979.aspx)
/// </summary>
/// <param name="cut">True if cut, false if copy</param>
void CopyToClipboard(bool cut)
{
string[] files = GetSelection();
if(files != null)
{
IDataObject data = new DataObject(DataFormats.FileDrop, files);
MemoryStream memo = new MemoryStream(4);
byte[] bytes = new byte[]{(byte)(cut ? 2 : 5), 0, 0, 0};
memo.Write(bytes, 0, bytes.Length);
data.SetData("Preferred DropEffect", memo);
Clipboard.SetDataObject(data);
}
}
And then on the paste (if someone pastes onto us), we do the reverse: /// <summary>
/// Paste context menu option
/// </summary>
private void pasteMenuItem_Click(object sender, System.EventArgs e)
{
IDataObject data = Clipboard.GetDataObject();
if (!data.GetDataPresent(DataFormats.FileDrop))
return;
string[] files = (string[])
data.GetData(DataFormats.FileDrop);
MemoryStream stream = (MemoryStream)
data.GetData("Preferred DropEffect", true);
int flag = stream.ReadByte();
if (flag != 2 && flag != 5)
return;
bool cut = (flag == 2);
foreach (string file in files)
{
string dest = homeFolder + "\\" +
Path.GetFileName(file);
try
{
if(cut)
File.Move(file, dest);
else
File.Copy(file, dest, false);
}
catch(IOException ex)
{
MessageBox.Show(this, "Failed to perform the" +
" specified operation:\n\n" + ex.Message,
"File operation failed",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
RefreshView();
}
Points of interestThe most annoying thing was that if you cut from the sample and paste into Explorer, Explorer will move the files for you and take them out of your folder. However, there is no way to get a notification when this paste happens, so it is impossible to keep your view of the files up to date. I implemented a small file watcher which watches the sample folder, but I imagine this could get much more complicated if you need to watch many folders.
|
||||||||||||||||||||||