Skip to main content

Posts

Showing posts from 2011

Decomposing makes everything easier...

Time for us to start writing computer games! This first one will be a bit simple, but it's a start. Our task is to write a program that implements a guessing game. Our program generates a random number between 0 and 100. The player must guess the secret number. The program provides hints like "that's too high" or "that's too low" until the player finally guesses the secret number. We will work on this game in three steps.  Figure out how to generate a random number within a given range of values. Create a main function that processes one guess from the player, and provides hints. Add what we need to allow for multiple guesses until the player guesses the number. This process of development is called decomposition, which means breaking a task into sub-tasks, each of which is easy to do. Starting with step 1, do a Google search to see how to generate a random number using C++. Try searching on "rand C++". // random.cpp.  Maggie Johnson //...

Get some input

// get_input.cpp: Maggie Johnson // Description: Illustrate the use of cin to get input. #include <iostream> using namespace std; int main() {   int input_var = 0; // Enter the do while loop and stay there until either // a non-numeric is entered, or -1 is entered. Note that // cin will accept any integer, 4, 40, 400, etc.   do   {     cout << "Enter a number (-1 = quit): "; // The following line accepts input from the keyboard into // variable input_var. // cin returns false if an input operation fails, that is, if // something other than an int (the type of input_var) is entered.     if (!(cin >> input_var))  {       cout << "Please enter numbers only!" << endl;    cin.clear();    cin.ignore(10000,'\n');  //  break;     }     else if (input_var != -1)  {       cout...

Hello World

One of the best ways to learn how to program in a new language is by looking at lots and lots of example programs. The best thing to do is to copy and paste each program below into a text file and compile it. Then, try the experiments. By extending these example programs, you will gain familiarity with different aspects fo C++, and you will feel more confident when it comes time to write programs from scratch. // hello.cpp: Maggie Johnson // Description: a program that prints the immortal saying "hello world" #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; } Some experiments to try: The most important thing is to make sure you can compile and run this program.

Converting a Resource Identifier

An identifier is a constant integer whose name usually starts with ID. Although in Win32 programming you usually can use the name of a resource as a string, in MFC applications, resources are usually referred to by their identifier. To make an identifier name (considered a string) recognizable to an MFC (or Win32) function, you use a macro called MAKEINTRESOURCE . Its syntax is: LPTSTR MAKEINTRESOURCE(WORD IDentifier); This macro takes the identifier of the resource and returns a string that is given to the function that called it. In the strict sense, after creating the resource file, it must be compiled to create a new file that has the extension .res. Fortunately, Microsoft Visual C++ automatically compiles the file and links it to the application. 

Creating a Resource

Although an application can use various resources that behave independently of each other, these resources are grouped into a text file that has the .rc extension. You can create this file manually and fill out all necessary parts but it is advantageous to let Microsoft Visual C++ create it for you. To start creating the resources: On the main menu, you can click Project -> Add Resource... In the Solution Explorer, you can right-click the name of the project, position the mouse on Add, and click Resource... Any of these actions would display the Add Resource dialog box. From this dialog box, you would select the type of resource you want and click New (or Import...). You will then be expected to design or customize the resources. Throughout our lessons, we will see various examples. After creating or designing a resource, you must save it. In most cases, you can try closing the resource. In this case, Microsoft Visual C++ would prompt you to save the resource. If you agree to do t...

Overview

In an MFC application, a resource is a text file that allows the compiler to manage such objects as pictures, sounds, mouse cursors, dialog boxes, etc. Microsoft Visual C++ makes creating a resource file particularly easy by providing the necessary tools in the same environment used to program. This means you usually do not have to use an external application to create or configure a resource file (as done in other programming environments).

The Command Line

To execute a program, you must communicate its path and possibly some additional parameters to the compiler. This information is called the command line information and it is supplied as a string. You need to keep that in mind although all programs of our lessons will be compiled inside of Visual C++. The command line information is supplied to the compiler as the lpCmdLine argument of the WinMain() function. Internally, Visual C++ creates the path and communicates it to the compiler when you execute the program. If you want to find out what command line was used to execute your program, you can call the Win32's GetCommandLine() function. Its syntax is: LPTSTR GetCommandLine(VOID); This function takes no argument but returns the command line of an application as a null-terminated string.

A Window's Instance

When you start an application such as Notepad, you are said to have created an instance of the application. In the same way, when you declare a variable of a class, an instance of the class is created and made available to the project. The WinMain() function also allows you to create an instance of an application, referred to as the hInstance argument of the WinMain() function. The instance is created as an HINSTANCE . The CWinApp class provides a corresponding instance variable called m_hInstance . This variable can let you get a handle to the instance of your application. Alternatively, to get a handle to the instance of your application, you can call the AfxGetInstanceHandle() global function. Its syntax is: HINSTANCE AfxGetInstanceHandle(); Even more, to get a handle to your application, you can call the Win32 API's GetWindowLong() function. Suppose you have opened Notepad to view the source code of an HTML document. This is said that you have an instance of Notepad. Imag...

A Global Application Object

To make your application class available and accessible to the objects of your application, you must declare a global variable from it and there must be only one variable of your application. This variable is of the type of the class derived from CWinApp . Here is an example: class CSimpleApp : public CWinApp { }; CSimpleApp MyApplication; As you can see, you can name this global variable anything you want. By tradition, in Microsoft Visual C++, this variable is named theApp . Here is an example: CSimpleApp theApp; To get a pointer to this variable from anywhere in your application, call the AfxGetApp() framework function. Its syntax is: CWinApp* AfxGetApp(); To implement the role of the Win32's WinMain() function, the framework uses its own implementation of this function and the MFC provides it as AfxWinInit() . It is declared as follows: BOOL AFXAPI AfxWinInit(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow...

The Framework

Instead of creating an application using "raw" Win32 classes and functions, the MFC library simplifies this process by providing a mechanism called the framework. The framework is a set of classes, functions, and techniques used to create an application as complete as possible with as few lines of code as possible. To provide all this functionality, the framework works behind the scenes with the CWinApp class to gather the necessary MFC classes and functions, to recognize and reconcile the Win32 classes that the application needs. This reconciliation is also made possible by a set of global functions. These functions have names that start with Afx... Some of these functions are: AfxFormatString1 AfxFormatString2 AfxMessageBox AfxFreeLibrary AfxGetApp AfxGetAppName AfxGetInstanceHandle AfxGetMainWnd AfxGetResourceHandle AfxInitRichEdit AfxLoadLibrary AfxMessageBox AfxRegisterWndClass AfxSocketInit AfxSetResourceHandle AfxRegisterClass AfxBeginT...

CObject, the Ancestor of All/Most MFC Classes

To implement its functionality, the MFC is organized as a hierarchical tree of classes, the ancestor of which is CObject : Although you can create C++ classes for your applications, most of the classes you will use throughout our lessons descend directly or indirectly from CObject . The CObject class lays a valuable foundation that other classes can build on. Using the rules of inheritance, the functionality of CObject can be transparently applied to other classes as we will learn little by little. Some of the features that CObject provides are: Performing value streaming for saving or opening the contents of files Controlling dynamic creation and destruction of its inherited classes Checking the validity of variables of classes, etc You will hardly use CObject directly in your program. Instead, you may create your own classes that are based on CObject . Here is an example: class CCar : public CObject { public: CCar(); char *Make; char *Model; int Year; long ...

Introduction to MFC

The Microsoft Foundation Class (MFC) library provides a set of functions, constants, data types, and classes to simplify creating applications for the Microsoft Windows family of operating systems. On the main menu, click File -> New Project... In the Project Types, click Visual C++ In the Templates list, click Win32 Project Set the Name to Exercise1 Click OK On the first page of the wizard, click Next On the second page, accept Windows Application and click Empty Project Click Finish

Introduction to Win32

Win32 is a library made of data types, variables, constants, functions, and classes (mostly structures) that can be used to create applications for the Microsoft Windows family of operating systems. A typical application is made of at least two objects: a control and a host object on which the control is positioned. To create a Win32 application using Microsoft Visual C++: On the Start Page, you can click New Project On the main menu, you can click File -> New Project... On the Standard toolbar, you can click the New Project button You can press Ctrl + N Any of these actions would display the New Project dialog box. From there, select Win32 Project item. Just like a C++ program always has a main() function, a Win32 program uses a central function called WinMain . The syntax of that function is: INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ); To support international characters, Microsoft Windows provides anot...

Adding Classes

To add a class to your project, you have various options. You can separately create a header file then create a source file. You can also import an existing header file and a source file from any valid path. Microsoft Visual C++ makes it easy to create a C++ class. When doing this, a header and a source files are prepared. A (default) constructor and a destructor are added. You can delete or completely change the supplied constructor and/or the destructor. Also, Microsoft Visual C++ includes the name of the header file in the source file so you would not have forgotten to do this. Most classes in Microsoft Visual C++ have their names starting with the letter C. Although this is only a suggestion, and any valid class name would work fine, to keep some harmony, in our lessons, we will follow this convention. To create a new class and add it to your project: On the main menu, click Insert -> Add Class... In the Class View, right-click the name of the project, position the mouse on Add...

Adding Existing Files to a Project

If a file had already been created and exists on a separate folder, drive, directory or project, you can import it and add it to your application. When you decide to add an existing file to a project, because Microsoft Visual Studio allows you a great level of flexibility on the types of files you can add, it is your responsibility to check that the file is valid. If you copy a file from somewhere and paste it in the folder that contains your project, the file is still not considered as part of the project. You must explicitly add it. Before adding an existing file to your project: On the main menu, you can click File -> Add Existing Itemテδ「テ「窶堋ャテつヲ On the main menu, you can also click Project -> Add Existing Itemテδ「テ「窶堋ャテつヲ In the Solution Explorer, you can right-click the name of the project, position your mouse on Add and click Add Existing Itemテδ「テ「窶堋ャテつヲ Any of these actions would open the Add Existing Item dialog box. This requires you to locate the item and select it. O...

Creating a File

Although the most popular files used in Microsoft Visual C++ are header and source files, this IDE allows you to create various other types of files that are not natively C++ types. To create a file, if a project is currently opened: On the main menu, click Project -> Add New Item... This would open the New dialog box from where you can select the desired type of file and click Open. By default, the new file would open in the Source Code Editor. If you want to open the file otherwise, after selecting it in the New dialog box, click the arrow of the Open As combo box. In the Solution Explorer or the Class View, right- click the name of the project, position the mouse of Add, and click the option you want

Introduction to Microsoft Visual C++/MFC

Microsoft Visual C++ is a programming environment used to create applications for the Microsoft Windows family of operating systems. To use these lessons, you must have installed either Microsoft Visual Studio 2010. Although Microsoft Visual Studio 2010 Professional provides many programming environments for the price of one, in our lessons, we will use it but we will cover only the Microsoft Foundation Class (MFC) library side. After installing it, to use the programming environment, you must first open it. To do that, you would click Start -> (All) Programs -> Microsoft Visual Studio 2010 -> Microsoft Visual Studio 2010. In our lessons, unless used in code, the -> arrow means an action that should follow another. Based on this: Edit -> Copy means click Edit, then click Copy View -> Toolbars -> Custom means click View position the mouse on Toolbars, and then click Custom To start Microsoft Visual C++ or Visual Studio, on the Taskbar, click Start (All) Prog...

Links

Official resources MSDN Visual C++ Home microsoft.public.vc.language The Top CodeGuru CodeProject DeveloperFusion.com C/C++ Users Journal RSDN (RU) First Steps (RU) Sources.ru (RU) Tricks, Tips, FAQs Bjarne Stroustrup's C++ Style and Technique FAQ CodeGuru FAQ C++ FAQ Lite by Marshall Cline CodeProject Forum FAQ Boob Moore's page (Win32, NT, MFC) Edelmiro Fuentes's page http://www.sources.ru/cpp/cpp_vcpp_faq.shtml (RU) Akzhan Site (RU) Forums, newsgroups CodeGuru Forum microsoft.public.vc.language comp.lang.c++ RSDN Forum (RU) Libraries, Components, Add-ons, Add-ins Boost Libraries #1 for C++! TinyXML - light yet powerful XML library. Forget about MSXML! :) Whole Tomato Software: Code syntax highlighting Compuware corporation: BoundsChecker, SoftICE etc. Xtreme ToolkitPro - Industry leading Visual C++ MFC UI controls. STL Error Decryptor UCanCode: XD++ MFC Class Library for Diagrams Regular Expression Library Disk File Sy...