windows程序设计 消息循环

The Message Loop

A programretrieves these messages from the message queue by executing a block of codeknown as the "message loop":

while (GetMessage (&msg, NULL, 0,0))

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

Themsgvariable is a structure of type MSG, which is defined in the WINUSER.Hheader file like this:

typedef struct tagMSG

{

HWND hwnd ;

UINT message ;

WPARAM wParam ;

LPARAM lParam ;

DWORD time ;

POINT pt ;

}

MSG, * PMSG ;

The POINTdata type is yet another structure, defined in the WINDEF.H header file likethis:

typedef struct tagPOINT

{

LONG x ;

LONG y ;

}

POINT, * PPOINT;

TheGetMessagecall that begins the message loop retrieves a message from themessage queue:

GetMessage (&msg, NULL, 0, 0)

This callpasses to Windows a pointer to a MSG structure named msg. The second, third,and fourth arguments

are set toNULL or 0 to indicate that the program wants all messages for all windowscreated by the program.

Windowsfills in the fields of the message structure with the next message from themessage queue. The fields of

thisstructure are:

hwnd Thehandle to the window which the message is directed to. In the HELLOWIN program,this is thesame as the hwndvalue returned from CreateWindow, because that's theonly window the program has.

message Themessage identifier. This is a number that identifies the message. For eachmessage, there is a corresponding identifier defined in the Windows headerfiles (most of them in WINUSER.H) that begins with the identifier WM("window message"). For example, if you position the mouse pointerover HELLOWIN's client area and press the left mouse button, Windows will put amessage in the message queue with a messagefield equal to WM_LBUTTONDOWN, whichis the value 0x0201.

wParam A32-bit "message parameter," the meaning and value of which depend onthe particular message.

lParamAnother 32-bit message parameter dependent on the message. time  The time the message was placed in themessage queue.

If themessagefield of the message retrieved from the message queue is anything exceptWM_QUIT (which equals 0x0012), GetMessagereturns a nonzero value. A WM_QUITmessage causes GetMessageto return 0.

Thestatement:

TranslateMessage (&msg) ;

passes themsgstructure back to Windows for some keyboard translation. (I'll discuss thismore in Chapter 6.)

Thestatement

DispatchMessage (&msg) ;

again passesthe msgstructure back to Windows. Windows then sends the message to theappropriate window procedure for processing. What this means is that Windowscalls the window procedure. In HELLOWIN, the window procedure is WndProc. AfterWndProcprocesses the message, it returns control to Windows, which is still servicingthe DispatchMessagecall. When Windows returns to HELLOWIN following theDispatchMessagecall, the message loop continues with the next GetMessagecall.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值