// C++/CLI 示例代码:创建一个简单的 Windows 窗体应用程序
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
public ref class MyForm : public Form
{
public:
MyForm()
{
this->Text = "C++/CLI Windows Forms Example";
Button^ myButton = gcnew Button();
myButton->Text = "Click Me!";
myButton->Location = System::Drawing::Point(50, 50);
myButton->Click += gcnew EventHandler(this, &MyForm::OnButtonClick);
this->Controls->Add(myButton);
}
private:
void OnButtonClick(Object^ sender, EventArgs^ e)
{
MessageBox::Show("Button clicked!");
}
};
[STAThread]
int main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew MyForm());
return 0;
}
命名空间和引用:
#using <System.dll>
和 #using <System.Windows.Forms.dll>
引用了 .NET Framework 中的系统库。using namespace System;
和 using namespace System::Windows::Forms;
引入了常用的命名空间。类定义:
public ref class MyForm : public Form
定义了一个继承自 Form
的托管类 MyForm
,用于创建一个 Windows 窗体。构造函数:
MyForm()
构造函数中设置了窗体的标题,并创建了一个按钮 myButton
,设置了按钮的位置和点击事件处理程序。事件处理:
void OnButtonClick(Object^ sender, EventArgs^ e)
是按钮点击事件的处理方法,当按钮被点击时会弹出一个消息框。主函数:
[STAThread]
属性确保线程以单线程单元 (STA) 模式运行,这是 Windows Forms 应用程序所必需的。Application::Run(gcnew MyForm());
启动应用程序并显示窗体。上一篇:c++ 树
下一篇:c++ gcd函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站