C++/CLI 创建WPF程序

本文简单演示下用C++/CLI创建WPF程序,IDE为VS2015


首先创建CLR项目,选择CLR空项目:


然后,右键源文件,选择新建class,选择CLR->Component Class


接下来添加引用:

一共有三个:1.PresentationCore    2.PresentationFrameWork  3.WindowBase 很关键必须要加,这是WPF的核心库


然后MyComponent.cpp代码如下:

#include "MyComponent.h"


using namespace Project1wpf;  //引入命名空间
using namespace System::Windows;  
using namespace System::Windows::Controls;
using namespace System::Windows::Media;

[System::STAThreadAttribute]//一定要加,不然会报错??

int main(array<System::String^>^args)
{
	Application^ ap = gcnew Application();
	Window^ w = gcnew Window();
	DataGrid ^dg = gcnew DataGrid();
	dg->Background = Brushes::Yellow;
	w->Content = dg;  
	ap->Run(w);
	return 0;
}


然后,设置main函数入口,这里可能有别的方法,还需研究,目前手动设置:

选择项目:project1wpf,右键->Linker->advanced->Entry Point中输入:main


最后F5运行:可以看到内容为datagrid的window

至此,已经创建完毕WPF程序


原文地址:https://www.cnblogs.com/kevinWu7/p/10163522.html