通过调用InserMenu,我给记事本添加了一个菜单项,参考下面的代码,用全局钩子来截取菜单点击消息,我参考了下面的代码,希望能在菜单点击时候响应我的一组操作,但是发现下面的代码中如果把WM_MENUSELECT 换成WM_COMMAND程序就不会响应,结果变成了鼠标移到到新加的菜单上就执行那组操作,我该怎么才能让那个菜单点击时执行我想要的操作?请大家帮忙,谢谢!
const 
 CM_MENUSELECT = WM_USER + 800; type 
 TForm1 = class(TForm) 
   ... 
 private 
   { Private declarations } 
   procedure CMMenuSelect(var Message: TMessage); message CM_MENUSELECT; 
var 
 MsgHook: HHook; 
 MenuItemText: string; function GetMenuItemText(Menu: HMENU; MousePt: TPoint): string; 
var 
 ID: Integer; 
 Info: TMenuItemInfo; 
 S: array[0..255] of Char; 
begin 
 ID := Integer(MenuItemFromPoint(WindowFromPoint(MousePt), Menu, MousePt)); 
 FillChar(Info, SizeOf(Info), 0); 
 Info.cbSize := SizeOf(Info); 
 Info.fMask := MIIM_TYPE; 
 Info.dwTypeData := S; 
 Info.cch := 255; 
 if not GetMenuItemInfo(Menu, ID, True, Info) or (Info.cch = 0) then 
   Result := '' 
 else 
   Result := S; 
end; function MsgHookCallback(Code: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall; 
var 
 P: PCWPSTRUCT; 
 Flags: Word; 
 Menu: HMENU; 
 MousePt: TPoint; 
begin 
 if Code = HC_ACTION then 
 begin 
   P := PCWPSTRUCT(lParam); 
   if P.message = WM_MENUSELECT then 
   begin 
     Flags := HIWORD(P.wParam); 
     Menu := P.lParam; 
     if (Menu <> 0) and (Flags and MF_MOUSESELECT <> 0) then 
     begin 
       GetCursorPos(MousePt); 
       MenuItemText := GetMenuItemText(Menu, MousePt); 
       PostMessage(Form1.Handle, CM_MENUSELECT, Menu, MakeLong(MousePt.X, MousePt.Y)); 
     end; 
   end; 
 end; 
 Result := CallNextHookEx(MsgHook, Code, WParam, LParam); 
end; procedure TForm1.N4Click(Sender: TObject); 
begin 
 if MsgHook = 0 then 
   MsgHook := SetWindowsHookEx(WH_CALLWNDPROC, @MsgHookCallback, HInstance, GetCurrentThreadID); 
end; procedure TForm1.N5Click(Sender: TObject); 
begin 
 if MsgHook <> 0 then 
 begin 
   UnhookWindowsHookEx(MsgHook); 
   MsgHook := 0; 
 end; 
end; procedure TForm1.CMMenuSelect(var Message: TMessage); 
begin 
 Memo1.Lines.Add(Format('菜单项:%s 菜单句柄:%.8x 鼠标位置:(%d, %d)', 
   [MenuItemText, Message.WParam, LOWORD(Message.LParam), HIWORD(Message.LParam)])); 
end;