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






4.93/5 (12 votes)
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
"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:
#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.