我正在努力完成一项我认为很简单的任务。获取拖放数据从资源管理器到Windows窗体的文件名
我有一个Windows Forms应用程序,用C++/CLI编写。我希望用户能够将文件拖放到表单中进行某种处理。
我设置表格行为的AllowDrop和的dragenter事件引发。
我如何检索文件名?我看到this example in C#,但没有得到任何运气移植到C++/CLI
这是我的尝试:
private: System::Void tabSingle_DragEnter(System::Object^ sender,
System::Windows::Forms::DragEventArgs^ e)
{
if (e->Effect == DragDropEffects::Link)
{
// This is true
}
try
{
String^ filename = (String^) e->Data->GetData("FileName");
}
catch(...)
{
// System.InvalidCastException: Impossible to convert an object of type 'System.String[]' in to'System.String'
}
}
提前感谢!
编辑:我更新了答案和我收到的例外的描述。看起来像预期的对象是一个System.String []。我应如何修改该行具有相同的行为,因为这行C#(从here)
Array data=((IDataObject)e.Data).GetData("FileName") as Array;
“这些总是失败”:*它是如何失败的?你得到的例外是什么? –
更新我的问题,收到的例外 – Lelo