gpt4 book ai didi

delphi - 如何检测TMenuItem右键单击?

转载 作者:行者123 更新时间:2023-12-03 15:13:50 25 4
gpt4 key购买 nike

平台:delphi 2010

  1. 将 Tmainmenu 拖放到 form1 上
  2. 将 Tpopupmenu 拖放到 form1 上
  3. 添加 mainmenu1 和 popupmenu 项目(主菜单-->文件-->项目1,弹出菜单-->弹出项目1)
  4. item1.onRgihtClick 显示弹出菜单
  5. f9
  6. 文件-->item1 右键单击​​,弹出菜单,选择item1 bla bla bla...
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 222
ClientWidth = 447
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
Menu = MainMenu1
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object MainMenu1: TMainMenu
Left = 136
Top = 64
object file1: TMenuItem
Caption = 'file'
object recentfile1: TMenuItem
Caption = 'item 1'
end
end
end
object PopupMenu1: TPopupMenu
Left = 24
Top = 136
object popupitem1: TMenuItem
Caption = 'popup item'
OnClick = popupitem1Click
end
end
end

最佳答案

这是以下示例的菜单结构

File1           Edit1
FileItem11 EditItem11
FileItem21 EditItem21

和两个弹出菜单项。代码:

type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
FileItem11: TMenuItem;
FileItem21: TMenuItem;
Edit1: TMenuItem;
EditItem11: TMenuItem;
EditItem21: TMenuItem;
PopupMenu1: TPopupMenu;
PopupItem11: TMenuItem;
PopupItem21: TMenuItem;
procedure PopupItem11Click(Sender: TObject);
procedure PopupItem21Click(Sender: TObject);
private
FSelectedItem: TMenuItem;
FTracking: Boolean;
procedure MenuRButtonUp(var Msg: TMessage); message WM_MENURBUTTONUP;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.MenuRButtonUp(var Msg: TMessage);
var
Cmd: UINT;
begin
if not FTracking then
FSelectedItem :=
MainMenu1.FindItem(GetMenuItemID(Msg.LParam, Msg.WParam), fkCommand);

if (not FTracking) and (FSelectedItem <> nil) then begin
FTracking := True;
LongBool(Cmd) := TrackPopupMenuEx(PopupMenu1.Handle,
TPM_RECURSE or TPM_BOTTOMALIGN or TPM_RETURNCMD,
Mouse.CursorPos.X, Mouse.CursorPos.Y, Handle, nil);
FTracking := False;
if Cmd <> 0 then
PopupMenu1.DispatchCommand(Cmd);
end;
inherited;
end;

procedure TForm1.PopupItem11Click(Sender: TObject);
begin
Caption := 'Popup Item 1 clicked on ' + FSelectedItem.Caption;
end;

procedure TForm1.PopupItem21Click(Sender: TObject);
begin
// whatever..
Caption := 'Popup Item 2 clicked on ' + FSelectedItem.Caption;
end;

关于delphi - 如何检测TMenuItem右键单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com