在c++中使用.net

使用c++创建窗口程序非常不方便,可以用.net的winform api来创建ui再用c++来粘合程序。

c++项目中导入.net方法:项目上右键->公共语言运行时支持选“公共语言运行时支持(/clr)”。也可以直接创建c++的“Windows窗体应用程序项目”

如果创建的是Win32项目,那么使用.net中的类有三种方法:

1.用using namespace System,相当于c#中的using。

2.用#using <System.dll>这样的语法先把类导进来才可以用,#using是c++的.net专用语法,不带公共语言运行时的项目不能用。

3.直接在类名的前面加命名空间,如System::Windows::Forms::Application::EnableVisualStyles();

例子:

创建win32项目

#include "stdafx.h"
using namespace System;
//或者 #using <System.Windows.Forms.dll>

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

    System::Windows::Forms::MessageBox::Show("Hello, Windows Forms");
}

 在c++的.net环境下创建对象要用gcnew,而且对象指针要用^符号而不是*,其他都一样。

原文地址:https://www.cnblogs.com/wangjixianyun/p/2913173.html