Last Updated:

Windows Forms Using C++ / in VisualStudio

WindowsForms is a necessary component if you plan to create your own program, where you need a user interface. This is so that the user can easily use the C++ application without having to work directly with the command line. Moreover, the program will look more presentable if you apply WindowsForms. What it is and how it works will be described in the article.

But first, it is necessary to clarify that WindowsForms is necessary in order to make a classic interface. No special design elements can be added. In general, if you add WindowsForms to your C++ program, it will look very simple, but you will not need to enter certain commands manually each time.

We need this component from Microsoft to facilitate the use of the program. You only need to select one or another element in the interface and click on it, as a rule. Sometimes, when you need a specific value that is not specified in the database, you need to enter text in the field. But often, in the interface of the program there are only buttons - controls.

To create a new C++ project in VisualStudio


The first thing you need to do to create your program with a high-quality interface is to prepare a C++ project in VisualStudio. You should start with the most basic. Directly, with the creation of a new project in C++ in the IDE. To do this, you need to run the program, and then select the "Create a project" item (this is how it is implemented in the 2019 version, in others there may be a different algorithm).

Next, a window will appear before the user where you need to specify all the parameters of the new project. Here you need to specify "Project Type", as well as write "WindowsForms" in order to further indicate in VisualStudio that a component from Microsoft will be used for the interface and that the program will not become a console version. Also, you need to designate a template on this screen, which is called "WindowsForms Application (. NETFramework). After such manipulations, it remains only to click on the "Next" button at the bottom.

The third and final step in creating a C++ project is to specify the name of the program. In the field "Set up a new project" and "Project name" you need to specify "Hello, World!" (for example). After that, it remains only to create the development itself. And then there is work on the code in C++, as well as a link with WindowsForms. However, this will be discussed later. The project has already been created, the main part of the work remains - writing code.


To add WindowsForms to a C++ project


If you create a C++ project without WindowsForms, you can fix this situation. That is, the user only needs to have a C++ project with him without any additional elements. First you need to open one, after which a person will have the same development on the screen. However, it is not needed, only the Solution Explorer window is required.

Having found your project, you need to right-click on its name to open the context menu, where the "Add" item is selected, hovering over which the cursor will appear, the "Create Item" action will appear. Just by clicking on this, the user will have the option to add WindowsForms. And if you want to do it quickly, then just press the keyboard shortcut - Ctrl + Shift + A.

In the new window that opens, you will see what you wanted. You can add any element, but in this case you need a "WindowsForms Form", which is adapted to the C++ language. This is exactly what is chosen by the user, if a graphical interface for the program is needed. Also, you can change the name of the form to indicate the window in a different way.

After the form is added, you need to make some changes to the "MyForm.cpp" file (or the name will be different if the user renamed the form) with the source code.

There you need to specify the following set of lines:

using namespace System;
using namespace System::Windows::Forms;

void main(array<String^>^ arg) {

WinFormsTest::MyForm form; WinFormsTest is the name of your Application::Run(%form) project


When you enter this code, you need to specify the name of your project, because its name will be different from "WinFormsTest".

Next, after you have made changes to the file, you want to save the file. Further steps will be taken in VisualStudio. By right-clicking on the name of your project, you need to select the "Properties" item, and then go to the menu "Linker" - "System" - "Subsystem". There you need to specify "Windows" and apply the changes.

Also, additional changes are required. Without leaving the "Linker"-"Advanced"-"Entry Point". In the field you need to specify "main". After such manipulation, again, you need to apply the new values and exit this window.

This is where the addition of WindowsForm ends. Now you can write your program using a graphical interface for easier use.


Create forms and controls


In general, creating controls in WindowsForms is a simple matter. Here you just need to work on the location of the buttons, since the form is already there. After running the form, a toolbar will appear on the side, where you need to select the "Toolbox", or press the combination Ctrl+Alt+X. For greater convenience, it is recommended to pin the window that appears.

Next, by selecting the Button object, you can position it on a form and adjust its dimensions. And going to the section with its properties, it is possible to change the text, for example, to "ClickThis" (tab "Appearance"), as well as set special parameters, such as the name of the button from "Button1" to "btnClickThis" (tab "Design").

After that, the most standard control will be on the form. You can do the same with other objects. The main thing is to specify the correct parameters for them. And then working with a program written in WindowsForms with the participation of the C++ language will be more comfortable, because the graphical interface will be configured.