简体   繁体   English

C#从我的自定义应用程序拖放到记事本

[英]c# Drag and Drop from my custom app to notepad

I have a custom app which needs to support Drag and Drop. 我有一个需要支持拖放的自定义应用程序。 On Dragging the grid in my app, in its DoDragDrop method I have provided the object to be dropped in a serialized format. 在我的应用程序中拖动网格时,在其DoDragDrop方法中,我提供了要以序列化格式放置的对象。

When the drop is to one of my apps, it is able to deserailize the string and create the object. 当拖放到我的一个应用程序上时,它可以对字符串进行反序列化并创建对象。

What I want to do is allow the source app to be able to drop into NotePad/TextPad as well. 我想做的是允许源应用程序也可以放入NotePad / TextPad。 I can see that I can drag and drop files from windows explorer to Notepad , but am not able to drag and drop plain text to NotePad. 我可以看到可以将文件从Windows资源管理器拖放到Notepad,但是不能将纯文本拖放到NotePad。 Guess it checks the DataFormat in the DragEnter event and dis-allows strings but allows files to be dropped into it. 猜猜它检查DragEnter事件中的DataFormat并禁止使用字符串,但允许将文件拖放到其中。

  • Is there a way to change yr format in the source app so that it provides a temp file / a string. 有没有一种方法可以在源应用程序中更改yr格式,从而提供临时文件/字符串。
  • Is it possible to provide the data in 2 formats so that the target drop can accept whichever format it is happy with? 是否可以提供2种格式的数据,以便目标放置可以接受其满意的任何格式?

thanks in advance! 提前致谢!

You can add multiple formats of your data to the DataObject you pass into the DoDragDrop call, so just add another call to SetData to add the new formats. 您可以将多种格式的数据添加到传递给DoDragDrop调用的DataObject中,因此只需向SetData添加另一个调用即可添加新格式。 This is the most appropriate implementation, this way the Drop target can query for available formats and choose the one it likes best. 这是最合适的实现,通过这种方式Drop目标可以查询可用格式并选择最喜欢的格式。

 DataObject d = new DataObject();
 d.SetData(DataFormats.Serializable, myObject);
 d.SetData(DataFormats.Text, myObject.ToString());
 myForm.DoDragDrop(d, DragDropEffects.Copy);

See here : 这里

Storing Data in Multiple Formats 以多种格式存储数据

For this particular code snippet: 对于此特定代码段:

DataObject dataObject = new DataObject();
string sourceData = "Some string data to store...";

// Encode the source string into Unicode byte arrays.
byte[] unicodeText = Encoding.Unicode.GetBytes(sourceData); // UTF-16
byte[] utf8Text = Encoding.UTF8.GetBytes(sourceData);
byte[] utf32Text = Encoding.UTF32.GetBytes(sourceData);

// The DataFormats class does not provide data format fields for denoting
// UTF-32 and UTF-8, which are seldom used in practice; the following strings 
// will be used to identify these "custom" data formats.
string utf32DataFormat = "UTF-32";
string utf8DataFormat  = "UTF-8";

// Store the text in the data object, letting the data object choose
// the data format (which will be DataFormats.Text in this case).
dataObject.SetData(sourceData);
// Store the Unicode text in the data object.  Text data can be automatically
// converted to Unicode (UTF-16 / UCS-2) format on extraction from the data object; 
// Therefore, explicitly converting the source text to Unicode is generally unnecessary, and
// is done here as an exercise only.
dataObject.SetData(DataFormats.UnicodeText, unicodeText);
// Store the UTF-8 text in the data object...
dataObject.SetData(utf8DataFormat, utf8Text);
// Store the UTF-32 text in the data object...
dataObject.SetData(utf32DataFormat, utf32Text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM