Skip to main content

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);
As you can see, the Win32's WinMain() and the MFC's AfxWinInit() functions use the same arguments.

Comments

Popular posts from this blog

How to create a new static library project in Visual C++

From the File menu, select New and then Project…. From the Project types pane, under Visual C++, select Win32. From the Templates pane, select Win32 Console Application. Choose a name for the project, such as MathFuncsLib, and enter it in the Name field. Choose a name for the solution, such as StaticLibrary, and enter it in the Solution Name field. Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application Wizard dialog, press Next. From the Application Settings page of the Win32 Application Wizard, under Application type, select Static library. From the Application Settings page of the Win32 Application Wizard, under Additional options, deselect Pre-compiled header. Press Finish to create the project.

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...

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...