This is our internal check list to create a new Windows Forms Project

Create a ‘CLR’ > ‘Windows Forms Application’. This selects a C++/CLI project and will automatically set the /clr compiler option.

For VS2013 and onwards…

Microsoft have decided to discourage using C++ for new windows for applications, instead preferring people to use C# or VB so have removed the template.  The easy solution  is to just create the project using VS2008 and then open it in the new Visual Studio.  

Once created:-

Project > Properties > Configuration Properties > General > Common Language Runtime Support

Select the compiler option (/clr:safe is typically best, use /clr if you want to include standard C files in the project).  DO THIS FOR BOTH DEBUG AND RELEASE

Re-name the created form to frmMain.h by right clicking it in the solution explorer.

Then update the #include and Application::Run(gcnew… to frmMain in project_name.cpp

Re-name the frmMain.h class from Form1 to frmMain by using a edit > replace on the whole .h file

Things To Add To The Default Form Class

At the top of the file:

#pragma once

//<<< Add any includes here
Class constructor:
	public ref class form_main : public System::Windows::Forms::Form
		//********** CONSTRUCTOR **********
Add to stdafx.h

#include "ap-main.h"

//Create Global Class with objects accessible from anywhere in the application
ref class GlobalObjects
	//public: static SomeNamespace::SomeClass ^SomeClass1;
	//public: static System::String ^SomeStringName;
Project > Add New Item > Create file ap-main.cpp and start it off with:
#include "StdAfx.h"
#include "ap-main.h"

//********** GLOBAL VARIABLES **********
//(Variables should not have '^' - Strings cannot be created globally)
//bool SomeVariableName;
Project > Add New Item > Create file ap-main.h and start it off with:
//(Don't use the #pragma once preprocessor directive as this will cause the file to only be included once in the whole compilation)
//Use this file for all global defines for the project - its automatically included in every file by the compiler

//********** VERSION HISTORY **********
//V1.00	##/##/##		Developer Name
//- Original release

//- Set assembly:AssemblyVersionAttribute in the AssemblyInfo.cpp file (leave the * as the last digit so default values are used)
//- Also set FILEVERSION in app.rc (just leave last 2 digits as 0, 0)

//********** CONSTANTS **********

//Some examples:
//#define	A_FILENAME			"users.xml"
//#define	A_VALUE				100

//********** GLOBAL VARIABLES **********
//(Variables should not have '^' - Strings cannot be created globally)
//extern bool SomeVariableName;

Set the 'Form Border Style' to Fixed Single (or as required)
Set 'MaximiseBox' and 'MinimizeBox' properties
Set StartPostion (typically WindowsDefaultLocation)
Set 'Text'
Set 'icon' (or turn off ShowIcon)


Add the following try block around the main function:-

	try		//Using a try block here to try and get details of any windows errors that are not properly handled
		// Enabling Windows XP visual effects before any controls are created

		// Create the main window and run it
		Application::Run(gcnew frmMain());
		return 0;
	catch (Exception ^e)
		MessageBox::Show(L"Critical Application Error\nPlease copy this message when reporting:\n\n" + e, L"Error", MessageBoxButtons::OK, MessageBoxIcon::Error);

If you want to prevent multiple instances of the application from running then add this before the Application::Run(gcnew frmMain());. You need to do this here and not in the main form constructor as the application won't exit correctly.

	System::Diagnostics::Process ^CurrentProcess = System::Diagnostics::Process::GetCurrentProcess();
	array<System::Diagnostics::Process^> ^CurrentProcesses = System::Diagnostics::Process::GetProcessesByName(CurrentProcess->ProcessName);
	if (CurrentProcesses->Length > 1)
		MessageBox::Show(L"There is already another instance of the application running", Application::ProductName, MessageBoxButtons::OK, MessageBoxIcon::Asterisk);
		return 0;
File Version

For the .exe to have a file version you have to do this
Solution Explorer > Resource Files > app.rc. Double click it.
In the resource view that opens select app.rc > right click > Add Resource > Version > New
The area at the top is the main values (this is where you set file version)
The area at the bottom is language specific and is where you set other values. Set the following

Company Name as required
File description [blank] or give description of what ap is
Internal Name [blank]
Legal Copyright as required (used in about form)
Original filename [blank]
Product Name as requried (ap name) (used in about form)


VC++ also has the same attributes in assemblyInfo.cpp. Bloody stupid, but to do with managed and unmanaged things. The info in the app.rc above will be used as part of the file properties. But the info in the assemblyInfo.cpp is used by the installer and may also be picked up by other things, so we have to setup both.

Set the following assembly:: values:

	[assembly:AssemblyTitleAttribute("My Application Name")];
	//Product title

	[assembly:AssemblyCompanyAttribute("My Client Co Name")];
	//Company name-Often used when creating user direcotry for an application etc.

	[assembly:AssemblyProductAttribute("My Application Name")];
	//Product name-Often used when creating user directory under company name, naming message boxes, etc

	//The * assigns 1.0.d.s, where d is days since February 1, 2000, and s is number of seconds since midnight/2.

(See notes Application Control > Assembly Attributes for full details of how to use these)


Change the projects app.ico to the icon to be used (so its made part of the .exe file)
Simply delete the default app.ico file in the project directory and replace with the icon file to be used, named app.ico.

Compiler Options

/clr:safe – code is compiled to be verifiably safe – a maximally safe type. Required when your assmelby will run in very restrictive envonments etc (e.g. web broser). THIS IS THE OPTION I SHOULD USE.

/clr:pure – Uses IL instructions only, not machine instructions but is not verifiably safe. E.g. it may use pointers or other features that could produce buffer overruns, access violations and other memory corruption.

/clr – mixed mode. You can use the .NET framework but also native C++ programs. May contain platform specific code.

/clr:oldSyntax – don't use its depreciated.

No option – your compiling native C++ in the classic way. No C++/CLI language features available.

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.


Your email address will not be published. Required fields are marked *