簡體   English   中英

C#從我的自定義應用程序拖放到記事本

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

我有一個需要支持拖放的自定義應用程序。 在我的應用程序中拖動網格時,在其DoDragDrop方法中,我提供了要以序列化格式放置的對象。

當拖放到我的一個應用程序上時,它可以對字符串進行反序列化並創建對象。

我想做的是允許源應用程序也可以放入NotePad / TextPad。 我可以看到可以將文件從Windows資源管理器拖放到Notepad,但是不能將純文本拖放到NotePad。 猜猜它檢查DragEnter事件中的DataFormat並禁止使用字符串,但允許將文件拖放到其中。

  • 有沒有一種方法可以在源應用程序中更改yr格式,從而提供臨時文件/字符串。
  • 是否可以提供2種格式的數據,以便目標放置可以接受其滿意的任何格式?

提前致謝!

您可以將多種格式的數據添加到傳遞給DoDragDrop調用的DataObject中,因此只需向SetData添加另一個調用即可添加新格式。 這是最合適的實現,通過這種方式Drop目標可以查詢可用格式並選擇最喜歡的格式。

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

這里

以多種格式存儲數據

對於此特定代碼段:

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