Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C++
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.93/5 (12 votes)
11 Oct 2017CPOL2 min read 42K   9   5
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)


Written By
Software Developer
United States United States
Software Developer

Comments and Discussions

 
QuestionCreation of the form Pin
Member 1554455022-Feb-22 11:14
Member 1554455022-Feb-22 11:14 
QuestionThanks Pin
YvesDaoust6-Feb-19 22:54
YvesDaoust6-Feb-19 22:54 
This is helpful. And shameful of MS to have hidden away the templates.

QuestionC++ and Microsoft Pin
Member 1337156613-Aug-18 3:14
Member 1337156613-Aug-18 3:14 
AnswerRe: C++ and Microsoft Pin
YvesDaoust6-Feb-19 22:49
YvesDaoust6-Feb-19 22:49 
GeneralMy vote of 5 Pin
Jay Bardeleben12-Oct-17 7:47
professionalJay Bardeleben12-Oct-17 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.