windows程序设计 处理消息

来源:互联网 发布:js 不等于undefined 编辑:程序博客网 时间:2024/05/01 09:50

Processing the Messages

Generally,Windows programmers use a switchand caseconstruction to determine what messagethe window procedure is receiving and how to process it accordingly. When awindow procedure processes a message, it should return 0 from the windowprocedure. All messages that a window procedure chooses not to process must bepassed to a Windows function named DefWindowProc. The value returned fromDefWindowProcmust be returned from the window procedure.

In HELLOWIN,WndProcchooses to process only three messages: WM_CREATE, WM_PAINT, andWM_DESTROY.

The windowprocedure is structured like this:

switch (iMsg)

{

case WM_CREATE :

[process WM_CREATE message]

return 0 ;

case WM_PAINT :

[process WM_PAINT message]

return 0 ;

case WM_DESTROY :

[process WM_DESTROY message]

return 0 ;

}

return DefWindowProc (hwnd, iMsg,wParam, lParam) ;

Playing a Sound File

The veryfirst message that a window procedure receives—and the first that HELLOWIN'sWndProcchooses to process—is WM_CREATE. WndProcreceives this message whileWindows is processing the CreateWindowfunction in WinMain. That is, whenHELLOWIN calls CreateWindow, Windows does what it has to do and, in theprocess, Windows calls WndProcwith the first argument set to the window handle andthe second argument set to WM_CREATE (the value 1). WndProcprocesses theWM_CREATE message and returns controls back to Windows. Windows can then returnto HELLOWIN from the CreateWindowcall to continue further progress in WinMain.

The firstargument to PlaySoundis the name of a waveform file. (It could also be a soundalias name defined in the Sounds section of the Control Panel or a programresource.) The second argument is used only if the sound file is a resource.The third argument specifies a couple of options. In this case, I've indicatedthat the first argument is a filename and that the sound is to be playedasynchronously—that is, the PlaySoundfunction call is to return as soon as thesound file starts playing without waiting for it to complete. That way theprogram can continue with its initialization.

The WM_PAINT Message

The secondmessage that WndProcprocesses is WM_PAINT. This message is extremely importantin Windows programming. It informs a program when part or all of the window'sclient area is "invalid" and must be "updated," which meansthat it must be redrawn or "painted."

WM_PAINTprocessing almost always begins with a call to BeginPaint:

hdc = BeginPaint (hwnd, &ps) ;

and endswith a call to EndPaint:

EndPaint (hwnd, &ps) ;

In bothcases, the first argument is a handle to the program's window, and the secondargument is a pointer to a structure of type PAINTSTRUCT. The PAINTSTRUCTstructure contains some information that a window procedure can use forpainting the client area. I'll discuss the fields of this structure in the nextchapter; for now, we'll just use it in the BeginPaintand EndPaintfunctions.

TheBeginPaintcall validates the entire client area and returns a "handle to adevice context." A device context refers to a physical output device (suchas a video display) and its device driver. You need the device context handleto display text and graphics in the client area of a window. Using the devicecontext handle returned from BeginPaint, you cannot draw outside the clientarea, even if you try. EndPaintreleases the device context handle so that it isno longer valid.

AfterWndProccalls BeginPaint, it calls GetClientRect:

GetClientRect (hwnd, &rect) ;

The firstargument is the handle to the program's window. The second argument is apointer to a rectangle structure of type RECT. This structure has four LONGfields named left, top, right, and bottom. The GetClientRectfunction sets thesefour fields to the dimensions of the client area of the window.The leftand top fields are always set to 0. Thus, therightand bottomfields represent the width and height of the client area in pixels.

DrawText, asthe name implies, draws text. Because this function draws something, the firstargument is a handle to the device context returned from BeginPaint. The secondargument is the text to draw, and the third argument is set to -1 to indicatethat the text string is terminated with a zero character.

Whenever theclient area becomes invalid (as it does when you change the size of thewindow), WndProcreceives a new WM_PAINT message. WndProcobtains the updatedwindow size by calling GetClientRectand again displays  the text in the next center of the window.

The WM_DESTROY Message

HELLOWINresponds to the WM_DESTROY message in a standard way by calling

PostQuitMessage (0) ;

returnmsg.wParam ;

Don't Call Me, I'll Call You

ThewParamfield of the structure is the value passed to the PostQuitMessagefunction(generally 0). The return statement exits from WinMainand terminates theprogram. The wParamand lParamparameters to the window procedure are not used inHELLOWIN except as parameters to DefWindowProc. These parameters give thewindow procedure additional information about the message. The meaning of theparameters is message-dependent.

Let's lookat an example. Whenever the client area of a window changes in size, Windowscalls that window's window procedure. The hwndparameter to the window procedureis the handle of the window changing in size. (Remember that one windowprocedure could be handling messages for multiple windows that were createdbased on the same window class. The hwndparameter lets the window procedure knowwhich window is receiving the message.) The messageparameter is WM_SIZE. ThewParamparameter for a WM_SIZE message is the value SIZE_RESTORED,SIZE_MINIMIZED, SIZE_MAXIMIZED, SIZE_MAXSHOW, or SIZE_MAXHIDE (defined in the WINUSER.Hheader file as the numbers 0 through 4). That is, the wParamparameter indicateswhether the window is being changed to a nonminimized or nonmaximized size,being minimized, being maximized, or being hidden.

ThelParamparameter contains the new size of the window. The new width (a 16-bitvalue) and the new height (a 16-bit value) are stuck together in the 32-bitlParam. The WINDEF.H header file defines some handy macros that help youextract these two values from lParam. We'll do this in the next chapter.

Queued and Nonqueued Messages

The queuedmessages are primarily those that result from user input in the form of keystrokes(such as the WM_KEYDOWN and WM_KEYUP messages), characters that result fromkeystrokes (WM_CHAR), mouse movement (WM_MOUSEMOVE), and mouse-button clicks(WM_LBUTTONDOWN). Queued messages also include the timer message (WM_TIMER),the repaint message (WM_PAINT), and the quit message (WM_QUIT).

Thenonqueued messages are everything else. Nonqueued messages often result fromcalling certain Windows functions. For example, when WinMaincalls CreateWindow,Windows creates the window and in the process sends the window procedure aWM_CREATE message. When WinMaincalls ShowWindow, Windows sends the window procedureWM_SIZE and WM_SHOWWINDOW messages. When WinMaincalls UpdateWindow, Windowssends the window procedure a WM_PAINT message. Queued messages signalingkeyboard or mouse input can also result in nonqueued messages. For example,when you select a menu item with the keyboard or mouse, the keyboard or mousemessage is queued but the eventual WM_COMMAND message indicating that a menuitem has been selected is nonqueued.

In manycases, the window procedure must retain information it obtains in one messageand use it while processing another message. This information must be saved invariables defined as staticin the window procedure, or saved in globalvariables.

Of course,you'll get a much better feel for all of this in later chapters as the windowprocedures are expanded to process more messages.


0 0
原创粉丝点击