Hey, welcome!
O.K. lets start. Yes, lets start MS VC++ 5or 6!
Now you should see the folowing on your screen:
Thats the "developing-enviroment". Cool, huh?
Now I will explain you how to work with this IDE with an
example.
We will code the higher-lower-game, you know it: the computer
guesses a number and you have to think of that number and the
computer only says: "your guessed number was
higher/lower"....blablabla...
Now, you will see 4 tabs on the left hand side: "ClassView", "ResourceView", "FileView" and "Info-(guess what)View". We will work only with "ResourceView", so select it.
>Open the "dialog" -
Folder, you will see a node with the name: "IDD_GUESS_DIALOG".
This is the dialog created for you, yes only for you (you
actually have paid enough for this proggie, so it can do a few
things for you ;-)!
>Double click this node and you will see the dialog which will
be the main dialog, the only, holy one dialog the dialog of
freedom and peace and love all over the world! No! this was only
a joke ;-).
>Play a little bit with this dialog, stretch em and move the
buttons around, but DONT DELETE them! If you think, your dialog
should be tested, click on the little gray trigger on the
left-hand-side below the tabs and you will see, how your dialog
looks like, if it is shown by your proggie.
>Now, right click the dialog and select the "properties"-option. Play a little bit with this options (and
the options of the buttons too) to see, how the layout changes,
but DONT CHANGE their ID!
Now it is time to take a look in the project-dir (this directory in which the source files are). You will see, that there are ALOT of files, they are too only created for you and your program, so DONT CHANGE them manually! This files are all needed for your programm!
Before, I said that you must not change the ID of the dialog. I will explain a little bit about ID's. Each dialog and dialog-object and each other resource (things, which are added, but not the source-code. The icon is a resource too!) has a unique number which is represented by the ID. Look into the "Resource.h" file and you will see the following code:
//{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by GUESS.RC // #define IDR_MAINFRAME 128 #define IDD_GUESS_DIALOG 102
This is the best time to start with the program! Design a dialog, that looks like this (delete the OK-Button and the static-caption with the "todo..."-text):
A short description to each number:
Now I will explain what we need for the programm:
What we will do first:
>Init all the procedures and variables. We have to make
"member variables". These variables are the data-store
for our edit-field and the "Wrong
tries:"-field. In the meber variable of the
data-field, the guessed number will be stored.
You have to link a editable control with a variable. This
variable is called "member variable".
Select the "IDC_WRONG_CAPTION" and link it with the variable "m_wrong_caption". This time you have to change the "Variable type" to "CString".
You linked one control with a "CString" variable. Click on "help->Search..." and enter "CString" to learn more about these variables. DO IT NOW!!!
Finished reading something about CString variables? They're cool, huh?
At this point I can say, that it is almost done, we have to say what routines should be called if the user presses a button and code the game!
Lets specify the "new game"- routine:
You now should see this:
The red arrow points to our function.
Before we can add some code we have to specify global variables.
>scroll to the top of the file and add the following
code(after the last "#endif"):
int
guessed_computer; //this is the number
"guessed" by the computer
int wrong_tries; //number of wrong tries
>Now we have to add a routine which sets the wrong guesses to zero and displays this.
//set the wrong
guesses to zero
wrong_tries = 0; //sets the variable to zero
UpdateData( TRUE ); //specifies that all member-variables
//have to take their value from their
//controls
m_wrong_caption = "Wrong tries: 0";
UpdateData( FALSE );//specifies that all controls have to
//take their value from their member-
//variables.
This routine is ready!
Dont forget to save often!
Now we have to make the guess-routine!
Lets go on...
Now, add a "member function"
called "OnGuess" to the button "Guess" (this button with the ID "IDC_GUESS_BUTTON").
Here is the solution, if cant do it yourself:
Yeah! we have to write the last two
routines:
*Check if the right number.
*if not, increment counter and display message.
*if yes, show a "congratulation - message-box".
Here the simple check:
And here the routine if the two numbers are equal:
If you aren't understanding some functions, look at their definition (you can select any expression and press F1 to get help!)
Here is the routine for the false guess:
This was my little tutorial, hope you enjoyed it!
Write me if you have any suggestions or questions.
Donnations please to Bill Gates (he will need it, if the lawyers
are finished with him hehehe ;-).