How to Create a C++ GUI Application Using Visual Studio_ _ Simplilearn

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio?

| Simplilearn

All Courses

Software Development

Tutorials Articles Ebooks Free Practice Tests On-demand Webinars Live Webinars

Home Resources Software Development C++ Tutorial for Beginner The Best Guide To Create The C++
GUI Application Using Libraries

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 1/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

The Best Guide To Create The C++ GUI Application Using Libraries

Lesson 14 of 24 By Ravikiran A S

Last updated on Apr 25, 2023 500075

Previous Next

Tutorial Playlist

Table of Contents

How to Create a Project and Configure a Visual Studio to Run a C++ GUI Application?

Creating Windows Form Application

C++ GUI Calculator Application

Conclusion

To develop C++ GUI or C++ graphical user interface application, you need an IDE that supports the
C++ GUI application. To create the GUI app, you must use Visual Studio 2019 because it is better
suited for the C++ GUI application. In this tutorial, you will learn how to create a C++ GUI application
in a detailed manner.

How to Create a Project and Configure a Visual Studio to Run a C++ GUI Application?
https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 2/17
7/5/24, 4:16 AM
j g
How to Create a C++ GUI Application Using Visual Studio? | Simplilearn
pp

So, start with creating a project in Visual studio. After opening the visual studio application, you
need to select the development settings to Visual C++ and choose the color theme.

Once you are done with that, you need to click on this Create a new project.

After this, you are going to add a project template, so you must search for CLR Empty
Project(.NetFramework). This framework provides information sharing between the .Net and C++
code.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 3/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

In the next step, you can write the project's name and select the project's location.

Once you are done with this, you can click on create, and the project will be created.

Learn From The Best Mentors in the Industry!

Automation Testing Masters Program

EXPLORE PROGRAM

Creating Windows Form Application

As the project has been created, now you will build a windows form application and for that, you
must create a form file. Click on this Project icon in the upper bar and select Add New item.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 4/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

After this step, select UI under Visual C++, click on the Windows form, and press ‘add’ to open the
form file.

Now, you will do some additional configurations. Again you must click on Project -> Properties, it will
open the configuration properties, and select Linker from there, and from the drop-down, l click on
System. On clicking the system the subsystem option will appear, and you have to select that from
the drop-down because the Subsystem tells the operating system how to run the .exe file. You will
have to select Windows from the drop-down bar.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 5/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

After this step, you have to select the entry point. So under configuration settings, choose Advanced
and write Entry point as main and click on ok. The entry point means the starting address of .exe file.

Once the configurations are done, click on the Myform.cpp file from the left bar and add the code in
this file.

Code:

#include "MyForm.h"

using namespace System;

using namespace System::Windows::Forms;

[STAThread]

void main(array<String^>^ args)

Application::EnableVisualStyles();

Application::SetCompatibleTextRenderingDefault(false);

MyProject::MyForm form;

Application::Run(% form);
https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 6/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

You can paste the above code in the .cpp file. This code shows how the program will execute;
MyProject is the project's name; you have to change it according to your project name. In this case,
the name of the project is Project2. Similarly, MyForm is the name of the form file; you can change it
as well according to the name of your form file and save it.

Now, click on Myform.h, and you will notice the coding part is there, but there is no form. So, you
must save it and reopen the visual studio after closing it.

After opening the visual studio, you must click on Myform.h, and the windows form will appear as
shown in the figure below.

Now, you can check the form by running it, so you can click on the Local Windows debugger button,
and if it is running fine, you must proceed to add some buttons to the windows form.

Now, go ahead to learn how to create the C++ GUI application.

Preparing Your Blockchain Career for 2024


https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 7/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn
Preparing Your Blockchain Career for 2024

Free Webinar | 5 Dec, Tuesday | 9 PM IST

REGISTER NOW

C++ GUI Calculator Application

As you have successfully created the windows form, now you will design the calculator application
and for that, you need to add some buttons and textboxes on the form. To add textbox, click on the
toolbox on the right-hand sidebar and search for textbox. To add the textbox in the form, click on the
textbox and release it over the form.

Once it is added, you can adjust the size of the textbox by clicking on the Properties -> Fonts and
adjust it according to your requirements.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 8/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

Similarly, you can create another textbox and adjust its size and font. You must create two textboxes
because you want two numbers or digits to compute. You can either create the textbox again or
copy the first one and paste it into the form.

Then, you can add the label in between both the textboxes as an operator. To add the label, you can
again go to the toolbox and add the label from there, adjust it from fonts, and write the text inside it
from the text option inside the properties.

After adjusting, the form will look like as shown in the figure.

Now, you need some buttons to add to this calculator like addition, multiplication, etc. To add the
button click on the toolbox and add buttons from there Unlike textboxes you can simply resize the
https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 9/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn
button, click on the toolbox and add buttons from there. Unlike textboxes, you can simply resize the
button by clicking on it and adjusting it manually. But to change the text, you have to go to properties
-> text and write down the text, like in this case, you must write the symbol +.

Similarly, you will need to add three more buttons for subtraction, multiplication, and division.

You can also change the color of the text in properties. To display the result, add a label to the form
where the calculated result will be displayed, and name it as Result.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 10/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

As the layout of the calculator is done, so you will now add the functionality to it.

To add the functionality to the buttons, you need to know the actual names of these buttons or
labels. The names can be found out by clicking on the button or label and then clicking on the
properties, so the name would be displayed on top of properties. For example, in the below figure,
the textbox name is shown. Similarly, the buttons’ names are button1, button2, button3, button4.

Now double-click on the add button(+) to add the functionality.

After double-clicking on the add button, it will open the Myform.h file, and you must create the event
handler for this add button whose actual name is button1.

You need to do the addition by clicking this (+) button, so let’s write the following code.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 11/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

Here, you have declared the variable output which will store the result. The textBox1 is the name of
the textbox and the arrow operator gives access to the members of the object. The arrow operator is
pointing to Text which means you have to change the text of textBox1. Similarly, you must do it for
textBox2 and add both of them.

You know that whatever you write inside the textBox will be a text or can be a string, and you can’t
add the strings. So, you need to convert those strings into integers. System::Convert::ToInt16 will
convert the string to an integer.

To display the text, write the actual name of the Result label, i.e., label2. Here, you are again using
the arrow operator pointing to the text because you want to change the text and print the output.

Become a Full Stack Developer in Just 6 Months!

Full Stack Java Developer

EXPLORE PROGRAM

To print the result or output, convert the integer to string using System::Convert::ToString(output)
because you have already done the computation.

You can also check if the code is working fine by clicking on the Local Windows debugger button
and adding the values in the form.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 12/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

You have added the functionality for the add(+) button. Similarly, you must double click on the
subtraction button and do the same for it as well but only the signs would be different. Similarly, for
all the buttons the same actions must be done.

Similarly, you are going to do it for the multiplication and division buttons.

Once you are done with this, you can check if it is working fine by running it and then doing the
addition, subtraction, multiplication, division in the calculator.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 13/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

If all the operations are working fine, then it means the C++ GUI calculator application is created.

Conclusion

After reading this tutorial on C++ GUI, you would have understood how to create a project and
configure visual studio to run C++ GUI application and the Creation of windows form application.
You will also learn how to create a C++ GUI application.

If you are looking to build a career in software development, check the Post Graduate Program in
Full Stack Development by Simplilearn. It can prove to be the ideal solution to help you build your
career in the right way.

Do you have any questions regarding this tutorial on C++ GUI? If you do, then put them in the
comments section. We'll help you solve your queries. To learn more about C++GUI, click on the
following link: C++ GUI.

Happy learning!

About the Author

Ravikiran A S

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 14/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the
hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data,
and powerful Big Data Frameworks l…

View More

Recommended Programs

Full Stack Java Developer Lifetime


Access*
784 Learners

Full Stack Web Developer - MEAN Stack Lifetime


Access*
922 Learners

Automation Testing Masters Program Lifetime


Access*
1494 Learners

*Lifetime access to high-quality, self-paced e-learning content.

Explore Category

Recommended Resources

Implementing Stacks in Data Blockchain Career Gu


https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 15/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn
p g
Structures Comprehensiv…

2 Comments 
1 Login

G Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

 Share Best Newest Oldest

Per Sjoborg − ⚑
2 years ago

Thanks for a great article. I managed to use it as a way to get started in C++/Visual Studio with no
prior experience in either.
I have a few notes:
1. There are two nearly identical types of projects to create. The only difference I can see is that
one says "Library" and the other "Console". I do not know if there is a difference if there are, I
would point this out specifically.

2. I would add another screen capture when dealing with the setting of the main function. "...you
have to select the entry point. So under configuration settings, choose Advanced and write Entry
point as main and click on ok.". The screen captures where very helpful and this extra one might
be.

3. This one is very nit-picky :-)

"MyProject is the project's name; you have to change it according to your project name"

Maybe change "MyProject" to CHANGETHISTOYOURPROJECTSNAME -)


Thanks again and I will use the playlist to learn more.

0 0 Reply ⥅

Team Simplilearn Mod > Per Sjoborg − ⚑


2 years ago

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 16/17
7/5/24, 4:16 AM How to Create a C++ GUI Application Using Visual Studio? | Simplilearn

Thanks for your feedback, Per!

1 0 Reply ⥅

© 2009 -2024- Simplilearn Solutions.

Disclaimer
PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.

https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-gui 17/17

You might also like