3. Windows and messages

xiaoxiao2021-03-06  145

3. Windows and messages

In the first two chapters, the program uses the same functional messagebox to output text to the user. The MessageBox is created a "window". In Windows, the word "window" has exact meanings. One window is a rectangular area on the screen that receives the user's input and display output content in the format of text or graphics.

The Messagebox function creates a window, but this is just a limited special window. The message window has a title column with a closed button, an option graphic, one or more lines, and up to four buttons. Of course, you must select Windows to provide your icon and button.

The Messagebox is very useful, but will not use it too much below. We cannot display graphics in the message block, and you cannot add a menu in the message block. To add these items, you need to build your own window and start now.

It is very simple to establish a window in your own window, just call the CREATEWINDOW card. Ok, although it is true for CreateWindow in the function of establishing the window, and you can also find this file in / platform sdk / user interface service / windowing / windows / window reference / window finctions, but you will find the first parameter of CreateWindow The so-called "Window Category Name", and the window category is connected to the so-called "Window Message Processing". Before we call CREATEWINDOW, a little background knowledge will be helpful. The overall structure is designed for Windows program, which is actually a program-oriented programming (OOP). This is the most obvious in the most used objects in Windows. This object is the reason why Windows is named "Windows", which has a characteristic feature, and may even appear in your dream, this is what is called "Window". The most obvious window on the desktop is the application window. These windows contain the title column of the display, the menu, may even have tool columns and roll columns. Another type of window is a dialogue, which can have a title column or no title column. The decorative dialogue surface also has a wide variety of buttons, radio buttons, check squares, clear unparalleled blocks, rolling columns, and text input areas. Each small visual object is a window. More specifically, these are called "sub-window" or "Control Items" or "Sub Video Control Items". As an object, the user will see these windows on the screen and interact directly with them through keyboards and horses. More interesting is that the program of writing is extremely similar to the user's view. The window receives the input of the window in the form of "message", and the window also uses the message to communicate with other window. The understanding of the message will be one of the obstacles that learn how to write a Windows program. This has a Windows message example: We know that most Windows programs have a size suitable app window. That is, you can change the size of the window by dragging the border of the window through a hilar. Typically, the program will respond to this magnitude change by changing the content in the window. You may guess (and you are also correct), is Windows itself instead of the application in processing and user re-adjusting the sharing of the window size. Since the application can change its display, it also "knows" the window size change. How does the application know that the user changed the size of the window? Since the program writer is used to the text mode program, the operating system does not set the mechanism to notify the user. The key to the problem is to understand the architecture used by Windows. When the user changes the size of the window, the Window sends a message to indicate the size of the new window. Then the program can adjust the content in the window to respond to changes in the size. "Windows sends a message." We hope that readers don't see this sentence without seeing this sentence. What does it mean? What we are discussing here is a program code, not an email system. How to send a message to the job system? In fact, the so-called "Windows Send Message" means a function in the Windows Call program, which describes this specific message. This letter in the Windows program is called "Window Message Processing". Undoubtedly, readers are very familiar with the practice of a program call job system. For example, the program is to use the relevant system call when opening the magnetic file. If the reader is not used to it, it may be a homework system call program, which is the basis for the Windows object-oriented architecture. Each window established by the program has a related window message processing program. This window message processing program is a function that can be either in a program or in a dynamic linking library. Windows sends a message to the window by calling the window message processing.

Window message processing processes based on this message and then transmits the control back to Windows. More specifically, the window is usually established on the basis of the "Window Category". The window category identifies the window message processing program that handles the window message. Use the window category to enable multiple windows to belong to the same window category and use the same window message processing program. For example, all buttons in all Windows programs are based on the same window category. This window category is coupled to a window message processing that handles all button messages (in Windows Dynamic Library). In the project-oriented programming, the object is a combination of program and data. Windows is an object that the program is a window message processing program. The information is information and Windows saved for each window and the window category in the system. Window message processing program is sent to the window. These messages are often informed of the window, and the user is using a keyboard or a mouse for input. This is exactly the key view window knows that it is "pressed". In the window size, or when the window surface needs to be redrawn, the window is notified of the window. After the Windows program starts, Windows establishes a "message 伫 伫" for the program. This message is used to store messages that may be established by the program. There is a small section code in the program, called "message back circle", used to remove the message from the column and send them to the corresponding window message processing program. Some messages are sent directly to the window message processing, and do not place in the message column. If you are too briefly description of this Windows architecture, let us see how the window, window category, window message processing program, message, message ring, and window messages, information of. This may be some help you. The Hellowin program creates a window First, you need to register a window category, which requires a window message processing to process the window message. Handling Windows messages have brought some burden on each Windows program. The whole thing to do in the Hellowin program shown in the formula 3-1 is the cuisine. Cheng 3-1 Hellown

Hellowin.c

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

Hellowin.c - displays "Hello, Windows 98!" In Client Area

(c) Charles Petzold, 1998

-------------------------------------------------- --------------------- * /

#include

Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

{

Static tchar szappname [] = text ("hellowin");

Hwnd hwnd;

MSG msg;

WNDCLAS WNDCLASS;

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

WNDCLASS.LPFNWNDPROC = WNDPROC;

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

WNDCLASS.HINSTANCE = HINSTANCE;

Wndclass.hicon = loading;

Wndclass.hcursor = loadingcursor (null, idc_arrow); wndclass.hbrbackground = (Hbrush) getStockObject (White_brush);

WNDCLASS.LPSZMENUNAM = NULL;

Wndclass.lpszclassName = szappname;

IF (! registerclass (& wndclass))

{

MessageBox (Null, Text ("This Program Requires Windows NT!"),

Szappname, MB_ICONEROR);

Return 0;

}

HWND = CREATEWINDOW (Szappname, // WINDOW CLASS NAME

TEXT ("The Hello Program"), // WINDOW CAPTION

WS_OVERLAPPEDWINDOW, // WINDOW STYLE

CW_USEDEFAULT, // Initial X position

CW_USEDEFAULT, // Initial Y position

CW_USEDEFAULT, // Initial X size

CW_USEDEFAULT, // Initial y size

Null, // Parent Window Handle

Null, // WINDOW MENU HANDLE

Hinstance, // Program Instance Handle

NULL); // CREATION Parameters

ShowWindow (hwnd, icmdshow);

UpdateWindow (HWND);

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

Return msg.wparam;

}

Lresult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM)

{

HDC HDC;

Paintstruct PS;

RECT;

Switch (Message)

{

Case WM_CREATE:

Plays (Text ("Hellion.wav"), NULL, SND_FILENAME | SND_ASYNC);

Return 0;

Case WM_Paint:

HDC = BeginPaint (HWND, & PS);

GetClientRect (hwnd, & review);

DrawText (HDC, Text ("Hello, Windows 98!"), -1, & review,

DT_SINGLINE | DT_CENTER | DT_VCENTER);

Endpaint (hwnd, & ps);

Return 0;

Case WM_DESTROY:

PostquitMessage (0);

Return 0;

}

Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);

}

A normal application window is established, as shown in Figure 3-1. "Hello, Windows 98!" Is displayed in the center of the window display area. If you install a sound card, you can also hear the corresponding reading sound.

Figure 3-1 Hellown Window

Remind you Note: If you use Microsoft Visual C to create a new project for this program, you have to add the program library file required for the connection. Select the setting option from the Project function table and select the Link page label. Select General from Category Clean square, then add WinMM.LIB (Windows MultiMedia - Windows Multimedia) in Object / Library Modules text. You do this because hellowin will use multimedia function calls, and the multimedia program library files are not included in the form. Otherwise, the connection is incorrectly reported, indicating that Playsound is not available. Hellowin will access the file hellowin.wav, which is in the helpin directory of the cuffs attached to this book. When performing hellowin.exe, the internal directory must be hellowin. When this program is executed in Visual C , although the executive generates in the Release or Debug subdirectories in Hellowin, the execution program is still in Hellown.

Tongshi

In fact, most of the HELLOWIN.C program is included in each Windows program code. No one can remember all the writing of this process; usually, the Windows program writer always copies an existing program when it starts writing a new rule, and then makes corresponding modifications. You can use this book to use the program included in the disc.

As mentioned above, Hellowin will display a string in its window. This statement is not completely correct. The text is actually displayed in the center of the program display area, which is a large white area in the title column and boundaries in Figure 3-1. This difference is important to us; the display area is a free drawing and displaying the window area of ​​the output result.

If you think about it, you will find that although only 80 trip code, this window is surprisingly with many features. You can use a mouse to hold down the title column, move the window on the screen; you can press the size border to change the size of the window. When the window size changes, the program automatically repositions the "Hello, Windows 98!" Strings in the center of the display area. You can zoom in to Hellowin by maximizing buttons to full screen; you can also reduce the program into one illustration. You can perform all options in the system function table (that is, press the small icon at the leftmost left end of the title column); you can also select the Close option from the system function table, or click the rightmost button of the title column, or double-click the title. The leftmost illustration of the column is to close the window to terminate the execution.

We will make a detailed check in this chart in the remainder of this chapter. Of course, we must first look at it.

Like the paradigm in the first two chapters, hellowin.c also has a WinMain, but it has another one, named WndProc. This is the window message processing program. Note that there is no call code in Hellowin.c. Of course, there is a reference to WndProc in WinMain, and this is why the file is declared near the beginning.

Windows Versuck Call

Hellowin calls at least 18 Windows functions. These functions are listed in the order that appears in Hellown, and their own concise descriptions:

Loadicon loads the drainage. LoadCursor is loaded with a mouse cursor supply. GetStockObject acquires a graphic article (in this example, it is a brush object to draw the background of the window). RegisterClass registers the window category of the program window. MessageBox displays a message. CREATEWINDOW creates a window according to the window category. Showwindow displays the window on the screen. UpdateWindow indicates that the window is self-updating. GetMessage has achieved a message from the message column. TranslateMessage translates some keyboard messages. DispatchMessage sends a message to the window message processing program. Playsound Plays an audio file. Beginpaint begins to draw a window. GetClientRect gets the size of the window display area. DrawText Display String. Endpaint ends the window. POTQUITMESSAGE inserts a "exit program" message in the message gap. DEFWINDOWPROC performs a constant message processing. These functions are illustrated in the Platform SDK file and declared in different header files, most of which declare in Winuser.h.

Uppercase letter identification word

Readers may notice that there are several uppercase identification words in Hellowin.c, which is defined in the Windows header file. Some identifies contain two letters or three letters, and the head is behind the bottom line:

CS_HREDREDTT_VCENTERSND_FILENAMECS_VREDRAWIDC_ARROWM_CREATECW_USEDEFAULTIDI_APPLICATIONWM_DESTROYDT_CENTERMB_ICONERRORWM_PAINTDT_SINGLINESND_ASYNCWS_OVERLAPPPPPPedWindow

These are simple numerical constants. The word first indicates the category to which the constant belongs, as shown in Table 3-1.

Table 3-1

Character Class CS Window Category CW Create Window DT Drawing Text IDI Illustration IDIDC Cursor IDMB Message Square SND Sound WM Window Message WS Window Style

Advakers the author writer not to force the remembering the numerical constant in the WINDOWS program. In fact, each numerical constant used in Windows has a corresponding identification word definition in the header file.

New data type

Other identifier in Hellowin.c is a new data type, and it is also defined using the TypeDef narrative or #define description in the Windows header file. Initially, it is for easyging Windows programs from the original 16-bit system to future use 32-bit (or other) operating systems. This method is not as smooth as everyone's imagination, but this concept is basically correct.

Sometimes these new information is just for convenience of abbreviation. For example, the UINT data type for the second parameter for WndProc is just a unsigned int (no fixed-to-line integer), in Windows 98, this is a 32-bit value value. The PSTR data type of the third parameter for WinMain is the indicator of a string, that is, a char *.

The meaning of other data type is not obvious. For example, WndProc's third and fourth parameters are defined as WPARAM and LPARAM, respectively, the source of these names is a bit history background: When Windows is still a 16-bit system, the third parameter of WndProc is defined as a word, this is A 16-bit no-unsigned short is integer, and the fourth parameter is defined as a long, this is a 32-bit element has a positive and negative intensity, resulting in the front of the text "param" plus Pre-word "W" and "L". Of course, in 32-bit Windows, WPARAM is defined as a UINT, and LPARAM is defined as a long (this is a long integer pattern in C), so the two parameters of the window message processing are 32 digits. Yuan's value. This may be a bit strange, because the Word data is still defined in Windows 98 as a 16-bit unparalleled integer, so "W" before "PARAM" is a bit misused. The WndProc function is transmitted back to a value of LRESULT, which is simply defined as a long. WinMain is specified in a WinAPI type (all Windows files defined in the header file are specified), and the WndProc is specified for a CallBack type. These two identifiers are defined as _stdcall, and the call parameter transmission mode of a functions that occur between the Windows itself and the user's application.

Hellowin also uses four data structures defined in the Windows header file (we will discuss later in this chapter). These data structures are shown in Table 3-2.

Table 3-2

Structural Meaning MSG Message WNDCLASS Window Category Structure PaintStruct Drawing Structure Rect Rectangular Structure

The front two data structures are used in WinMain, which defines two structures called MSG and WNDCLASS, and the two data structures are used in WNDPROC, which defines the PS and RECT structures.

Code introduction

Finally, there are three uppercase identification words (see Table 3-3) for different types of "code":

Table 3-3

Identification word meaning Hinstance enforcement entity (program itself) code HWND window code HDC device content code

The code is very frequent in Windows. Before the end of this chapter, we will encounter Hicon (graphic code), HCURSOR (microbiographical number) and Hbrush (painted code).

The code is an integer (usually a 32-bit yuan) integer, which represents an object. The code in Windows is similar to the archive code used in traditional C or MS-DOS design. The program is almost always obtained by calling the Windows Function. Program uses this code in other Windows functions to use the object it represents. The actual value of the code is irrelevant to the program. However, the Windows module that provides code to your program knows how to use it to use the corresponding item.

Hungarian representation

Readers may notice that there are some modes in hellowin.c very quirky. Such as szcmdline, it is the parameter passed to WinMain.

Many Windows program writers use a variable name called "Hungarian Specification". This is to commemorate the legendary Microsoft program writer Charles Simonyi. Very simple, the variable name begins with one or more lowercase letters, these letters indicate variables. For example, SZ in szcmdline represents "strings ending at 0". The hinstance and hpRevinstance in the H-name represents "Code"; the ICMDSHOW is the first "integer". The latter two parameters of WndProc also use Hungarian representation. As I explained earlier, although WPARAM should be more appropriately named UIParam (represent "no normal negative integers"), but because these two parameters are defined using data type WPARM and LPARAM, so keep them Traditional name. When naming structure variables, lowercases can be used as a string name of the string name, or used as the entire variable name. For example, in the WinMain in Hellowin. C, the MSG variation is the structure of the MSG type; WNDCLASS is a structure of the WndClassex type. In the WNDPMC function, the PS is a PAINTSTRUCT structure, and the RECT is a RECT structure.

Hungarian representation can help program writingers and early discovery and avoid errors in the program. Since variable names describe both variables, it is more likely to avoid errors that generate data type.

Table 3-4 lists the number of variations that are often used in this book.

Table 3-4

Word data type CCHAR or WCHAR or TCHARBYBYTE NSHORTIINTX, YINT is used as X coordinate and Y coordinate Cx, and Cyint is used as X length and Y length, respectively; C represents "counter" B or FBool (int); f represents "Flag" WWORD (no lack of short integers) llong (long integer) DWDWORD (no fixed number long integer) FNFunction SSTRING SZ with bit element group value 0 String h code p indicator

Register window category

The window is established in accordance with a window category, and the window category is used to identify the window message processing program for processing the window message.

Different windows can be established in accordance with the same window category. For example, all button windows in Windows-including buttons, verification blocks, and radio buttons - are based on the same window category. The window category defines the window message processing program and other features of the window established in this category. When establishing a window, define some features unique to this window.

Before setting up a window, you must first call the registerclass to register a window category. This function requires only one parameter, namely a structural indicator of WndClass. This structure includes two fields of point strings, so the structure defines two different ways in the Winuser.h header file, the first is the ASCII version of WndClassa:

Typedef struct tagwndclassa

{

Uint style;

WndProc lpfnwndproc;

Int cbclsextra;

Int Cbwndextra;

Hinstance hinstance;

Hicon Hicon;

Hcursor hcursor;

Hbrush Hbrbackground;

LPCSTR LPSZMENUNUNAME;

LPCSTR LPSZCLASSNAME;

}

WNDCLASSA, * PWNDCLASSA, NEAR * NPWNDCLASSA, FAR * LPWNDCLASSA; Here you prompt information and Hungarian representation: where the LPFN header represents "point to the long indicator". (In Win32 API, long indicators and short indicators (or short-range indicators) are not distinguished. This is just 16-bit WINDOWS relics.) The CB word represents "bit element group" and usually represents a bit. Group size. The H-first is a code, and the HBR word represents "a code of a painting". LPSZ Charity representative "point to indicators of 0 end strings".

The structure of the Unicode version is defined as follows:

Typedef struct tagwndclassw

{

Uint style;

WndProc lpfnwndproc;

Int cbclsextra;

Int Cbwndextra;

Hinstance hinstance;

Hicon Hicon;

Hcursor hcursor;

Hbrush Hbrbackground;

LPCWSTR LPSZMENUNUNUNAME;

LPCWSTR LPSZCLASSNAME;

}

WNDCLASSW, * PWNDCLASSW, NEAR * NPWNDCLASSW, FAR * LPWNDCLASW;

The only difference with the former is that the last two fields are defined as pointing to the wide string constant, rather than pointing to the ASCII string constant.

Winuser.h defines the WndClassA and WndClassW structures (and the indicators of the point to the structure) define the explanation of WNDCLASS and the WNDCLASS (including some backward compatible program code):

#ifdef unicode

Typedef wndclassw wndclass;

TypedEf PWNDCLASSW PWNDCLASS;

Typedef npwndclassw npwndclass;

Typedef lpwndclassw lpwndclass;

#ELSE

Typedef wndclassa wndclass;

Typedef PWNDClassa PWNDCLASS;

Typedef npwndclassa npwndclass;

Typedef lpwndclassa lpwndclass;

#ENDIF

When the structure is listed later, it will only list the same structure as the same function, which is like this to WndClass:

Typedef struct

{

Uint style;

WndProc lpfnwndproc;

Int cbclsextra;

Int Cbwndextra;

Hinstance hinstance;

Hicon Hicon;

Hcursor hcursor;

Hbrush Hbrbackground;

LPCTSTR LPSZMENUNAME;

LPCTSTR LPSZCLASSNAME;

}

WNDCLASS, * PWNDCLASS;

I will no longer focus on the definition of the indicator. A program author's program should not be disordered because of different indicators that are headed by LP or NP.

Define a structure for WNDCLASS in WinMain, usually like this:

WNDCLASS WNDCLASS;

Then you can initialize the 10 columns of the structure and call RegisterClass. The two most important two fields in the WNDCLASS structure are the second and last one, and the second field (lpfnwndproc) is the location of the window message processing program used by all the windows established by this category. In Hellowin.c, this is a WndProc. The last field is the text name of the window class. Crew writers can freely define their names. In the program that only establishes a window, the window category name is usually set to the program name.

Other fields describe some of the features of the window category according to the following method. Let's take a look at each field in the WNDCLASS structure.

Narrate

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

Bring two "Window Category Style" identification words using C bit "or" operator. In the header file WinUser.h, a whole group has been defined with the identification word headed by CS:

#define CS_VREDRAW 0x0001

#define cs_hredraw 0x0002

#define cs_keycvtwindow 0x0004

#define cs_dblclks 0x0008

#define cs_owndc 0x0020

#define cs_classdc 0x0040

#define cs_parentdc 0x0080

#define cs_nokeycvt 0x0100

#define cs_noclose 0x0200

#define cs_savebits 0x0800

#define cs_bytealignclient 0x1000

#define cs_bytealignwindow 0x2000

#define cs_globalclass 0x4000

#define CS_IME 0x00010000

Since each identification word can set a value of a bit element in a composite value, the identification word defined in this manner is often referred to as "bit yuan flag". Usually we only use a small number of window categories. The two identifier words used in Hellowin indicate that all windows established according to this category, whenever the horizontal direction of the window (CS_HREDRAW) or the vertical direction size (CS_VREDRAW) changes, the window will completely retransmate. Changing the Hellown's window size, you can see the string still displayed in the center of the window, the two identifier make sure this. Soon we will see how the window message processing has learned the change in this window.

The second field of the WNDCLASS structure is initialized by the following description:

WNDCLASS.LPFNWNDPROC = WNDPROC;

This narrative sets this window message processing program to WNDPROC, which is the second function in Hellowin.c. This process will handle all messages in all windows established based on this window category. In the C language, when using a letter name in the structure, it is really provided to the metric.

The following two fields are used to reserve some additional spaces in the window structure saved in Windows and Windows:

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

The program can use the reserved space as needed. Hellowin did not use them, so the set value is 0. Otherwise, like the Hungarian representation, this field will be treated as a "reserved bit group". (The CHECKER3 in Chapter VII Checker3 will use the CBWndextra field.)

The next field is the program's executive entity code (which is also one of the parameters of WinMain): wndclass.hinstance = Hinstance;

Narrate

WNDCLASS.HICON = LOADICON (NULL, IDI_Application);

Set an illustration for all windows established according to this window category. The icon is a small dot matrix image, which represents the user, will appear on the left end of the header column in the Windows work column and the window. Behind this book, you will learn how to book your own Windows program. Now, we will use a predefined illustration for convenience.

To get the code of the predefined illustration, you can set the first parameter to null to call Loadicon. At the time of the illustration of the subordinator custom (the illustration of the .exe program) on the magnetic sheet should be set, this parameter should be set to the executive entity code HINSTANCE. The second parameter represents the illustration. For a predefined illustration, this parameter is the identification word ("ID representative illustration") starting with the IDI, and the identifier is defined in WinUser.h. The IDI_Application is a simple window small graphics. The Loadicon letter is back to the code of the illustration. We don't care about the actual value of this code, which is only used to set the value of the HICON field element. This field is defined in the WNDCLASS structure as a HICON type, and the meaning of this type is "Handle to an icon".

Narrate

WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow);

Very similar to the previous narrative. The loadcursor is loaded into a predefined hidden mouse cursor (named IDC_ARROW), and passed back the code of the cursor. This code is set to the HCursor field of the WNDCLASS structure. When the mouse cursor appears on the display area of ​​the window established by this category, it becomes a small arrow.

The next field specifies the background color based on this category. The HBR word in the HBRBACKGROUND field represents "Handle to A Brush". The painting is a drawing word, refers to the coloring style of one area. Windows has several standard paintings, also known as "STOCK" painting. The getStockObject call shown here will pass the code of a white painted brush:

WNDCLASS.HBRBACKGROUND = getStockObject (White_brush);

This means that the background of the window display area is completely white, which is an extremely common practice.

The next field specifies the window category function table. Hellowin has no application function table, so the field is set to NULL:

WNDCLASS.LPSZMENUNUNAME = NULL;

Finally, a category name must be given. For small screws, the category name can be the same as the program name, that is, "Hellowin" string in the szappname variable.

Wndclass.lpszclassName = szappname;

As for the string consisting of ASCII characteristics or by Unicode characterization, the Unicode recognition is defined.

After initializing the 10 fields of this structure, hellowin calls registerclass to register this window category. This function has only one parameter, which is the indicator of the WNDCLASS structure. In fact, the RegisterClassa file will get an indicator to the WNDCLASSA structure, while the RegisterClassW function will get an indicator to the WndclassW structure. Which of the program is to be used to register the window category, depending on the message sent to the window contains ASCII text or Unicode text.

There is now a problem: If you compile the program with a defined Unicode identifier, the program will call RegisterClassw. This program can be implemented in Microsoft Windows NT in Microsoft WINDOWS NT. However, if this program is executed on Windows 98, the RegisterClassw is not implemented. The function has an entry point, but only the 0 after the call, indicating the error. This is a good opportunity to notify the user if the Unicode program executing under Windows 98 is a problem with the user's problem and termination. This is a method of multi-program in this book processes the registerclass function:

IF (! registerclass (& wndclass))

{

MessageBox (Null, Text ("This Program Requires Windows NT!"),

Szappname, MB_ICONEROR);

Return 0;

}

Since MessageBoxW is one of several Unicode functions that can be performed in a Windows 98 environment, it is performed normally.

Of course, this program assumes that registerclass does not fail because of other reasons, such as the WndClass structure is set to NULL. GetLasteErrorphort will help you determine the cause of errors in this case. GetLastError is a Funwriting in Windows, which can get more error information when aphortical call fails. Different files will point out if you can get these information with getLastError. When you call RegisterClassW in Windows 98, getLastError will return 120. In WineError.h, you can see that the value 120 is equal to the identifier error_call_not_implement. You can also view errors in / Platform SDK / Windows Base Services / Debugging and Error Handling / Error Codes / System Errors - Numeric Order.

Some Windows program writers like to check the feedback values ​​of all-in-one-incorrect-in-one. This is really a bit, I believe you are also very accustomed to checking errors after configuring your memory. And many Windows functions need to configure memory. For example, RegisterClass needs to configure a memory to save information about the window category. As a result, you should check the execution of this function. On the other hand, if it does not get the required memory due to RegisterClass, it will declare the call failed, and Windows is probably changing.

In the paradigm of this book, I did the least wrong check. This is not because I think the error check is not a good way, but because this will make us in the examples.

Finally, an old experience is: In some Windows paradigm, you may see the following code in WinMain:

IF (! hprevinstance)

{

WNDCLASS.CBSTYLE = CS_HREDRAW | CS_VREDRAW;

Initialize other WNDCLASS

RegisterClass (& Wndclass);

}

This is the reason for "old difficulties". In the 16-bit Windows, if you start a new executive entity in the program being executed, WinMain's HPREVINSTANCE parameter will be the active entity code of the previous executive entity. In order to save memory, two or more executive entities may share the same window category. In this way, the window category is only registered when HPREVINSTANCE is NULL, indicating that there is no other implement entity.

HPREVINSTANCE is always NULL in 32-bit Windows. This program is executed normally, and it is actually not necessary to check HPREVINSTANCE. Create a window

The window category defines the general feature of the window, so you can build many different windows using the same window category. When you actually call CREATEWINDOW, you may specify more detailed information about the window.

Windows program newers sometimes confuse the difference between the window category and the window, and all features of a window cannot be set once. In fact, it is very convenient to separate these style information in this way. For example, all button windows can be established based on the same window category, and the window message processing related to this window category is located inside Windows. The window category is responsible for the keyboard and mouse input of the button, and defines the appearance image on the screen. From this point, all buttons work in the same way. But not all buttons are the same. They can have different sizes, different screen positions, and different strings. Some of these features later are part of the window definition instead of the window category.

The information passed to the RegisterClass function will be set in a data structure, and the information passed to the CreateWindow function will be set in a separate parameter in a file. Below is a CreateWindows call in Hellowin.c, and each field has made a complete description:

HWND = CREATEWINDOW (Szappname, // WINDOW CLASS NAME

TEXT ("The Hello Program"), // WINDOW CAPTION

WS_OVERLAPPEDWINDOW, // WINDOW STYLE

CW_USEDEFAULT, // Initial X position

CW_USEDEFAULT, // Initial Y position

CW_USEDEFAULT, // Initial X size

CW_USEDEFAULT, // Initial y size

Null, // Parent Window Handle

Null, // WINDOW MENU HANDLE

Hinstance, // Program Instance Handle

NULL); // CREATION Parameters

Here, I don't want to actually have the CreateWindowa function and the CreateWindowW. Two functions are processed by the first two parameters as ASCII or Unicode strings.

The parameters labeled "Window Class Name" are szappname, which contains strings "Hellowin" - this is the window category name registered. This is the way we built the window linkage.

The window established in this program is a normal overlapping window. It contains a title column. There is a system function table button on the left side of the title column. The title column is narrowed, amplified, and closed, and there is a border indicating the size of the window size. This is the window of the standard style, named WS_OVERLAPPEDWINDOW, which appears in the "Window Style" parameter of CreateWindow. If you look at Winuser.h, you will find that this style is a combination of several pivot flags:

#define ws_overlappedWindow (WS_OVERLAPTID | /

WS_CAPTION | /

WS_SYSMENU | /

WS_THICKFRAME | / WS_MINIMIZEBOX | /

WS_MAXIMIZEBOX)

The "Window Title" is the text displayed in the title column.

Note "Initial X Position" and "Initial Y Position" parameters specify the initial position of the upper left corner of the window with respect to the top left corner of the window. Since these parameters use CW_USEDEFAULT identification words, the Windows uses the overlapping window to use the overlapping window. (CW_USEDEFAULT is defined as 0x80000000.) In the case, Windows sequentially positions the newly established window, enhancing the vertical and horizontal distances in the upper left corner of the windows to increase on the screen. Similarly, the parameters of "Initial X Size" and "Initial Y Size" specify the width and height of the window, respectively. Similarly, CW_USEDEFAULT identification words are used, indicating that Windows wants Windows to use the internal size.

In establishing a "top-level" window, such as the application window, the comment is set to NULL as the "Father". Typically, if there is a parent child relationship between the window, the sub-window always appears above the parent window. The application window appears above the desktop window, but does not have to find the code of the desktop window for calling createWindow.

Because the window does not have a functional table, the "Window function table" is also set to NULL. "Program Execution Entity Code" is set to perform an entity code, which is passed to this program as a parameter of WinMain. Finally, the "establishment parameter" indicator is set to NULL, and you can use this parameter to access the information that may be referenced in later programs.

CREATEWINDOW Removes the code of the established window, which is stored in the variable hwnd, the latter is defined as the HWND type ("Window Code"). Each window in Windows has a code, and the program is used to use the window. Many windows functions need to use hwnd as a parameter, so Windows can know which window is for the function. If many windows have been established in a program, each window has a code. Windows code is one of the most important code handled by the Windows program.

Window

After the CreateWindow call back, this window has been established inside the Windows. That is to say, Windows has been configured with a memory that saves all information to specify the window in the CreateWindow call to specify the window, and Windows will find these information later according to the window code.

However, the light is like this, and the window does not appear on the video display. You also need two functions, one is:

ShowWindow (hwnd, icmdshow);

The first parameter is the window code that has just been established with CREATEWINDOW. The second parameter is ICMDSHOW transmitted to WinMain as a parameter. It determines how the window is initially displayed on the screen, which is generally small, minimized or maximized. The user may make the best choice when the installation program is installed in the function table. If the window is displayed in general size, then WinMain is received to showWindow after being transmitted. If the window is maximized, SW_SHOWMAXIMIZED. And if the window is only displayed on the work column, SW_SHOWMINNOACTIVE.

The showwindow function displays the window on the display. If the second parameter of ShowWindow is SW_SHOWNORMAL, the display area of ​​the window will be overwritten by the background drawing brush defined in the window category. Funny call

UpdateWindow (HWND);

Will redraw the display area. It does this by sending a WM_PAINT message via send to the window message processing program (ie, WndProc in Hellowin.c). Behind, we will explain how WndProc handles this message. Message round

After calling UpdateWindow, the window appears on the video display. The program must now be prepared to read the user's keyboard and mouse input. Windows maintains a "message 伫" for each Windows program that is currently executed. After the input event occurs, Windows converts the event to a "message" and put the message in the message.

The program takes the message from the message column by executing a program called "message back ring":

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

The MSG variable is the structure of the type MSG, and the type MSG is defined in Winuser.h as follows:

Typedef struct tagmsg

{

Hwnd hwnd;

Uint Message;

WPARAM WPARAM;

LParam Lparam;

DWORD TIME;

Point Pt;

}

MSG, * PMSG;

The Point data is also a structure, which is defined in WINDEF.H as follows:

TypedEf struct tagpoint

{

Long x;

Long Y;

}

Point, * ppoint;

The message recovery starts with a getMessage call, which removes a message from the message 伫 column:

GetMessage (& MSG, NULL, 0, 0)

This call is passed to the Windows indicator, pointing to the MSG structure named MSG. Second, third, third and fourth parameters are set to null or 0, indicating that the program receives all the messages of all the windows you build it yourself. Windows fills the various fields of the message structure with the next message removed from the message column, each of the fields of the structure, including:

HWND receives the window code of the message. In the Hellowin program, this parameter is the same as the HWnd value sent back by CreateWindow, because this is the only window owned by the program. Message message identification word. This is a value to identify the message. For each message, there is a corresponding identification word that defines the Windows header file (most of them in Winuser.h), starting with the header WM ("Window Message", Window message). For example, the user puts the mouse cursor within the Hellowin display area, and press the left button, Windows puts a message in the message column, which is equal to WM_LButtondown. This is a constant, its value is 0x0201. WPARAM A 32-bit "Message Parameter", its meaning and values ​​are different depending on the message. LPARAM A 32-bit message parameter, its value is related to the message. Time messages are placed in the message. PT messages are placed in a hosted hook.

As long as the Message field that removes the message from the message column is not WM_QUIT (its value is 0x0012), GetMessage will return a non-zero value. WM_Quit messages will cause getMessage to pass back 0.

Narrate

TranslateMessage (& MSG);

Pass the MSG structure to Windows to make some keyboard conversions. (About this, we will discuss in the sixth chapter.)

Narrate

DispatchMessage (& MSG);

Also post the MSG structure to Windows. Then, Windows sends the message to the appropriate window message processing, let it process. That is to say, Windows will call the window message processing program. In Hellown, this window message processing is a WndProe. After processing your message, WndProc is back to Windows. At this point, Windows also stays in the DispatchMessage call. After ending the DISPATCHMESSAGE call, Windows returns to Hellowin and then starts a message back from the next GetMessage call.

Window message processing program

The above we discussed the necessary burden: registered window categories, establish a window, then display the window on the screen, the program enters the message back, and then remands the message from the message column.

The actual action occurs in the window message processing program. The window message processing program determines what is displayed in the display area of ​​the window and how the window responds to the user input.

In Hellown, the window message processing program is a Voicies named WNDPROC. Window message processing program can be named (only conflicts with other names). A Windows program can contain multiple window messaging processes. A window message processing is always associated with a particular window category registered with the call registerclass. The CreateWindow function creates a window according to a particular window category. But according to a window category, multiple windows can be created.

Window messaging processes are always defined as the following form:

Lresult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM)

Note that the four parameters of the window messaging program are the same as the first four columns of the MSG structure. The first parameter hWnd is the code of the window that receives the message, which is the same as the return value of the CreateWindow function. This parameter is the unique viewing code known by the program with the HellowIn similar program (only one window). If the program is established in accordance with the same window category (also the same window message processing program), the HWND identifies a particular window that receives the message.

The second parameter is the same as the Message field in the MSG structure, which is the value of the identity message. The last two parameters are 32-bit message parameters, providing more information about the message. These parameters contain detailed information for each message. Sometimes the message parameter is two 16-bit metadses stored together, and sometimes the message parameter is a indicator of a string or data structure.

The program is usually not directly calling the window message processing, and the window message processing is usually called by the Windows itself. By calling the SendMessage function, the program can directly call its own window message processing program. We will discuss the SendMessage in the following chapter.

Handle message

Each message accepted by the window message processing program is identified by a value, that is, the Message parameter transmitted to the window message processing program. Windows Header Profile WinUser.h is defined for each message parameter with "WM" (Window message) identifier.

In general, Windows program writers use Switch and Case structures to determine what information is received by Window message processing, and how to properly process it. Window message processing must be transmitted to 0 when processing a message. All messages that are not processed by Windows messaging processes should be passed to a Windows Funel called DefWindowProc. The values ​​returned from the DefWindowProc must be submitted by the window message processing program.

In Hellown, WndProc only selects three messages: WM_CREATE, WM_PAINT and WM_DESTROY. The structure of the window message processing program is as follows:

Switch (IMSG)

{

Case WM_CREATE:

Handling WM_CREATE message

Return 0;

Case WM_Paint:

Handling WM_PAINT message

Return 0;

Case WM_DESTROY:

Handling WM_DESTROY message

Return 0;

}

Return DefWindowProc (HWND, IMSG, WPARAM, LPARAM);

This is important to call DEFWIndowProc to provide internal processing for all messages that do not process the window message processing. Otherwise, general actions, such as termination, will not be implemented normally.

Play the sound file

The first message received by the window message processing - is also the first message for WndProc selection processing - is WM_CREATE. WNDPROC receives this message when Windows processes the CREATEWINDOW in WinMain. That is to say, when hellowin calls CREATEWINDOW, Windows will do some work that it must do. In these work, Windows calls WNDPROC, set the first parameter to the window code, and the second parameter is set to WM_CREATE. WndProc processes WM_CREATE messages and transmits control back to Windows. Windows can then pass back from the CreateWindow call to Hellowin and continue in WinMain to perform the next process.

Typically, the window message processing program is initialized once during WM_CREATE processing. Hellowin plays a sound file called Hellion.wav in the process of this message. It uses a simple Plays and Sound letter to do this. The function description / Platform SDK / Graphics and Multimedia Services / Multimedia Audio / Waveform Audio in, and the file in / Platform SDK / Graphics and Multimedia Services / Multimedia Reference / Multimedia Functions in.

The first parameter of Plays is the name of the sound file (it may also be an alias in the Control Panel's Sounds, or a program resource). The second parameter is only used when the sound file is a kind of resource. The third parameter specifies some options. In this example, I specify the first parameter is a file name, and is not simultaneously playing the sound, ie the Plays and Sound call is immediately transferred immediately when the sound file starts playing, and will not wait for its completion. Under this method, the program can continue to initialize.

WNDPROC ends the processing of the entire WM_CREATE by transmitting 0 from the window message processing program.

WM_PAINT message

The second message processed by WndProc is WM_PAINT. This message is very important in Windows program. When a part of the window display area is displayed or all change to "invalid", it will be notified by this message notification.

How does the display of the display area become invalid? At the beginning of the window, the entire display area is invalid because the program has not been drawn on the window. The first WM_PAINT message (usually happens to call UpdateWindow in WinMain) Indicates that the window message processing is drawn on the display area.

After the user changed the size of the Hellowin window, the display area's display content is restarted. The reader should still remember that the style field of the Wndclass structure in Hellown is set to the flag CS_HREDRAW and CS_VREDRAW. Such format settings indicate Windows, and after the window size is changed, the entire window display content is invalid. Then, the window message processing will receive a WM_PAINT message. When the user minimizes Hellowin, then restore the window to the previous size, Windows will not save the contents of the display area. In a graphical environment, the amount of data involved in the window display area is very large. Therefore, the Windows command is invalid, and the window message processing is received a WM_PAINT message and automatically restores the content of its window.

When moving the window, when it overlaps it, Windows does not save the content covered by another window. After this part is no longer covered, it is marked as invalid. Window message processing receiving a WM_PAINT message to update the content of the window.

Processing for WM_PAIN is almost always starting from a beginpaint call:

HDC = BeginPaint (HWND, & PS);

The end of an endpaint call:

Endpaint (hwnd, & ps);

In these two calls, the first parameters are the program of the window, and the second parameter is the structural indicator of the type PAINTSTRUCT. The PAINTSTRUCT structure contains some window message processing, which can be used to update the contents of the display area. We will discuss each field of this structure in the next chapter. Now we only use it in the BeginPaint and Endpaint functions.

In the BeginPaint Call, if the background of the display area has not been deleted, it is removed by Windows. It uses the brush specified in the HBRBackground field of the Wndclass structure of the registration window category. In Hellown, this is a white backup brush. This means that Windows will delete the window background by setting the window background to white. BeginPaint Call For the entire display area, and transfer to a "device content code". The device content refers to an entity output device (e.g., a video display) and its device driver. The display area of ​​the window displays text and graphics requires the device content code. However, the content code that is transmitted back from BeGinPaint cannot draw in the display area, and the reader can try it. Endpaint release device content code to make it no longer valid.

If the Window message processing does not process the WM_Paint message (this is very rare), they must be transmitted to DefWindowProc. DefWindowProc only calls BeginPaint and EndPaint in turn to make the display area valid.

After the call is Beginpaint, WndProc then calls getClientRect:

GetClientRect (hwnd, & review);

The first parameter is the code of the program window. The second parameter is an indicator, pointing to the Rectangle structure of a RECT type. This structure has four long fields, which are left, Top, Right, and Bottom. GetClientRect Sets the four fields to the size of the window display area. The LEFT and TOP fields are typically set to 0, the Right and the Bottom field are set to the width and height of the display area (the number points).

In addition to passing the Rect structure indicator as the fourth parameter of DrawText, WndProc is no longer processed by it:

DrawText (HDC, Text ("Hello, Windows 98!"), -1, & review,

DT_SINGLINE | DT_CENTER | DT_VCENTER; DRAWText can output text (as indicated by its name). Since the function is to output text, the first parameter is the device content code (the second parameter is the text you want to output, the third parameter is -1, the indicator string is ending of.

The last parameter of DrawText is a series of bitmarks, which are all defined in Winuser.h (although due to its display output, it makes DrawText call like a GDI card, but it does become User because of the premium drawing function. Part of the module. This one is explained in / platform SDK / GRAPHICS AND MultiMedia Services / GDI / FONTS AND TEXT). The flag indicates that the text must be displayed on one line, the horizontal direction and the vertical direction are located in the rectangular center specified by the fourth parameter. Therefore, this functions will make the string "Hello, Windows 98!" Is displayed in the center of the display area.

Once the display area becomes invalid (as in the case where the size of the occurs) is changed, WndProc receives a new WM_PAINT message. WNDPROC has changed the window size after the call getClientRect and the text is displayed in the center of the new window.

WM_DESTROY message

The WM_DESTROY message is another important message. This message indicates that Windows is close to the window according to the user's instruction. This message is that the user clicks on the Close button or the selected Close in the program (behind this chapter, we will discuss how the WM_DESTROY message takes effect).

Hellowin responds to WM_DESTROY messages by call PostquitMessage:

PostquitMessage (0);

This card is inserted into a WM_Quit message in the program's message. As mentioned earlier, GetMessage transmits non-0 value for all messages removed from the message column other than WM_quit. When getMessage gets a WM_QUIT message, it passes back 0. This will cause WinMain to quit the message ring and terminate the program. Then the proceedings perform the following description:

Return msg.wparam;

The WPARAM field of the structure is the value passed to the PostquitMessage function (usually 0). The RETURN will then exit WinMain and terminate the program.

The difficulty of Windows program is difficult, even with Hellowin's instructions, the reader's structure and principles may still feel mysterious. When writing a simple C program for the traditional environment, the entire program may be included in the main function. In Hellowin, WinMain only contains registration window categories, establishs a window, and takes a message from the message column and the program code must be sent. All actual actions of the program occur in the window message processing program. In Hellown, these do not have many, WndProc simply broadcasts a sound file and displays a string in the window. However, in the following chapters, the reader will find everything made by the Windows program, is a message that responds to the window message processing program. This is one of the concepts of conceptual difficulties. Before you start writing a Windows program, you must first figure it out. Don't call me, I will call you before, and the program writer is familiar with the practice of using the operating system call. For example, the C programmaker opens the file using the FOPEN. The FOPEN-in-one is ultimately opened by calling job systems. This problem is not. However, Windows is different, although Windows has more than 1,000 functions available, but Windows also calls the user, such as the window message processing program WndProc defined earlier. Window message processing is related to the window category, the window category is registered for the program call registerclass. The window established by this category uses this window message processing to process all messages for the window. Windows sends a message to the window via a call window message. When the window is established, Windows calls WNDPROC. Windows also calls WNDPROC when Windows is closed. When the window changes the size, movement, or turns into a graphic, select a project from the function table, move the rolling column, press the mouse button or enter the word or from the keyboard, and the window display area must be updated, Windows must be updated. Call WnProc. All of these WNDPROC calls are done in the form of a message. In most Windows programs, the main part of the program is used to process messages. Windows can be sent to the window message handling program that is usually identified by the name of the WM, and is defined in the Winuser.h header file. In fact, from the common formula of the program, it is not unsenerated in the traditional programming. The Signal in the CTRL-C can intercept other interrupts of the Ctrl-C interrupt or work system. A program code that intercends the hardware interruption in the old program written for MS-DOS. But in Windows, this concept is extended to include all events. Everything that happens in the window is transmitted to the window message processing program in the form of a message. Then, the window messaging process responds to this message in some way, or transmits the message to the DEFWINDOWPROC to perform the internal processing. In Hellowin, the Window Message Processing WPARAM and LPARAM parameters do not have other uses in addition to parameters passed to DefWindowProc. These parameters give the other information about the message, the meaning of the parameters is related to the specific message. Let's take an example. Once the display area of ​​the window has changed, Windows calls the window message processing program of the window. The HWND parameter of the window message processing is the code that changes the size of the window (remember, a window message processing program can handle the message of multiple windows established based on the same window category. Parameter hwnd Let the window message processing program know which window At receiving a message). Parameters Message is WM_SIZE. The value of the message WM_SIZE WPARAM is SIZE_RESTORED, SIZE_MINIMIZED, SIZE_MAXIMIZED, SIZE_MAXSHOW, or SIZE_MAXHIDE (in the Winuser.h header file, respectively, numbers 0 to 4).

That is, the parameter WPARAM indicates that the window is not minimized or not maximized, and it is minimized, maximized, or hidden. The LPARAM parameter contains the size of the new window, the new width, and the new height are 16-bit metades, together with a 32-bit LPARAM. WINDEF.H provides a huge set of help from LPARAM from LPARAM in WINDEF.H, we will explain this gauge in the next chapter. Sometimes, DEFWINDOWPROC will generate additional messages after processing the message. For example, suppose the user performs Hellown, and the user finally clicks on the Close button, or assumes that the keyboard or humei is selected from the system function table, DefWindowProc processes this keyboard or mouse input, in detecting the user selection After the close option, it sends a WM_SYSCOMMAND message to the window message processing. WndProc passes this message to DefWindowProc. DefWindowProc Sends a WM_CLOSE message to the window message processing. WndProc will pass it again to DefWindowProc. DestroyWindow calls DestroyWindow to respond to this WM_CLOSE message. DESTROYWINDOW causes Windows to send a WM_DESTROY message to the window message processing. WndProc calls PostquitMessage, putting a WM_QUIT message into the message column, and responds to this message. This message causes the message recovery in WinMain to terminate, and then the process ends. Grunge message and non-rumored messages we have already talked, Windows sends a message to the window, which means the Windows call window message processing program. However, the Windows program also has a message back, and it calls GetMessage to remove the message from the message column, and call DispatchMessage to send the message to the window message processing program. Then, the Windows program is waiting for the message (similar to the same keyboard input in the normal), then send the message to a certain place? Or, is it directly received from the outside? In fact, both situations exist. The message can be divided into "伫 伫" and "non-rightened". The 伫 伫 的 is placed in the WINDOWS. In the post-style message ring, retrans back and assigned to the window message processing program. Non-rollerized messages are sent directly to the Windows messaging program when Windows calls window. That is to say, the 伫 伫 给 给 伫 给 伫In any case, the window message processing program will get all the information of the window - including the geminized and non-rightened. Window message processing is the "Message Center" of the window. The 伫 伫 message is basically the result of the user input to hit the key (such as WM_KEYDOWN and WM_KEYUP messages), the font generated by the keystroke (WM_CHAR), the mouse movement (WM_MOUSEMOVE) and the mouse button (WM_LButtondown) Out. The 伫 伫 message also includes clock messages (WM_TIMER), update messages (WM_PAINT) and exiting messages (WM_QUIT). Non-intensive messages are other messages. In many cases, non-rightened messages come from call specific Windows functions. For example, when WinMain calls CREATEWINDOW, Windows will create a window and send a WM_CREATE message to the window message processing in the process. When WinMain calls ShowWindow, Windows will send WM_SIZE and WM_SHOWWINDOW messages to the window message processing.

When WinMain calls UpdateWindow, Windows will send a WM_PAINT message to the window message processing. The 伫 伫 message signal sent when the keyboard or mouse input is input, can also appear in the non-lined message. For example, when selecting a functional entry with a keyboard or a mouse, the keyboard or mouse message is to gently, and the WM_COMMAND message selected by the function table item may be non-generic. This process is obviously complicated, but fortunately, most of them is solved by Windows, not our program. From the perspective of the window message processing program, these messages are in an ordered, synchronous manner. Window message processing program can handle them or do not process. When I said that the message is in an orderly manner, I said that first, the message is different from the hardware interrupt. When processing a message in a window message processing program, the program will not be suddenly interrupted by other messages. Although the Windows program can perform multi-performing, each executed message is column only for the window message processing, the window processing message executed in this execution. In other words, the message back ring and the window message processing program are not executed concurrent. When a message reciprocity receives a message from its message, then call DispatchMessage to send the message to the window message processing, until the window message processing will be transmitted back to Windows, DispatchMessage ends the execution. Of course, the window message processing program can call the window message processing to send another message. At this time, the window message processing must complete the processing of the second message before the word call back. At that time the window message processing program will process the original message. For example, when the window program calls UpdateWindow, Windows will call the Window message processing to process the WM_PAINT message. After the Window Message Processing Processing WM_PAINT message is completed, the UpdateWindow call will transfer the control back to the window message processing program. This is to say that the window message processing must be re-entry. In most cases, this will not bring problems, but program writers should realize this. For example, assume that you set a static variable when you process a message in the Window message processing program, and then call a Windows Verifier. When you transfer this card, can you guarantee the value of the variable or the original? It is difficult to say - it is likely that your call is another message, and the window message processing changes the value of the variable when processing this message. This is also one of the reasons why some compiling optimization options must be turned off when compiling the Windows program. In many cases, the window message processing must save it from the message and use this information when processing another message. These information can be stored in static variables or overall variables of the window. Of course, readers will have a more clear understanding of the following chapters because the window message processing will process more messages. Action Rapid Windows 98 and Windows NT are all preferred multi-workers. This means that when a program is working for a long time, Windows can allow the user to switch to another. This is a good thing, and now the current Windows exceeded the previous 16-bit Windows place. However, this priority multi-worker does not always work with what you want due to Windows designs. For example, suppose your program takes a minute to handle a message. Yes, the user can switch control to another, but it is not possible to make any action on your program. Users cannot move your program window, zoom it, minimize, close it, and nothing. This is because your window message processing program is busy working with a long time. The surface is not a window message processing program in the execution of its own movement and zoom operation, but it is actually it is doing it. This is the work of the DefWindowProc, which must be considered part of your window message processing program.

转载请注明原文地址:https://www.9cbs.com/read-127586.html

New Post(0)