鼠标按下不知道,不过在WM_MENUSELECT消息中倒是可以捕捉到按下的菜单<br>在WH_CALLWNDPROCRET类型的HOOK中捕捉<br>下边是我以前学习写的一段代码,你参考一下<br><br>type<br> TGoData = record<br> TargetWnd : Hwnd;<br> AppHwnd : THandle;<br> ghHook : HHook;<br><br> HM_Selected: THandle;<br> Send: Boolean;<br><br> SLD_WM_SEND: Cardinal;<br><br> SLD_WM_NEW: Cardinal;<br> SLD_WM_OPEN: Cardinal;<br> SLD_WM_SAVE: Cardinal;<br> SLD_WM_QUIT: Cardinal;<br><br> HM_FILE: THandle;<br> HM_NEW: THandle;<br> HM_OPEN: THandle;<br> HM_SAVE: THandle;<br> HM_QUIT: THandle;<br> end;<br> PGoData = ^TGoData;<br>var<br> GoData: PGoData;<br> MemFile : THandle;<br> aStr: String;<br><br>procedure InitialMenu(HD_APP: THandle);<br>var<br> hm,phm: THandle;<br>begin<br> //注册系统消息<br> GoData^.SLD_WM_NEW :=RegisterWindowMessage('SLD_WM_NEW');<br> GoData^.SLD_WM_OPEN :=RegisterWindowMessage('SLD_WM_OPEN');<br> GoData^.SLD_WM_SAVE :=RegisterWindowMessage('SLD_WM_SAVE');<br> GoData^.SLD_WM_QUIT :=RegisterWindowMessage('SLD_WM_QUIT');<br> //添加菜单并获取各菜单标志<br> //文件菜单<br> phm :=CreateMenu;<br> GoData^.HM_FILE :=phm;<br> AppendMenu(phm,MF_STRING,phm+1,'新建 Ctrl+N');<br> GoData^.HM_NEW :=Lo(phm+1);<br> AppendMenu(phm,MF_STRING,phm+2,'打开 Ctrl+O');<br> GoData^.HM_OPEN :=Lo(phm+2);<br> AppendMenu(phm,MF_STRING,phm+3,'关闭');<br> GoData^.HM_SAVE :=Lo(phm+3);<br> AppendMenu(phm,MF_SEPARATOR,0,'-');<br> AppendMenu(phm,MF_STRING,phm+4,'退出');<br> GoData^.HM_QUIT :=Lo(phm+4);<br><br> hm :=GetMenu(HD_APP);<br> AppendMenu(hm,MF_POPUP,phm,'文件');<br>end;<br><br>//内存映射<br>procedure IntoShare; stdcall;export;<br>begin<br> MemFile := OpenFileMapping( FILE_MAP_WRITE, False, 'JJYY' );<br> if MemFile = 0 then<br> MemFile:=CreateFileMapping( $FFFFFFFF, nil,<br> PAGE_READWRITE, 0, SizeOf( TGoData ), 'JJYY');<br> GoData := MapViewOfFile( MemFile, FILE_MAP_WRITE, 0, 0, 0 );<br> if MemFile = 0 then<br> FillChar( GoData^, SizeOf( TGoData ),0);<br> SetLength(aStr,255);<br>end;<br><br>procedure DllParamSet(hw: Hwnd; hws: THandle);stdcall; export;<br>var<br> phm,hm: Hmenu;<br>begin<br> GoData^.AppHwnd :=hw;<br> GOData^.TargetWnd :=hws;<br> InitialMenu(hws);<br>end;<br><br>function MouseHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;<br>var<br> cpw: CWPRETSTRUCT;<br> i: Cardinal;<br>begin<br> cpw :=PCWPRETSTRUCT(lparam)^;<br> Result := 0;<br> If iCode < 0 Then<br> begin<br> Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br> Exit;<br> end;<br><br> if (cpw.hwnd = GoData^.TargetWnd) or (IsChild(GoData^.TargetWnd,cpw.hwnd)) then<br> begin<br> Result :=1;<br> if cpw.message = WM_MENUSELECT then<br> begin<br> With GoData^ do<br> begin<br> Send :=False;<br> if cpw.lParam = 0 then Exit;<br> HM_Selected :=cpw.lParam;<br>// ShowMessage(IntToStr(cpw.lParam));<br> i :=Lo(cpw.wParam);<br>// if i=Lo(cpw.lParam) then Exit;<br> SLD_WM_SEND :=0;<br> if HM_FILE = cpw.lParam then<br> begin<br> if i in [HM_NEW,HM_OPEN,HM_SAVE,HM_QUIT] then<br> begin<br> SEND :=true;<br> end<br> else<br> begin<br> SEND :=false;<br> end;<br> if HM_NEW=i then<br> SLD_WM_SEND :=SLD_WM_NEW<br> else if HM_OPEN=i then<br> SLD_WM_SEND :=SLD_WM_OPEN<br> else if HM_SAVE=i then<br> SLD_WM_SEND :=SLD_WM_SAVE<br> else if HM_QUIT=i then<br> SLD_WM_SEND :=SLD_WM_QUIT;<br> end;<br> end;<br> end;<br> if cpw.message = WM_SYSCOMMAND then<br> begin<br> PostMessage(GoData^.AppHwnd,GoData^.SLD_WM_SEND,0,0);<br> end;<br> end;<br>// result :=callnexthookex(0,icode,wparam,lparam);<br>end;<br><br>function EnableMouseHook: BOOL; export;<br>begin<br> Result := False;<br> if hNextHookProc <> 0 then Exit;<br>// WH_CALLWNDPROCRET WH_MOUSE WH_KEYBOARD WH_JOURNALRECORD WH_JOURNALPLAYBACK<br><br> hNextHookProc := SetWindowsHookEx(WH_CALLWNDPROCRET,<br> MouseHookHandler,<br> HInstance,<br> 0);<br> Result := hNextHookProc <> 0;<br>end;<br><br>function DisableMouseHook: BOOL; export;<br>begin<br> if hNextHookProc <> 0 then<br> begin<br> UnhookWindowsHookEx(hNextHookProc);<br> hNextHookProc := 0;<br> MessageBeep(0);<br> end;<br> Result := hNextHookProc = 0;<br>end;<br><br>procedure MouseHookExit;<br>begin<br> if hNextHookProc <> 0 then DisableMouseHook;<br> ExitProc := procSaveExit;<br>end;<br>