所以基本上我的 CLR C++ 项目在我按下按钮后冻结。按下后,应该会出现一个 openfiledialog,并允许用户选择一个 .txt 文件或任何其他文件,但是在调用 button_click 函数后,它会冻结,没有错误,没有警告只是冻结。
#pragma once
namespace AttarUi {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
/// <summary>
/// Summary for MainForm
/// </summary>
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// [
[STAThread]
~MainForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(154, 72);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(290, 111);
this->button1->TabIndex = 0;
this->button1->Text = L"show dialog";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
//
// MainForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(636, 264);
this->Controls->Add(this->button1);
this->Name = L"MainForm";
this->Text = L"MainForm";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Stream^ MyStream;
OpenFileDialog^ ofd1;
ofd1 = gcnew OpenFileDialog();
ofd1->InitialDirectory = "c:\\";
ofd1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
ofd1->FilterIndex = 2;
ofd1->RestoreDirectory = true;
if (ofd1->ShowDialog() == Windows::Forms::DialogResult::OK)
{
if ((MyStream = ofd1->OpenFile()) != nullptr)
{
MyStream->Close();
}
}
}
};
}
button1_Click 冻结我的应用程序。这是我的主要功能:
#include "MainForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
AttarUi::MainForm form;
Application::Run(% form);
}
我不明白我需要做什么。谁能帮帮我????
我希望 openFileDialog 出现,但没有任何反应,UI 冻结。