Ok so I have this program that accepts files the user drags and drops onto a label box. The program is currently able to accept files that are dropped. I then save the files into a System::Object^. Inside the System::Object^ is a {System.Array} that holds the path of the files dropped onto the label box.
I need to be able to access the file paths in the {System.Array} inside of the System::Object^. I am converting another program that I wrote in Visual Basic to C++; so I'm trying to keep the code of both programs pretty close to each other. I have looked at OLE for drag and drop a little bit and I'm thinking that I should be able to perform drag and drop the way I started in this code. I feel OLE is too much for what I need, I just need the file paths for the files. Any ideas on how I can get the file paths?
private: System::Void lblDragHere_DragEnter(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
if (e->Data->GetDataPresent(System::Windows::Forms::DataFormats::FileDrop))
e->Effect = System::Windows::Forms::DragDropEffects::All;
else
e->Effect = System::Windows::Forms::DragDropEffects::None;
lblError->Visible = false;
blnSaveSuccessful = false;
}
private: System::Void lblDragHere_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
bool blnContinue = false;
// Checks if the user has not set a save location for the files.
if (lblSaveLocation->Text == "Current Save Location")
{
lblError->ForeColor = Color::Red;
lblError->Text = "Please select a save location first.";
lblError->Visible = true;
}
else
{
lblError->Visible = false;
// Checks to see if the user actually dropped anything onto lblDragHere
if (e开发者_JS百科->Data->GetDataPresent(DataFormats::FileDrop))
{
System::Object ^ MyFiles;
// Assign the files to the array.
MyFiles = e->Data->GetData(DataFormats::FileDrop);
}
}
}
You should be able to directly get the data as a cli array of file names with a cast:
if(e->Data->GetDataPresent(DataFormats::FileDrop))
{
// Assign the files to the array.
array<String^>^ myFiles = (array<String^>^)e->Data->GetData(DataFormats::FileDrop);
// Do something with the files
for each(String^ file in myFiles)
{
...
}
}
(You should use void
instead of System::Void, it's more readable)
精彩评论