{"id":401,"date":"2010-11-11T13:31:51","date_gmt":"2010-11-11T13:31:51","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=401"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"creating-a-windows-forms-project-from-scratch","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/projects-general\/creating-a-windows-forms-project-from-scratch","title":{"rendered":"Creating A Windows Forms Project From scratch"},"content":{"rendered":"<h4>\nThis is our internal check list to create a new Windows Forms Project<br \/>\n<\/h4>\n<p>\nCreate a &lsquo;CLR&rsquo; &gt; &lsquo;Windows Forms Application&rsquo;. This selects a C++\/CLI project and will automatically set the \/clr compiler option.\n<\/p>\n<p>\nFor VS2013 and onwards&#8230;\n<\/p>\n<p style=\"margin-left: 40px;\">\nMicrosoft have decided to discourage using C++ for new windows for applications, instead preferring people to use C# or&nbsp;VB so have removed the template. &nbsp;The easy solution &nbsp;is to just create the project using VS2008 and then open it in the new Visual Studio. &nbsp;\n<\/p>\n<p>\nOnce created:-\n<\/p>\n<p style=\"padding-left: 30px;\">\nProject &gt; Properties &gt; Configuration Properties &gt; General &gt; Common Language Runtime Support\n<\/p>\n<p style=\"padding-left: 30px;\">\nSelect the compiler option (\/clr:safe is typically best, use \/clr if you want to include standard C files in the project). &nbsp;DO THIS FOR BOTH DEBUG AND RELEASE\n<\/p>\n<p style=\"padding-left: 30px;\">\nRe-name the created form to frmMain.h by right clicking it in the solution explorer.\n<\/p>\n<p style=\"padding-left: 30px;\">\nThen update the #include and Application::Run(gcnew&#8230; to frmMain in project_name.cpp\n<\/p>\n<p style=\"padding-left: 30px;\">\nRe-name the frmMain.h class from Form1 to frmMain by using a edit &gt; replace on the whole .h file\n<\/p>\n<h5>\nThings To Add To The Default Form Class<br \/>\n<\/h5>\n<p>\nAt the top of the file:\n<\/p>\n<pre>\r\n<code>#pragma once\r\n\r\n\/\/&lt;&lt;&lt; Add any includes here<\/code><\/pre>\n<h5>\nClass constructor:<br \/>\n<\/h5>\n<pre>\r\n<code>\tpublic ref class form_main : public System::Windows::Forms::Form\r\n\t{\r\n \tpublic:\r\n \t\t\/\/*********************************\r\n\t\t\/\/*********************************\r\n\t\t\/\/********** CONSTRUCTOR **********\r\n\t\t\/\/*********************************\r\n\t\t\/\/*********************************\r\n\t\tfrmMain(void)<\/code><\/pre>\n<h5>\nAdd to stdafx.h<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n#include &quot;ap-main.h&quot;\r\n\r\n\/\/Create Global Class with objects accessible from anywhere in the application\r\nref class GlobalObjects\r\n{\r\n\t\/\/public: static SomeNamespace::SomeClass ^SomeClass1;\r\n\t\/\/public: static System::String ^SomeStringName;\r\n};\r\n<\/code><\/pre>\n<h5>\nProject &gt; Add New Item &gt; Create file ap-main.cpp and start it off with:<br \/>\n<\/h5>\n<pre>\r\n<code>#include &quot;StdAfx.h&quot;\r\n#include &quot;ap-main.h&quot;\r\n\r\n\/\/**************************************\r\n\/\/**************************************\r\n\/\/********** GLOBAL VARIABLES **********\r\n\/\/**************************************\r\n\/\/**************************************\r\n\/\/(Variables should not have &#39;^&#39; - Strings cannot be created globally)\r\n\/\/DEFINE AS LOCAL (NOT EXTERN)\r\n\/\/DEFINE ALL THESE VARIABLES AS EXTERN IN AP-MAIN.H\r\n\/\/bool SomeVariableName;<\/code><\/pre>\n<h5>\nProject &gt; Add New Item &gt; Create file ap-main.h and start it off with:<br \/>\n<\/h5>\n<pre>\r\n<code>\/\/(Don&#39;t use the #pragma once preprocessor directive as this will cause the file to only be included once in the whole compilation)\r\n\/\/Use this file for all global defines for the project - its automatically included in every file by the compiler\r\n\r\n\/\/*************************************\r\n\/\/*************************************\r\n\/\/********** VERSION HISTORY **********\r\n\/\/*************************************\r\n\/\/*************************************\r\n\/\/\r\n\/\/V1.00\t##\/##\/##\t\tDeveloper Name\r\n\/\/- Original release\r\n\r\n\/\/----- NEW VERSION CHECKLIST -----\r\n\/\/- Set assembly:AssemblyVersionAttribute in the AssemblyInfo.cpp file (leave the * as the last digit so default values are used)\r\n\/\/- Also set FILEVERSION in app.rc (just leave last 2 digits as 0, 0)\r\n\r\n\/\/*******************************\r\n\/\/*******************************\r\n\/\/********** CONSTANTS **********\r\n\/\/*******************************\r\n\/\/*******************************\r\n\r\n\/\/Some examples:\r\n\/\/#define\tA_FILENAME\t\t\t&quot;users.xml&quot;\r\n\/\/#define\tA_VALUE\t\t\t\t100\r\n\r\n\/\/**************************************\r\n\/\/**************************************\r\n\/\/********** GLOBAL VARIABLES **********\r\n\/\/**************************************\r\n\/\/**************************************\r\n\/\/THESE ARE COPIES OF ALL THE VARIABLES IN AP-MAIN.CPP DEFINED AS EXTERN\r\n\/\/(Variables should not have &#39;^&#39; - Strings cannot be created globally)\r\n\/\/extern bool SomeVariableName;<\/code><\/pre>\n<h5>\nfrmMain<br \/>\n<\/h5>\n<p>\nSet the &#39;Form Border Style&#39; to Fixed Single (or as required)<br \/>\nSet &#39;MaximiseBox&#39; and &#39;MinimizeBox&#39; properties<br \/>\nSet StartPostion (typically WindowsDefaultLocation)<br \/>\nSet &#39;Text&#39;<br \/>\nSet &#39;icon&#39; (or turn off ShowIcon)\n<\/p>\n<h5>\nProjectName.cpp<br \/>\n<\/h5>\n<p>\nAdd the following try block around the main function:-\n<\/p>\n<pre>\r\n<code>\r\n\ttry\t\t\/\/Using a try block here to try and get details of any windows errors that are not properly handled\r\n\t{\r\n<span style=\"color: #808080;\">\t\t\/\/ Enabling Windows XP visual effects before any controls are created\r\n\t\tApplication::EnableVisualStyles();\r\n\t\tApplication::SetCompatibleTextRenderingDefault(false);\r\n\r\n\t\t\/\/ Create the main window and run it\r\n\t\tApplication::Run(gcnew frmMain());\r\n\t\treturn 0;<\/span>\r\n\t}\r\n\tcatch (Exception ^e)\r\n\t{\r\n\t\tMessageBox::Show(L&quot;Critical Application Error\\nPlease copy this message when reporting:\\n\\n&quot; + e, L&quot;Error&quot;, MessageBoxButtons::OK, MessageBoxIcon::Error);\r\n\t}\r\n<\/code><\/pre>\n<h5>\nProjectName.cpp<br \/>\n<\/h5>\n<p>\nIf 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&#39;t exit correctly.\n<\/p>\n<pre>\r\n<code>\r\n\t\/\/----- CHECK FOR APPLICATION ALREADY RUNNING (MULTIPLE INSTANCES ARE NOT PERMITTED) -----\r\n\tSystem::Diagnostics::Process ^CurrentProcess = System::Diagnostics::Process::GetCurrentProcess();\r\n\tarray&lt;System::Diagnostics::Process^&gt; ^CurrentProcesses = System::Diagnostics::Process::GetProcessesByName(CurrentProcess-&gt;ProcessName);\r\n\tif (CurrentProcesses-&gt;Length &gt; 1)\r\n\t{\r\n\t\tMessageBox::Show(L&quot;There is already another instance of the application running&quot;, Application::ProductName, MessageBoxButtons::OK, MessageBoxIcon::Asterisk);\r\n\t\tApplication::Exit();\r\n\t\treturn 0;\r\n\t}\r\n<\/code><\/pre>\n<h5>\nFile Version<br \/>\n<\/h5>\n<p>\nFor the .exe to have a file version you have to do this<br \/>\nSolution Explorer &gt; Resource Files &gt; app.rc. Double click it.<br \/>\nIn the resource view that opens select app.rc &gt; right click &gt; Add Resource &gt; Version &gt; New<br \/>\nThe area at the top is the main values (this is where you set file version)<br \/>\nThe area at the bottom is language specific and is where you set other values. Set the following\n<\/p>\n<p style=\"padding-left: 30px;\">\nCompany Name as required<br \/>\nFile description [blank] or give description of what ap is<br \/>\nInternal Name [blank]<br \/>\nLegal Copyright as required (used in about form)<br \/>\nOriginal filename [blank]<br \/>\nProduct Name as requried (ap name) (used in about form)\n<\/p>\n<h5>\nAssemblyInfo.cpp<br \/>\n<\/h5>\n<p>\nVC++ 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.\n<\/p>\n<p>\nSet the following assembly:: values:\n<\/p>\n<pre>\r\n<code>\r\n\t[assembly:AssemblyTitleAttribute(\"My Application Name\")];\r\n\t\/\/Product title\r\n\r\n\t[assembly:AssemblyCompanyAttribute(\"My Client Co Name\")];\r\n\t\/\/Company name-Often used when creating user direcotry for an application etc.\r\n\r\n\t[assembly:AssemblyProductAttribute(\"My Application Name\")];\r\n\t\/\/Product name-Often used when creating user directory under company name, naming message boxes, etc\r\n\r\n\t[assembly:AssemblyVersionAttribute(\"1.0.*\")];\r\n\t\/\/The * assigns 1.0.d.s, where d is days since February 1, 2000, and s is number of seconds since midnight\/2.\r\n<\/code><\/pre>\n<p>\n(See notes Application Control &gt; Assembly Attributes for full details of how to use these)\n<\/p>\n<h5>\nIcon<br \/>\n<\/h5>\n<p>\nChange the projects app.ico to the icon to be used (so its made part of the .exe file)<br \/>\nSimply delete the default app.ico file in the project directory and replace with the icon file to be used, named app.ico.\n<\/p>\n<h4>\nCompiler Options<br \/>\n<\/h4>\n<p>\n\/clr:safe &#8211; code is compiled to be verifiably safe &#8211; 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.\n<\/p>\n<p>\n\/clr:pure &#8211; 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.\n<\/p>\n<p>\n\/clr &#8211; mixed mode. You can use the .NET framework but also native C++ programs. May contain platform specific code.\n<\/p>\n<p>\n\/clr:oldSyntax &#8211; don&#39;t use its depreciated.\n<\/p>\n<p>\nNo option &#8211; your compiling native C++ in the classic way. No C++\/CLI language features available.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is our internal check list to create a new Windows Forms Project Create a &lsquo;CLR&rsquo; &gt; &lsquo;Windows Forms Application&rsquo;. This selects a C++\/CLI project and will automatically set the \/clr compiler option. For VS2013 and onwards&#8230; Microsoft have decided to discourage using C++ for new windows for applications, instead preferring people to use C# [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-projects-general"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":1420,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/401\/revisions\/1420"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}