在 Windows 10 上的 32 位 Delphi 11 VCL 應用程式中,我使用一個TApplicationEvents組件來捕獲 Windows 訊息。不幸的是,當我右鍵單擊MenuItem時,TApplicationEvents似乎對WM_MENURBUTTONUP訊息沒有反應TPopupMenu:
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
case Msg.message of
Winapi.Messages.WM_MENURBUTTONUP: CodeSite.Send('TForm1.ApplicationEvents1Message: WM_MENURBUTTONUP');
end;
end;
在微軟的檔案說:
WM_MENURBUTTONUP 訊息
當用戶在游標位于選單項上時釋放滑鼠右鍵時發送。
作為替代,WM_COMMAND通過左鍵單擊和右鍵單擊發送。但是,出于特定目的,我只需要在右鍵單擊選單項時做出反應。
uj5u.com熱心網友回復:
檔案中參考的部分解釋了為什么您沒有看到此訊息:
發送用戶在[...]
該TApplicationEvents.OnMessage事件只能檢測已發布的訊息,不能檢測已發送的訊息。
主選單
所以如果你想檢測這個訊息,你可以添加
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;
彈出選單
對于 a 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's設定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;

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363512.html
標籤:德尔福 登录 菜单项 delphi-11-alexandria
