Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
5.22 Mб
Скачать
    1. A Framework of mfc-program

An MFC program must have at least two classes: the application class and the window class. In fact, you can create a minimal MFC program in fewer lines of code than you'd believe. The following list outlines the steps you must take to create the smallest, functional MFC program:

  1. Create an application class derived from CWinApp.

  2. Create a window class derived from CFrameWnd with the special map of messages.

  3. Instantiate a global instance of your application object.

  4. Instantiate your main window object in the application object's InitInstance() function.

  5. Create the window element (the Windows window) associated with the window object by calling Create() from the window object's constructor.

  6. Create the map of messages.

Creating the Application Class

In application class, you must override the InitInstance() function. In Listing 1.1 you can see such application class, called CApp.

Listing 1.1 The description of CApp1 Class

class CApp : public CWinApp

{public:

BOOL InitInstance();

};

That CApp class is derived from MFC's CWinApp class. The CApp class overrides the InitInstance() function, which is the function MFC calls to create the application's window object. Because the base class's InitInstance() does nothing but return a value of TRUE, if you want your application to have a window, you must override InitInstance() in your application class.

Creating the Frame-Window Class

Listing 1.2 is a description of CMainWin class, which represents the CApp1 class's main window.

Listing 1.2 The part of header file for the CMainWin Class

class CMainWin : public CFrameWnd

{public:

CMainWin();

DECLARE_MESSAGE_MAP()

};

That notation DECLARE_MESSAGE_MAP() means the declaration of message processing in our program. In the first case there can be no processing.

The functions usage in MFC framework program

In declared classes we can see at least two functions: these are InitInstance() of CApp and the constructor CMainWin() of class with the same name. First function provides the initialization code of application, and second – the creation of main window of our program.

In the next listing is a code of them:

Listing 1.3 The Implementation of InitInstance() for CApp class, CMainWin() for CMainWin() class.

BOOL CApp::InitInstance()

{ m_pMainWnd = new CMainWin();

m_pMainWnd->ShowWindow(m_nCmdShow);

return TRUE;

}

CMainFrame::CMainFrame()

{ Create(NULL, "MFC App1"); }

Certainly, declared functions override the functions of parent classes. They have some special features.

Firstly – about InitInstance() function.

m_pMainWnd = new CMainFrame();

That line creates a new CMainFrame() object on the heap, calling the CMainFrame() class's constructor, where, the class creates the main window element. A pointer to the new window object gets stored in m_pMainWnd, which is a data member of the application class. Through this pointer the application class maintains contact with its main window.

After instantiating the window object, the window element must usually be displayed. This is done by calling the window object's ShowWindow() function, like this:

m_pMainWnd->ShowWindow(m_nCmdShow);

ShowWindow()'s single argument indicates how the window should be displayed initially. You need only pass along the application object's m_nCmdShow data member, which contains the value of the nCmdShow parameter that Windows passed to WinMain().

When an application starts up, there are several ways that its window can be initially displayed. These different display styles are represented by a set of constants defined by Windows, including SW_HIDE, SW_MINIMIZE, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, and SW_SHOWNORMAL, among others.

In CMainFrame() function, there is used the only call - the Create() function with two arguments. But really such function has many parameters. Create()'s prototype looks like that:

BOOL Create(LPCTSTR lpszClassName, // name window class (with parameters of window)

LPCTSTR lpszWindowName, // header (text) on the top of window

WORD dwStyle = WS_OVERLAPPEDWINDOW, // window style, default style – with system menu and three standard buttons in the corner (WS_OVERLAPPEDWINDOW).

const RECT& rect = rectDefault, // the position and size of window

CWnd* pParentWnd = NULL, // the pointer to the parent window and NULL if current window is main window of the program.

LPCTSTR lpszMenuName = NULL, // the reference to the main menu of program.

DWORD dwExStyle = 0, // extended style specification

CCreateContext* pContext = NULL); // supplementary context structures

So, really in our case the most of parameters have default values.

By the way, the declaration of position and size uses the special type:

typedef struct tagRECT

{LONG left; // Specifies the x-coordinate of the upper-left corner of a rectangle;

LONG top; // Specifies the y-coordinate of the upper-left corner of a rectangle;

LONG right; // Specifies the x-coordinate of the lower-right corner of a rectangle;

LONG bottom; // Specifies the y-coordinate of the lower-right corner of a rectangle;

} RECT;

The RECT structure defines the coordinates of the upper-left and lower-right corners of a rectangle.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]