Getting Started with Windows Forms using Visual C++ Tutorial – Page 2 – Holowczak.com Tutorials

Creating a new Windows Forms Project

In this brief tutorial we will create a Fahrenheit to Celsius converter using a Windows Forms Application written in C++. This tutorial assumes you have Microsoft Visual Studio installed (2008 or 2010 versions should all work – scroll down for tips on working with Visual Studio 2012, 2013 or 2015).

1) Run Visual Studio

2) Pull down the File menu and choose New Project

3) Open up the Visual C++ folder (this may be located in the “Other Languages” folder) on the left hand side. Select CLR category within Visual C++ and then highlight “Windows Forms Application” on the right side

4) Give the new application a name: FtoC

Visual Studio 2012

Visual Studio 2010

5) Click the OK button to create the new blank Form1.

At this point the new Windows Forms Application will be created and a new blank form will appear.

Adding the Forms Project Templates to Visual Studio 2012

If you do not see the Windows Forms templates in the Visual Studio 2012, you must first add them by following this note and/or this note: www.t-hart.org/vs2012/

Creating a Forms Application in Visual Studio 2013 and 2015

If you are working with Visual Studio 2013 or Visual Studio 2015, you will need to manually add a Form to a CLR application.
This link has some steps which can be summarized as:

  1. Create a new Project. Select the Visual C++ folder, then CLR and finally select CLR Empty Project
  2. Give the new application a name: FtoC and click OK.
  3. Pull down the Project menu and select Add New Item. Select the UI folder on the left and then select Windows Form on the right. Name the form Form1.h and click the Add button.
  4. In the Solution Explorer, right-click on the Form1.cpp file and select View Code.
  5. Add the following code to the Form1.cpp file and save it:
    #include "Form1.h"
    
    using namespace FtoC;
    using namespace System;
    using namespace System::Windows::Forms;
    
    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
    	// Enabling Windows XP visual effects before any controls are created
    	Application::EnableVisualStyles();
    	Application::SetCompatibleTextRenderingDefault(false); 
    
    	// Create the main window and run it
    	Application::Run(gcnew Form1());
    	return 0;
    }
    
    

    Note: If you have created your project under a different name, you will need to change the the namespace to match the main namespace of your project. For example if you just created a project named MyCalculator, change the line:

    using namespace FtoC;
    
    

    so that it matches the name of the namespace:

    using namespace MyCalculator;
    
    

    In addition, if you gave the new form a different name (such as MyForm) you will need to change all references in the rest of this tutorial from Form1 to MyForm.

  6. Pull down the Project menu and select Properties. Open up the Configuration Properties and then Linker and then System.
    Change the SubSystem property to Windows (/SUBSYSTEM:WINDOWS).
  7. Open up the Configuration Properties and then Linker and then Advanced.
    Set the Entry Point property to main (just type in main). This should match the main function specified in the code above in Form1.cpp.
  8. Save all of the files and the Project and now you are ready to continue with the rest of the tutorial.

Reminder: If you added the form named “MyForm” (instead of Form1) you will need to adjust the rest of the instructions in this tutorial.