您能否获得指向 System::Object^ 内的 {System.Array} 的指针?

发布于 2024-12-06 15:33:08 字数 1643 浏览 0 评论 0原文

好的,我有这个程序,它接受用户拖放到标签框中的文件。该程序当前能够接受删除的文件。然后我将文件保存到 System::Object^ 中。 System::Object^ 内部是一个 {System.Array},它保存拖放到标签框中的文件的路径。

我需要能够访问 System::Object^ 内的 {System.Array} 中的文件路径。我正在将用 Visual Basic 编写的另一个程序转换为 C++;所以我试图让两个程序的代码彼此非常接近。我稍微研究了一下 OLE 的拖放功能,我认为我应该能够按照我在这段代码中开始的方式执行拖放操作。我觉得 OLE 对于我的需要来说太多了,我只需要文件的文件路径。关于如何获取文件路径有什么想法吗?

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->Data->GetDataPresent(DataFormats::FileDrop))
        {
            System::Object ^ MyFiles;

            // Assign the files to the array.
            MyFiles = e->Data->GetData(DataFormats::FileDrop);
        }
    }
}

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->Data->GetDataPresent(DataFormats::FileDrop))
        {
            System::Object ^ MyFiles;

            // Assign the files to the array.
            MyFiles = e->Data->GetData(DataFormats::FileDrop);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2024-12-13 15:33:08

您应该能够通过强制转换直接以 cli 文件名数组形式获取数据:(

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)
    {
        ...
    }
}

您应该使用 void 而不是 System::Void,它更具可读性)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文