Some necessary nouns of MFC (II)

Source: Internet
Author: User
Tags getmessage message queue

    1. Ui:windows program is divided into "program code" and "ui (User Interface) resources" two parts, two part finally with RC

The compiler is consolidated into a full EXE file (Figure 1-1). The so-called UI resource refers to the Function menu, dialog box

Appearance, program icon, cursor shape and so on. The actual content of these UI resources (binary code) is based on the

Types of tools, and exist in various extensions, such as. ico,. bmp,. cur, and so on. Programmers must be in a so-called

Describe them in the resource description file (. rc). The RC compiler (RC.EXE) reads the description of the RC file and then all the UI

A resource file is centrally produced. RES file, and then combined with the program code, this is a complete windows

Executable file.

2. Message-driven: can be divided into messages generated by a hardware device, such as a mouse

Mobile or keyboard is pressed), placed in the system queue, and by the Windows system or other

Messages sent over by Windows programs are placed in the program queue (application queue).

3. UpdateWindow function: We want to send a wm_paint to the window to drive the drawing action of the window, so call

UpdateWindow.

4. Message handler: TranslateMessage is to convert the keyboard message, DispatchMessage will pass the message to the window

Number to deal with;

Because at the time of the message, the operating system has indicated the owning window according to the state of the time, and the window category that the window belongs to has clearly marked the window function (that is, the function specified by Wc.lpfnwndproc). So dispatchmessage does not need to specify the window function (note that the dispatchmessage is to send the message to the corresponding window function)

5. Encapsulate the data and the functions with the data to be processed: a concrete implementation of encapsulating "data" and "method of processing data" in object-oriented concept

6. Message map: It is possible to design the contents of the window function more modular, more generalized: (see MFC in layman's details)

#define DIM (x) (sizeof (x)/sizeof (X[0))

struct Msgmap_entry {

UINT nmessage;

LONG (*PFN) (HWND, UINT, WPARAM, LPARAM);

};

struct Msgmap_entry _messageentries[] =

{

Wm_create, OnCreate,

WM_PAINT, OnPaint,

Wm_size, OnSize,

WM_COMMAND, OnCommand,

Wm_setfocus, OnSetFocus,

Wm_close, OnClose,

Wm_destroy, OnDestroy,

};

struct Msgmap_entry _commandentries =

{

Idm_about, Onabout,

Idm_fileopen, OnFileOpen,

Idm_saveas, Onsaveas,

};

LRESULT CALLBACK WndProc (HWND hwnd, UINT message,

WPARAM WPARAM, LPARAM LPARAM)

{

int i;

for (i = 0; I < Dim (_messageentries); i++) {//

if (message = = _messageentries[i].nmessage)

Return ((*_MESSAGEENTRIES[I].PFN) (hWnd, message, WParam, LParam));

}

Return (DefWindowProc (hWnd, message, WParam, LParam));

}

LONG OnCommand (HWND hwnd, UINT message,

WPARAM WPARAM, LPARAM LPARAM)

{

int i;

for (i = 0; I < Dim (_commandentries); i++) {//

if (LoWord (wParam) = = _commandentries[i].nmessage)

Return ((*_COMMANDENTRIES[I].PFN) (hWnd, message, WParam, LParam));

}

Return (DefWindowProc (hWnd, message, WParam, LParam));

}

Long OnCreate (HWND hwnd, UINT wmsg, uint wParam, long LParam)

{

...

}

7. Dialog function: its type is very similar to the window function, but it is usually only

Wm_initdialog and WM_COMMAND two messages. Each control component in the dialog box is also a small window, each with its own window function, which communicates with the message and its manager (the parent window, the dialog box). All of the control components are WM_COMMAND, and the parameters tell which control component and which notification (notification)

Modal dialog is activated and ended by the DialogBox and EndDialog two API functions

8. Resource description file (. RC): RC file is a place to describe resources in words. There are more than nine commonly used resources, namely icon, CURSOR,

BITMAP, FONT, DIALOG, MENU, ACCELERATOR, STRING, Versioninfo. There may also be new resources that are constantly being added;

Some text descriptions need to go through the RC compiler to produce the binary code that can be used

    1. The birth and death of Windows programs:

***********************************

Detailed Explanation:

1. Call CreateWindow during program initialization, set up a window for the program, as the program's fluorescent

Screen stage. CreateWindow generated window will send wm_create directly to the window function,

The latter can then do some initialization actions (such as configuring memory, opening files, reading the initial capital

Material... )。

2. While the program is alive, it constantly crawls messages from the message store with GetMessage. If this elimination

The Wm_quit,getmessage returns 0 and ends the while loop, ending the entire program.

3. DispatchMessage the message to the window through the assistance and supervision of the Windows USER module

Function. The message will be judged and processed at that point.

4. The program is continuously 2. and 3. The action.

5. When the user presses the CLOSE command item in the System menu, the system sends out the WM_CLOSE. Usually the program

The window function does not section this message, so DefWindowProc handles it.

6. After DefWindowProc receives WM_CLOSE, call DestroyWindow to clear the window.

DestroyWindow itself will send out Wm_destroy.

7. The standard response of the program to Wm_destroy is to call PostQuitMessage.

8. PostQuitMessage No other action, just send out wm_quit message, ready to let the message follow

The GetMessage in the ring is obtained, as in step 2, ending the message loop.

******************************************

10. Processing of idle time: OnIdle; idle time, which means "there is no message waiting to be processed in the system" time

**************************

Background work is best done in idle time. Traditional SDK Program if you want to handle idle time, you can use the following

The loop replaces the traditional message loop in WinMain:

while (TRUE) {

if (PeekMessage (&msg, NULL, 0, 0, pm_remove) {

if (msg.message = = wm_quit)//Note the difference between the two

Break

TranslateMessage (&MSG);

DispatchMessage (&MSG);

}

else {

OnIdle ();

}

}

The reason is that PeekMessage and getmessage are different in nature. They are all caught in a message queue, if

Cannot be caught, the main thread of the program (primary thread, which is a UI execution thread) is suspended by the operating system.

When the operating system comes back to take care of this one thread of execution, and the discovery message queue is still empty, this time two APIs

The behavior of the function is different:

GetMessage will Summen, so the operating system to take care of other people.

PeekMessage will retrieve control to allow the program to execute for a period of time. So the above message loops into

Into the OnIdle function.

***************************

11. MFC has two very important virtual

Quasi-function: The Serialize function associated with the document and the OnDraw function associated with the view. You should rewrite these two virtual functions in your own Cmydoc and CMyView.

12.

Some necessary nouns of MFC (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.