Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

How to Create a Windows Form Project in Visual Studio Using C++/CLI

4.93/5 (12 votes)
11 Oct 2017CPOL2 min read 43.7K  
The Add New Project wizard in Visual Studio (since 2015) is missing an option for Windows Form App - only for C++. Windows Forms are still available for C# and VB.

Introduction

C++/CLI isn’t recommended for new Windows projects anymore so Microsoft makes it difficult to create a simple Windows Form application using this language.

Background

Per https://social.msdn.microsoft.com/Forums/vstudio/en-US/9f0d10d4-4f1d-44fa-99ed-da08e7f25fc7/windows-forms-application-in-visual-c-?forum=vcgeneral:

"MS removed the WinForms C++/CLI template ... The official explanation is that they want to encourage managed desktop UI development in C# or VB and relegate C++/CLI to serve as glue between native back end code and managed UI code."

Nevertheless there are still good reasons to use C++/CLI if your main skill set starts with C++: No pointers, garbage collection, STL-aware, .NET, etc.

For those of you who actually enjoy developing Windows Form applications with C++/CLI, the following steps will show you how to get started.

For our purposes, CLI is the same as CLR.

Using the Code

Open your solution > Right click on your solution > Add new project > Installed > Visual C++ > CLR > CLR Empty Project.

Give the project a name.

Right click on the new project in the Solution Explorer and select Properties.

Configuration > All Configurations

Configuration Properties > Linker > Advanced, change Entry Point to "Main".

Configuration Properties > Linker > System, change SubSystem to "Windows (/SUBSYSTEM/WINDOWS)”.

Right click on the new project > Add new item > UI > Windows Form (keeping the default name MyForm).

Modify MyForm.cpp to look like this:

C++
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;

[STAThread]
void Main(array<String^>^ args)
{
         Application::EnableVisualStyles();
         Application::SetCompatibleTextRenderingDefault(false);
         YourProjectName::MyForm form;
         Application::Run(%form);
}

Notice that when you right click on MyForm.h, there is no option for View Designer!!

Close & restart Visual Studio.

Now you can view the Designer.

Note: If you add a second form to your project & are unable to open the Designer for the new .h file, then close Visual Studio & reopen it.

Points of Interest

C# is the preferred language for new Windows projects.

But consider the huge body of legacy C++ native code out there waiting to be modified. C++/CLI should be your choice in this case.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)