C# Detect Desktop Folder Mouse-Up during a Drag operation?
< I have reviewed the "related questions" shown by SO before posting this >
Scenario :
Drag and drop initiated from a control within a WinForm.
note : all drag and drop code validated, carefully tested. I can use the code right now to create/write a file to the Desktop when the mouse is released.
Drag goes outside the Form, mouse goes up over the Desktop or some "item" on the Desktop.
[edit in response to comment by Groo]
In this case the drop may go onto the desktop (where it will become a file : that case is already handled); or, the drop may go into a control of the same type (as the control from which the drag was initiated) in another application's running instance (that case is taken care of). The case that I am asking about is where the drop occurs on a desktop Folder : Groo's comments have made want to test trying to mod开发者_开发知识库ify the DataObject type "on the fly" ... during the drag ... which I've never tried before : I have no idea if it's possible.
[end edit ]
I can use the api calls in this code sample [1*] to get an IntPtr ID for the mouse going up over the Desktop : the same IntPtr is returned for any Desktop item, like a Folder, a Shortcut, the Recycle Bin, etc.
What I want is : to be able to detect when the mouse is released over a Folder on the Desktop : of course what I want to do then is to get the path of the folder the mouse went up over : so I can create the file within that folder.
thanks, Bill
[1*]
"FindWindow By Jörg Bausch"
http://www.codeproject.com/KB/dialog/FindWindow.aspx?msg=3262771
If your file exists before you start to drag (or you are able to create it before), you can simply specify the source file path in the DataObject
instance passed to DoDragDrop
method.
Something like this (you should already have the appropriate handlers):
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
this.MouseDown += Form1_MouseDown;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
string[] files = new string[] { @"C:\SomeTestFile.txt" };
this.DoDragDrop(new DataObject(DataFormats.FileDrop, files),
DragDropEffects.Copy);
}
}
If you are interested in adding some fancy translucent effects while you are dragging, try this link. If your file doesn't exist and you really need to create it after it has been dropped to explorer, check this link.
You can also override DataObject
to handle the GetData
method which is called when the object is dropped:
public class MyDataObject : DataObject
{
public MyDataObject(string format, object data)
: base(format, data) { }
public override object GetData(string format)
{
MessageBox.Show("Format: "+format);
return base.GetData(format);
}
}
精彩评论