(接上篇, 窗口消息(2))
4 菜单消息
动作:鼠标左键选中一个主菜单,然后移动到某个子菜单项,然后点击它。消息序列如下:
第一部分:显示菜单
<00518>
000403C
0 P WM_NCMOUSEMOVE nHittest:HTMENU xPos:162 yPos:164
<00519>
000403C
0
S WM_NCHITTEST
xPos:162 yPos:164
<00520>
000403C
0 R WM_NCHITTEST nHittest:HTMENU
<00521>
000403C
0
S WM_SETCURSOR
hwnd:
000403C
0 nHittest:HTMENU wMouseMsg:WM_LBUTTONDOWN
<00522>
000403C
0 R WM_SETCURSOR fHaltProcessing:False
<00523>
000403C
0 P WM_NCLBUTTONDOWN nHittest:HTMENU xPos:162 yPos:164
<00524>
000403C
0
S WM_SYSCOMMAND
uCmdType:SC_MOUSEMENU xPos:162 yPos:164
<00525>
000403C
0
S WM_ENTERMENULOOP
fIsTrackPopupMenu:False
<00526>
000403C
0 R WM_ENTERMENULOOP
<00527>
000403C
0
S WM_SETCURSOR
hwnd:
000403C
0 nHittest:HTCAPTION wMouseMsg:0000
<00528>
000403C
0 R WM_SETCURSOR fHaltProcessing:False
<00529>
000403C
0
S WM_INITMENU
hmenuInit:
000503C
2
<00530>
000403C
0 R WM_INITMENU
<00531>
000403C
0
S WM_MENUSELECT
uItem:2 fuFlags:MF_POPUP | MF_HILITE | MF_MOUSESELECT hmenu:
000503C
2
<00532>
000403C
0 R WM_MENUSELECT
<00533>
000403C
0
S WM_INITMENUPOPUP
hmenuPopup:
000302F
8 uPos:2 fSystemMenu:False
<00534>
000403C
0 R WM_INITMENUPOPUP
<00535>
000403C
0 P WM_NCMOUSELEAVE
<00536>
000403C
0 P WM_MOUSEMOVE fwKeys:MK_LBUTTON xPos:162 yPos:164
<00537>
000403C
0
S WM_ENTERIDLE
fuSource:MSGF_MENU hwnd:
0004065A
<00538>
000403C
0
S WM_SETMESSAGESTRING
wStringID:00000000 lpszString:00000000
<00539>
000403C
0 R WM_SETMESSAGESTRING
<00540>
000403C
0 R WM_ENTERIDLE
<00541>
000403C
0 P WM_LBUTTONUP fwKeys:0000 xPos:162 yPos:164
其中,鼠标移动到位置后,左键按下,WM_NCLBUTTONDOWN消息产生-->WM_SYSCOMMAND产生时,其Cmd类型为SC_MOUSEMENU(在前面的消息序列中,已经出现过SC_CLOSE和SC_SIZE)。WM_ENTERMENULOOP表示进入菜单模式循环。如果是右键菜单(使用TrackPopupMenu),其wParam参数为TRUE。-->WM_INITMENU在主菜单项将要变成活动时产生(移动鼠标,或者使用菜单键选择),且在菜单循环中只发生一次,其中hmenuInit为主菜单的句柄。-->WM_MENUSELECT在用户按下鼠标左键,选择该主菜单项时发生,其中hmenu为主菜单句柄, 其它参数表明了菜单项的位置和状态。-->WM_NCMOUSELEAVE表示鼠标离开非客户区(?可是现在鼠标显然没有离开,此时,鼠标应该被当前窗口Capture了)。-->然后是WM_SETMESSAGESTRING(该消息为MFC内部消息,用来向状态条更新字符串信息。CFrameWnd类提供了OnSetMessageString响应该消息,另外,还提供了GetMessageString虚方法,可以改变字符串的内容)-->鼠标左键抬起(这是一个客户区鼠标消息,可从坐标上看,又同WM_NCLBUTTONDOWN发生时的位置一样.这个坐标应该是相对窗口左上角的。因为被Capture了,后面将会看到release capture)
第二部分:在主菜单中移动 (重复下面的消息)
<00584>
000403C
0
S WM_ENTERIDLE
fuSource:MSGF_MENU hwnd:
0004065A
<00585>
000403C
0 R WM_ENTERIDLE
<00586>
000403C
0 P WM_MOUSEMOVE fwKeys:0000 xPos:162 yPos:179
第三部分:选择子菜单项
<00587>
000403C
0
S WM_MENUSELECT
uItem:59392 fuFlags:MF_HILITE | MF_MOUSESELECT hmenu:
000302F
8
<00588>
000403C
0 R WM_MENUSELECT
<00589>
000403C
0
S WM_ENTERIDLE
fuSource:MSGF_MENU hwnd:
0004065A
<00590>
000403C
0
S WM_SETMESSAGESTRING
wStringID:0000E800 lpszString:00000000
<00591>
000403C
0 R WM_SETMESSAGESTRING
<00592>
000403C
0 R WM_ENTERIDLE
<00593>
000403C
0 P WM_MOUSEMOVE fwKeys:0000 xPos:162 yPos:180
<00594>
000403C
0
S WM_ENTERIDLE
fuSource:MSGF_MENU hwnd:
0004065A
<00595>
000403C
0 R WM_ENTERIDLE
(当鼠标在子菜单项上移动时,会重复<00593>~<00595>)
其中,基本同上面主菜单项选择时的消息序列,只是参数不同而已。WM_MENUSELECT中的hmenu为popup菜单的句柄,59392为选择的菜单项ID。WM_SETMESSAGESTRING中的wStringID也为59392.
第四部分,点击子菜单项
<00611>
000403C
0 P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:162 yPos:186
<00612>
000403C
0
S WM_ENTERIDLE
fuSource:MSGF_MENU hwnd:
0004065A
<00613>
000403C
0 R WM_ENTERIDLE
<00614>
000403C
0 P WM_LBUTTONUP fwKeys:0000 xPos:162 yPos:186
<00615>
000403C
0
S WM_UNINITMENUPOPUP
<00616>
000403C
0 R WM_UNINITMENUPOPUP
<00617>
000403C
0
S WM_CAPTURECHANGED
hwndNewCapture:00000000
<00618>
000403C
0 R WM_CAPTURECHANGED
<00619>
000403C
0
S WM_MENUSELECT
uItem:0 fuFlags:FFFF (menu was closed) hmenu:00000000
<00620>
000403C
0
S WM_SETMESSAGESTRING
wStringID:0000E001 lpszString:00000000
<00621>
000403C
0 R WM_SETMESSAGESTRING
<00622>
000403C
0 R WM_MENUSELECT
<00623>
000403C
0
S WM_EXITMENULOOP
fIsTrackPopupMenu:False
<00624>
000403C
0 R WM_EXITMENULOOP
<00625>
000403C
0 R WM_SYSCOMMAND
<00626>
000403C
0 P WM_COMMAND wNotifyCode:0 (sent from a menu) wID:59392
其中,WM_LBUTTONDOWN表明鼠标左键按下。-->抬起后,POPUP菜单消失
(WM_UNINITMENUPOPUP),接着,鼠标的Capture失去.-->WM_MENUSELECT的参数
说明了popup menu消失-->接着,WM_SETMESSAGESTRING的wStringID参数转化为
十进制为57345,它是MFC中的AFX_IDS_IDLEMESSAGE宏(就绪)(状态条上显示的字样)。-->接着,
退出菜单循环(WM_EXITMENULOOP)-->最后,系统产生59392菜单项的WM_COMMAND消息。