How to take a console based program and change
it to program with a GUI interface
1) Design the interface (form) first! This is very important!. Visual C++ will generate a
lot of files for you.
2) Place the text boxes, labels, and buttons you
intend to use. Make sure you can build
your program without getting errors, even though it only contains the code for
the interface.
3) Now comes the steps
you need to make your program do something.
You will be using your original cpp and h files EXCEPT for your
main program file (which
will be replaced by files generated as you design your
interface).
Step 3.1 Find the
directory where the files already generated are being saved – files with names like
stdafx.h, stdafx.cpp, Form1.h.
You will also see a file there which is called project name.cpp (using whatever project name you gave to your project).
Copy your original .cpp and .h files to the same directory
Step 3.2 Here’s how to
add the .h files to your project:
Open the stdahx.h which is already a
member of your project.
Find the //TODO comment.
That is where you add
#include
“your own h file”
for
each of your own .h files
Step 3.3 Add your
own cpp files to the project – but leave out the file containing your main
program. Add the following line of code
to the beginning of each of your own .cpp files.
#include
“stdafx.h”
Step 3.4 Open the
Form1.h file – this is where you will
add code to declare your variables, initialize your variables, and call various
actions when buttons are clicked.
public ref class Form1 :
public System::Windows::Forms::Form
Near the top of this file you will find
some code which reads
Immediately BEFORE this line is where you add all the
declarations of variables which were originally declared in your
main program (that is, the variables which are not local to some function).
Find the //TODO comment which shows up in this part of
the code which is already written for you.
public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here //
HERE you can assign initial values and call any functions which
you might need to initialize things – like reading in data from input files.
Now it’s time to find where to put
actions to be carried out when a user types in values, clicks buttons, etc.
Find the empty functions supplied to provide a place for these
actions. For example, find
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { };
You will be adding code to these functions to do something when
button1 is clicked.
Continue adding code for all of your buttons. When you are finished
your program should do everything you want it to do.
Note: I suggest you try
compiling your program as you complete each step and not wait until the
end. In this way, you might catch simple
errors early.