【问题标题】:c# Drag and Drop from my custom app to notepadc# 从我的自定义应用拖放到记事本
【发布时间】:2009-11-20 17:35:39
【问题描述】:

我有一个需要支持拖放的自定义应用程序。 在我的应用程序中拖动网格时,在其 DoDragDrop 方法中,我提供了要以序列化格式放置的对象。

当拖放到我的一个应用程序时,它能够使字符串脱轨并创建对象。

我想要做的是让源应用程序也能够放入 NotePad/TextPad。我可以看到我可以将文件从 Windows 资源管理器拖放到记事本,但无法将纯文本拖放到记事本。猜猜它会检查 DragEnter 事件中的 DataFormat 并禁止字符串,但允许将文件放入其中。

  • 有没有办法在源应用程序中更改 yr 格式,以便它提供临时文件/字符串。
  • 是否可以提供 2 种格式的数据,以便目标 drop 可以接受它满意的任何格式?

提前致谢!

【问题讨论】:

    标签: c# drag-and-drop


    【解决方案1】:

    您可以将多种格式的数据添加到传递给 DoDragDrop 调用的 DataObject 中,因此只需添加另一个对 SetData 的调用以添加新格式。这是最合适的实现,这样 Drop 目标可以查询可用格式并选择它最喜欢的格式。

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

    【讨论】:

      【解决方案2】:

      见here:

      以多种格式存储数据

      对于这个特定的代码sn-p:

      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-23
        • 2019-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-01
        • 2015-12-29
        相关资源
        最近更新 更多