Skip to main content

Posts

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