C++/CLI 创建WinForm程序

本文演示下用CLR创建一个简单的winform程序,IDE:VS2015

可以参考另一篇文章:http://blog.csdn.net/wcc27857285/article/details/78135314

第一步:


第二步:在头文件文件夹中新增class,选择windows Form


然后查看右侧引用,你会发现多了很多winform专用的dll,VS自动帮我们添加了这些引用

接下来,打开MyForm.cpp,输入代码如下:

#include "MyForm.h"

using namespace ClrWinForm;


int main(array<System::String^>^args)
{
	Application::EnableVisualStyles();
	MyForm^ form = gcnew MyForm();
	Application::Run(form);
	return 0;
}


至此F5运行,可以看到熟悉的winform界面:


然后,双击右侧MyForm.h,可以看到设计器,然后可以看到左侧的toolbox里面有winform熟悉的各种控件,又可以拖控件了!

在窗体上右键查看View code,可以看到代码如下:

#pragma once

namespace ClrWinForm {

	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 MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}

	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->components = gcnew System::ComponentModel::Container();
			this->Size = System::Drawing::Size(300,300);
			this->Text = L"MyForm";
			this->Padding = System::Windows::Forms::Padding(0);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		}
#pragma endregion
	};
}
这是VS自动生成的代码,和传统的winform的desinger.cs中的代码很相似,但是这里使用C++/CLI写的





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