C # Drag and drop from my custom application to notepad

I have a custom application that should support Drag and Drop. When dragging the grid in my application in the DoDragDrop method, I provided the object to be deleted in a serialized format.

When a fall refers to one of my applications, it can deserailize a string and create an object.

I want to make sure that the original application can also get into NotePad / TextPad. I see that I can drag and drop files from Windows Explorer into Notepad, but I can not drag plain text into NotePad. Guess that it checks the DataFormat in the DragEnter event and disallows rows, but allows files to be deleted.

  • Is there a way to change the yr format in the source application so that it provides a temporary file / line.
  • Is it possible to provide data in 2 formats so that the fall of the target can accept the format in which it is satisfied?

early!

+3
source share
2 answers

You can add several formats of your data to the DataObject that you pass to the DoDragDrop call, so just add another SetData call to add new formats. This is the most suitable implementation, so the Drop target can request available formats and choose the most suitable one.

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

Look here :

Saving data in multiple formats

This piece of code:

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);
+3
source

Source: https://habr.com/ru/post/1723490/


All Articles