通过调用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;
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;
解决方案 »
- 大家帮帮忙!!点一点51自习网的网址,我很需要积分,,谢谢!
- Delphi2009 安装BusinessSkinForm 7.33 报这样的错误
- 请问一个关于传给应用程序参数的问题
- lxpbuaa(桂枝香在故国晚秋), 可以提供你的 QQ吗?谢谢!俺的是:114390304 mail:[email protected]
- 我枚举一个游戏窗体的类,只得到了TDXDraw,我怎样才能得到TDXDraw中更多的信息呢,比如得到其中的文字?
- 如何动态生成Access文件,并定义表结构
- QRreport控件的用法
- 各位大虾QuickReport的问题?请高手看看!多谢!!!!!
- 请问各位大哥大姐,好心人请进!一个可能很简单的问题。
- 寻求GIS软件,同仁朋友们请推荐一把
- 关于delphi的指针的使用问题
- adotable控件中不能设置多个lookup字段吗?
http://tech.ddvip.com/2006-07/11543502696514.html
你可以看看