1

我知道下面的问题有点棘手,但我真的需要解决它......

想象一下,您有一个带有文本框控件(在我的例子中是 RichTextBox)的表单。现在,您在此控件中选择部分文本,启动拖动事件并将所选文本拖放到窗体外,直接到 Windows 资源管理器中的某个文件夹,例如桌面...

如何实现?除了以下链接,我在网上没有找到有用的东西:http ://forums.asp.net/t/1600192.aspx/1

我所知道的是这种文件写入操作称为“废文件生成”。有人对我有用吗?

4

1 回答 1

2

The FileDrop expects the files to exist and the drop just does a copy of the file. You will have to write your data out to a file and then pass that name to the DataObject. The below is just a demonstration and you will need to figure out how you write the files and clean everything up so you are not creating extra files on the user's PC.

private void richTextBox1_MouseLeave(object sender, EventArgs e)
{
    // If the left mouse button is down when leaving the rtb
    if (MouseButtons == MouseButtons.Left)
    {
        // Write everything to a temp file.
        System.IO.File.WriteAllText(@"z:\Temp\helloWorld.rtf", richTextBox1.SelectedRtf);
        string[] filenames = { @"z:\Temp\helloWorld.rtf" };
        DataObject obj = new DataObject();
        // Set the drag drop data with the FileDrop format
        obj.SetData(DataFormats.FileDrop, filenames);
        // Start the drag drop effect
        DoDragDrop(obj, DragDropEffects.All);
    }
}
于 2013-01-12T14:12:34.267 回答