documentation 的引用部分解释了为什么您没有看到此消息:
在用户 [...]
时发送
TApplicationEvents.OnMessage 事件只能检测 已发布 消息,不能检测 已发送 消息。
TMainMenu
所以如果你想检测这个消息,你可以添加
protected
procedure WndProc(var Message: TMessage); override;
到你的表单类,实现如下:
procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg = WM_MENURBUTTONUP then
ShowMessage('rbu')
else
inherited
end;
试试,例如:
procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg = WM_MENURBUTTONUP then
begin
var MI := Menu.FindItem(Message.LParam, fkHandle);
if Assigned(MI) and InRange(Message.WParam, 0, MI.Count - 1) then
ShowMessageFmt('Menu item "%s" right clicked.', [MI.Items[Message.WParam].Caption]);
end
else
inherited
end;
TPopupMenu
对于TPopupMenu,您需要编写自己的TPopupList后代:
type
TPopupListEx = class(TPopupList)
protected
procedure WndProc(var Message: TMessage); override;
end;
{ TPopupListEx }
procedure TPopupListEx.WndProc(var Message: TMessage);
begin
if Message.Msg = WM_MENURBUTTONUP then
ShowMessage('rbu')
else
inherited
end;
initialization
FreeAndNil(PopupList);
PopupList := TPopupListEx.Create;
并确保将TPopupMenu 的TrackButton 设置为tbLeftButton。
如果您有多个弹出菜单,您可以尝试这样的操作(未完全测试):
procedure TPopupListEx.WndProc(var Message: TMessage);
begin
if Message.Msg = WM_MENURBUTTONUP then
begin
for var X in PopupList do
if TObject(X) is TPopupMenu then
begin
OutputDebugString(PChar(TPopupMenu(X).Name));
var MI: TMenuItem;
if TPopupMenu(X).Handle = HMENU(Message.LParam) then
MI := TPopupMenu(X).Items
else
MI := TPopupMenu(X).FindItem(HMENU(Message.LParam), fkHandle);
if Assigned(MI) and InRange(Message.WParam, 0, MI.Count - 1) then
begin
ShowMessageFmt('Menu item "%s" right clicked.', [MI.Items[Message.WParam].Caption]);
Break;
end;
end;
end
else
inherited
end;