Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++ cli

作者:娚人ゝ無須赫赫有名   发布日期:2025-04-18   浏览:130

// 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;
}

解释说明:

  1. 命名空间和引用

    • #using <System.dll>#using <System.Windows.Forms.dll> 引用了 .NET Framework 中的系统库。
    • using namespace System;using namespace System::Windows::Forms; 引入了常用的命名空间。
  2. 类定义

    • public ref class MyForm : public Form 定义了一个继承自 Form 的托管类 MyForm,用于创建一个 Windows 窗体。
  3. 构造函数

    • MyForm() 构造函数中设置了窗体的标题,并创建了一个按钮 myButton,设置了按钮的位置和点击事件处理程序。
  4. 事件处理

    • void OnButtonClick(Object^ sender, EventArgs^ e) 是按钮点击事件的处理方法,当按钮被点击时会弹出一个消息框。
  5. 主函数

    • [STAThread] 属性确保线程以单线程单元 (STA) 模式运行,这是 Windows Forms 应用程序所必需的。
    • Application::Run(gcnew MyForm()); 启动应用程序并显示窗体。

上一篇:c++ 树

下一篇:c++ gcd函数

大家都在看

c++闭包

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++ 获取系统时间

c++进制转换函数

c++ tcp

c++ gcd函数

c++ cli

c++ 树

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站