I have read many topics about cut/copy to clipboard, but haven't found the answer for my problem
I'm working on a "File Manager" application like Windows Explorer. Files are listed in a listview, in details view.
I have CUT/COPY/PASTE operations, and I know how to use File.Move, .Copy, Clipboard.GetFileDropList(), .SetFileDropList()........... and it works great.
What I dont know is how and where can I write in memory (which method) and how can I read from memory (Clipboard) which operation is last used, Cut or Copy? Is there any string in memory windows explorer wri开发者_如何转开发tes, that I can read and then know if it is CUT or COPY?
I want to let the user be able to cut/copy from my app to win Explorer and vice versa.
If you want to determine which Clipboard operation was last called, I think you have to listen to Windows messages, particularly WM_CUT (0x0300) versus WM_COPY (0x0301), and track which was last sent/received. You can override the Control.WndProc
method as discussed on MSDN.
Here are a few more reference links for Windows messages:
Clipboard messages
WM_CUT
WM_COPY
You use Clipboard.SetText
to add text to the clipboard (Copy), and Clipboard.GetText
to retrieve text from the clipboard (Paste).
There is an article here that should be able to help you out.
Also, regarding copy/paste of files, see this SO question:
Using this topic I wrote this code:
private const string DROP_EFFECT = "Preferred DropEffect";
private void CmdPaste()
{
DataObject data = Clipboard.GetDataObject() as DataObject;
var obj = data.GetData(DROP_EFFECT);
bool isCut = false;
if (obj != null)
{
if (obj is MemoryStream) //from Windows
{
var m = (obj as MemoryStream).ToArray();
isCut = m[0] == 2;
}
else
isCut = obj.ToString() == "Move";
}
PasteItems(data.GetFileDropList(), Path, isCut); //here start operation with items
}
private void ItemsToClipboard(StringCollection items, bool isCut)
{
DataObject data = new DataObject();
data.SetFileDropList(items);
if (isCut)
data.SetData(DROP_EFFECT, DragDropEffects.Move);
else
data.SetData(DROP_EFFECT, DragDropEffects.Copy);
Clipboard.Clear();
Clipboard.SetDataObject(data);
}
精彩评论