在 Windows 10 上的 32 位 Delphi 11 VCL 應用程式中,當右鍵單擊任何選單項時,我需要獲取單擊的 MenuItem 的名稱。
TApplicationEvents當我單擊任何選單項時,我使用一個組件和此代碼來獲得通知:
procedure TformMain.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
case Msg.message of
Winapi.Messages.WM_COMMAND:
begin
CodeSite.Send('TformMain.ApplicationEvents1Message: WM_COMMAND');
end;
end;
end;
然而:
如何僅在右鍵單擊選單項時收到通知?
如何獲取單擊的 MenuItem 的 NAME?
uj5u.com熱心網友回復:
每個TMenu(即TMainMenu或TPopupMenu)都提供了一個FindItem方法,它允許您通過不同的條件查找專案。在您的情況下,對表單主選單的正確呼叫是
TheMenuItem := Menu.FindItem(Msg.wParam, fkCommand);
uj5u.com熱心網友回復:
由于我的應用程式中有多個表單,并且每個表單上都有多個(彈出)選單,因此這里需要一個特殊的解決方案:
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
case Msg.message of
Winapi.Messages.WM_COMMAND:
begin
// Todo: Check HERE for RightMouseButtonDown - BUT HOW? (Or how to check HERE for modifier keys?)
var ThisMenuItem := GetMenuItem(Msg.wParam);
if Assigned(ThisMenuItem) then
begin
CodeSite.Send('TForm1.ApplicationEvents1Message: Clicked MenuItem Name', ThisMenuItem.Name);
end;
end;
end;
end;
function TForm1.GetMenuItem(const aWParam: NativeUInt): TMenuItem;
var
ThisMenuItem: TMenuItem;
begin
Result := nil;
var ThisForm := Screen.ActiveForm; // works on any form in the application
for var i := 0 to ThisForm.ComponentCount - 1 do
begin
if ThisForm.Components[i] is TMenu then
begin
ThisMenuItem := TMenu(ThisForm.Components[i]).FindItem(aWParam, fkCommand);
if Assigned(ThisMenuItem) then
begin
Result := ThisMenuItem;
EXIT;
end;
end;
end;
end;
有什么優化嗎?
我還需要什么:如何檢查 WM_COMMAND 訊息處理程式中的 RightMouseButtonDown 或修飾鍵?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361593.html
