由于 Visual Studio 2012 确实支持 C++/CLI Winform 应用程序一目了然并不明显,即使 OP 肯定知道这一点,对于其他读者来说,我创建一个只是为了回答这个问题:
在 Visual Studio 2012 的“新建项目”对话框中,我选择了 [Visual C++ > CLR > CLR 空项目]。
在新项目中,我添加了一个普通的 C++ main 函数和一个 Windows 窗体。
在链接器设置中,我将其从控制台子系统更改为 GUI 子系统(并且由于 Microsoft 链接器的非标准行为,将入口点调整为 mainCRTStartup)。
也就是说,下面显示的是生成的 Winform 标头,其中仅包含手动添加的事件处理程序代码,该代码会触发打开文件对话框。
#pragma once
namespace CppWindowsFormsApplication {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MainWindow
/// </summary>
public ref class MainWindow : public System::Windows::Forms::Form
{
public:
MainWindow(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainWindow()
{
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(149, 208);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"&Open file";
this->button1->UseVisualStyleBackColor = true;
// ---------------------------------------------------- !Adds event handler -------------------
this->button1->Click += gcnew System::EventHandler(this, &MainWindow::button1_Click);
//
// MainWindow
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->button1);
this->Name = L"MainWindow";
this->Text = L"MainWindow";
this->ResumeLayout(false);
}
#pragma endregion
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
OpenFileDialog^ sfd = gcnew OpenFileDialog();
sfd->Filter = "Text Files|*.txt|All Files|*.*";
if( sfd->ShowDialog() != System::Windows::Forms::DialogResult::OK )
{
return;
}
//MessageBox::Show( sfd->FileName );
MessageBox::Show( "OK" );
}
};
}
我没有修复缩进等,因为我没有时间;几乎所有这些代码都是由 Visual Studio 2012 的 RAD (Rabid Application Development) 功能生成的。
与此问题的历史相关,现已删除:
我真的没有时间回答这个问题。我宁愿让其他人回答它(并从中获得一些声誉)。这是一个如此简单的问题,我敢肯定,如果不是这么快就结束了,任何数量的读者,比我有更多的时间,都会回答它。
但是回答它,将代码发布在其他地方,并放弃一些其他事项,是我看到的让它重新打开的唯一方法。
我真诚地请求每一位读者请不要投票关闭您不了解的内容。
只有在您彻底了解问题并且能够向自己和其他人证明关闭问题后社区会变得更好时,才能投票结束问题。
无法理解一个问题不一定意味着这个问题不好。