How to Create a Windows Forms Application in C++ and .NET using Visual Studio


Creating a New Project

Congrats on installing Microsoft Visual Studio! Let’s get started with the project creation step.

Video Guide : 



So, after you installed and configured Visual Studio, you can go to File->New->Project.

From there choose Visual C++. And CLR.




Next Choose CLR Empty Project and give it a name and Click OK to create the Project.



Now Visual Studio will create an Empty Project for you, which you can later on add more elements and create a Unique Application!
Next go to Project->Add New Item.

From the Dialog, choose UI->Windows Form.

Now Click Add.

Now you may get an error like this.

If so, close the MyForm.h file.

Now go to View->Solution Explorer.

Now Open MyForm.cpp by double clicking from the Solution Explorer.

Now paste the following Code. Now you may start to panic saying that you don’t want to copy and paste code. But the truth is this is just template code which is supposed to be added by Visual Studio. But unfortunately, Visual Studio doesn’t have the template from Visual Studio 2012 and onwards. Therefore, we need to create on our own. So, don’t panic. Follow me and I’ll explain everything when needed.

#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void main(array<String^>^args) {
Application::SetCompatibleTextRenderingDefault(false);
Application::EnableVisualStyles();
   MyFirstGUI::MyForm form;
   Application::Run(%form);
}



Now save and close it.
Now go back to Solution Explorer and Open MyForm.h.

And Last, go to Project->MyFirstGUI Properties.

Next go to Linker->System->SubSystem and choose Windows from the dropdown.

The go to Advanced and type main in the Entry Point.

Now click OK.
Now click on Local Windows Debugger Button to run your application!

Your application will be compiled and then the Application will be launched.


Congratulations on creating your First GUI App in C++.
Stay tuned, we will meet with the next tutorial. In the next tutorial, we will discuss how you can add text to the Application!
Also stay tuned for a detailed explanation on the things we did today!

No comments:

Post a Comment