Windows Form Applications in Visual C++/CLR 2012
How to use GUI components of Visual C++/CLR is explained at this website.
How to copy an old form is explained
at this website.
Programming
a windows form application in Visual C++/CLR (simply, .NET programming or .NET
Framework programming in Visual C++) lessens auxiliary programming and allows a
programmer to concentrate on substantial algorithms, by which you would exploit
the power of Visual C++/CLR more easily. The CLR (the Common Language Runtime)
is the Microsoft version of the CLI (the Common Language Infrastructure).
As a little
introduction to Visual C++/CLR 2012 programming, this website consists of three
parts, gHow to Create a New Windows Form Applicationh, gHow to Hide the Console Windowh and gControlling
Two (or More than Two) Formsh.
How to Create a New Windows Form
Application
Creating a new
windows form application in Visual C++/CLR consists of the following two steps:
1.
Prepare a new form.
2.
Write simple codes to present
the form.
The two steps are explained below.
1.
Prepare
a form.
Start Visual
Studio 2012, and select the menu gFILE|New Projecth(Figure 1).
Figure
1
The new project dialog box will appear (Figure
2). Select the template gVisual C++|CLR|CLR Empty Projecth.
Figure
2
If you want to change the Name, Location or
Solution name, you can set them as you like.
Click OK button, then the new project will be created (Figure 3). Select the menu gPROJECT|Add New Itemh.
Figure
3
gAdd New Itemh window will
be presented (Figure 4).
Select item gVisual C++|UI|Windows Formh and
click Add button.
Figure
4
Form designer window will
be presented (Figure 5).
Click Toolbox tab.
Figure
5
Click the lying pin (Figure 6).
Figure
6
The form designer window will be reappear
(Figure 7). Copy the Button on the form by click and drag. Double click the
button on the form.
Figure
7
Event handler button1_Click will be automatically prepared (Figure 8).
Figure
8
In Figure 9, gthis->Close();h
is inserted, so the form will be closed when the button is clicked.
Figure
9
2.
Write
simple codes to present the form.
Select the menu
gPROJECT|Add New Itemh. gAdd New Itemh window will
appear (Figure 10).
Figure
10
Choose the template gVisual C++|Code|C++ File (.cpp)h, then click
Add button. The source code file will be prepared (Figure 11).
Figure
11
Write the following codes (Figure 12):
#include "MyForm.h" // The header file of the form
using namespace Project1; // The
name of the namespace in MyForm.h
[STAThreadAttribute] // Run
this program in STA mode
int main(){
MyForm fm; // The form to be created and shown
fm.ShowDialog(); // Show
the form
return 0;
}
Figure
12
The included file gMyForm.hh,
namespace gProject1h and ref class name gMyFormh
correspond to those for the window form to be presented
(Figure 13).
Figure
13
After you finished the above work, click gBUILD|Build Solutionh menu (Figure 14).
Figure
14
If Build succeeded (Figure 15), then press
the function key F5 to run the program.
Figure
15
A console window will appear (Figure 16).
Figure
16
Then the form will appear (Figure 17).
Figure
17
How to Hide the Console Window
If you donft want the console window to appear, set the two
properties of the project, SubSystem and Entry Point,
as follows.
Click menu
gPROJECT|(project name) Propertiesh (Figure 18).
Figure
18
Property Pages window will appear (Figure
19). Select gConfiguration Properties|Linker|Systemh,
then Click cell gSubSystemh
Figure
19
A button will appear at the right end of
the cell (Figure 20).
Figure
20
Click the button, then a list of values will be presented (Figure 21).
Figure
21
Select gWindows (/SUBSYSTEM:WINDOWS)h, then push Apply button (Figure 22).
Figure
22
Next, Select gLinker|Advancedh,
then property gEntry Pointh will be shown (Figure 23).
Figure
23
As gEntry Pointh value, enter the same name
gmainh as in the source code file, then push Apply button (Figure 24).
Figure
24
Click OK button.
After the above setting of the two
properties of the project, the console window will not appear.
How to use components in Visual C++/CLR is explained at this website.
Controlling Two (or More than Two) Forms
If
you create a Windows form application on an Empty Project, several forms can be handled at the same level. For example, in the
following program, two forms are alternately presented
on clicking the buttons.
#include "MyForm.h"
#include "MyFormNoFrame.h"
#include "common.h"
using namespace TwoForms;
[STAThreadAttribute]
int main(){
MyForm mf;
MyFormNoFrame mfnf;
ck_exit = false;
do{
mf.ShowDialog();
if (ck_exit) break;
mfnf.ShowDialog();
if (ck_exit) break;
}while(true);
return 0;
}
Variable ck_exit is declared in header file common.h and
defined in code file common.cpp. The program is zipped in TwoForms.zip , which can be down
loaded by click. Unzipped files can be opened by
Visual Studio 2012.
If error messages are
shown (Figure A1), you can simply ignore them.
Figure
A1
If an error concerning a pane, on which
forms would be displayed, is shown, delete the pane
that caused the error by clicking gXh on the tab.
Then, reopen the pane by double clicking
the name in Solution Explorer. The form or the file will be
opened without troulbes.
The two forms are
designed as Figure25.
Figure
25
Properties of form MyFormNoFrame
are set as Figure 26.
Figure
26
Properties of button Next
are set as Figure 27.
Figure
27