吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 402|回复: 13
收起左侧

[已解决] 求教,为啥得不到子菜单项Caption ?

[复制链接]
冥界3大法王 发表于 2024-10-9 08:52
本帖最后由 冥界3大法王 于 2024-10-10 10:19 编辑

添加一个 MainMenu1,随便加1个主菜单名,3个子菜单名
要求得到 子菜单名的 Caption

[Delphi] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
procedure TForm13.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  // 假设MainMenu1已经添加到窗体中,并且有多个菜单项
  for I := 0 to MainMenu1.Items.Count - 1 do
  begin
    // 为每个菜单项关联事件处理程序
    MainMenu1.Items[I].OnClick := MainMenu1Click;
  end;
end;
 
procedure TForm13.MainMenu1Click(Sender: TObject);
var
  MenuItem: TMenuItem;
begin
  // 确保Sender是TMenuItem类型
  if Sender is TMenuItem then
  begin
    MenuItem := TMenuItem(Sender);      // 获取并显示菜单项的Caption
    ShowMessage(MenuItem.Caption);
  end;
end;

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

homejun 发表于 2024-10-9 09:23
本帖最后由 homejun 于 2024-10-9 09:26 编辑

[Delphi] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
procedure TForm1.FormCreate(Sender: TObject);
var
  I,j: Integer;
begin
  // 假设MainMenu1已经添加到窗体中,并且有多个菜单项
  for I := 0 to MainMenu1.Items.Count - 1 do
  begin
    // 为每个菜单子项关联事件处理程序
    for j := 0 to MainMenu1.Items[I].Count - 1 do
    begin
      MainMenu1.Items[I].Items[j].OnClick := MainMenu1Click;
    end;
 
  end;
 
end;

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
冥界3大法王 + 2 + 1 谢谢@Thanks!

查看全部评分

 楼主| 冥界3大法王 发表于 2024-10-9 12:47
lies2014 发表于 2024-10-9 15:25
冥界3大法王 发表于 2024-10-9 12:47
@homejun

那如果是这种子菜单的呢?

你这是粗心了吧?

2024-10-09_152228.jpg

[Delphi] 纯文本查看 复制代码
1
for z := 0 to PopupMenu1.Items[x].Items[y].Count - 1 do
 楼主| 冥界3大法王 发表于 2024-10-10 10:11
本帖最后由 冥界3大法王 于 2024-10-10 10:18 编辑
lies2014 发表于 2024-10-9 15:25
冥界3大法王 发表于 2024-10-9 12:47
@homejun

@homejun
@lies2014
要是下面这种又当如何?

Unit13.dfm
object Form13: TForm13
  Left = 0
  Top = 0
  Caption = 'Form13'
  ClientHeight = 280
  ClientWidth = 607
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -18
  Font.Name = 'Segoe UI'
  Font.Style = []
  PopupMenu = PopupMenu1
  OnCreate = FormCreate
  PixelsPerInch = 144
  TextHeight = 25
  object PopupMenu1: TPopupMenu
    AutoHotkeys = maManual
    Left = 56
    Top = 40
    object N1: TMenuItem
      Caption = #27604#36739#24037#20855
      object AAA1: TMenuItem
        Caption = 'AAA'
      end
      object BBB1: TMenuItem
        Caption = 'BBB'
      end
    end
    object N2: TMenuItem
      Caption = #32534#36753#24037#20855
      object N1111: TMenuItem
        Caption = '111'
      end
      object N2221: TMenuItem
        Caption = '222'
      end
    end
    object N3: TMenuItem
      Caption = #21453#27719#32534
      object N4: TMenuItem
        Caption = 'x64dbg'
      end
      object N5: TMenuItem
        Caption = 'Ollydbg'
        object N8881: TMenuItem
          Caption = '888'
        end
        object N7771: TMenuItem
          Caption = '777'
        end
      end
    end
  end
end

unit13.pas
unit Unit13;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus;

type
  TForm13 = class(TForm)
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    AAA1: TMenuItem;
    BBB1: TMenuItem;
    N2: TMenuItem;
    N1111: TMenuItem;
    N2221: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N8881: TMenuItem;
    N7771: TMenuItem;
    procedure FormCreate(Sender: TObject);
  private
    procedure MainMenu1Click(Sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form13: TForm13;

implementation

{$R *.dfm}

procedure TForm13.MainMenu1Click(Sender: TObject);
var
  MenuItem: TMenuItem;
begin
  if Sender is TMenuItem then            //确保Sender是TMenuItem类型
  begin
    MenuItem := TMenuItem(Sender);       //获取并显示菜单项的Caption
    ShowMessage(MenuItem.Caption);
  end;
end;
/////

procedure TForm13.FormCreate(Sender: TObject);
var
  I, j: Integer;
begin
  // 假设MainMenu1已经添加到窗体中,并且有多个菜单项
  for I := 0 to PopupMenu1.Items.Count - 1 do
  begin
    // 为每个菜单子项关联事件处理程序
    for j := 0 to PopupMenu1.Items[I].Count - 1 do
    begin
      PopupMenu1.Items[I].Items[j].OnClick := MainMenu1Click;
    end;
  end;
end;

end.

Project6.dpr
program Project6;

uses
  Vcl.Forms,
  Unit13 in 'Unit13.pas' {Form13};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm13, Form13);
  Application.Run;
end.

image.png
希望再好好的演示一下?
怎么去判断不是子菜单,而是子菜单项???
 楼主| 冥界3大法王 发表于 2024-10-10 10:42
本帖最后由 冥界3大法王 于 2024-10-10 10:47 编辑
procedure TForm13.MainMenu1Click(Sender: TObject);
var
  MenuItem: TMenuItem;
begin
  if (Sender is TMenuItem) and (MenuItem.Parent <> nil) then            //这样写不行!MenuItem.Parent <> MenuItem.Parent)也不行。
  begin
    MenuItem := TMenuItem(Sender);
    ShowMessage(MenuItem.Caption);
  end;
end;

//这样写不行!
homejun 发表于 2024-10-10 11:35
本帖最后由 homejun 于 2024-10-10 11:36 编辑

[Delphi] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
// 假设PopupMenu1已经添加到窗体中,并且有多个菜单项
for I := 0 to PopupMenu1.Items.Count - 1 do
begin
  // 为每个菜单子项关联事件处理程序
  for j := 0 to PopupMenu1.Items[I].Count - 1 do
  begin
    if PopupMenu1.Items[I].Items[j].Count = 0 then
      PopupMenu1.Items[I].Items[j].OnClick := MainMenu1Click;
    for z := 0 to PopupMenu1.Items[I].Items[j].Count - 1 do
    begin
      if PopupMenu1.Items[I].Items[j].Items[z].Count = 0 then
        PopupMenu1.Items[I].Items[j].Items[z].OnClick := MainMenu1Click;
    end;
  end;
end;
lies2014 发表于 2024-10-10 11:42
本帖最后由 lies2014 于 2024-10-10 11:53 编辑
冥界3大法王 发表于 2024-10-10 10:11
@homejun
@lies2014
要是下面这种又当如何?

这种用递归实现比较简单,逻辑也清晰,不管多少层菜单,一个函数搞定

[Delphi] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
unit Unit1;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus;
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
    AAA1: TMenuItem;
    BBB1: TMenuItem;
    N7771: TMenuItem;
    N8881: TMenuItem;
    N5: TMenuItem;
    N4: TMenuItem;
    N2221: TMenuItem;
    N1111: TMenuItem;
    N3: TMenuItem;
    N2: TMenuItem;
    N1: TMenuItem;
    PopupMenu1: TPopupMenu;
    procedure FormCreate(Sender: TObject);
  private
    procedure MainMenu1Click(Sender: TObject);
    procedure SetItem(tmpItems: TMenuItem);
  public
 
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
{ TForm1 }
 
procedure TForm1.MainMenu1Click(Sender: TObject);
var
  MenuItem: TMenuItem;
begin
  if Sender is TMenuItem then            //确保Sender是TMenuItem类型
  begin
    MenuItem := TMenuItem(Sender);       //获取并显示菜单项的Caption
    ShowMessage(MenuItem.Caption);
  end;
end;
 
procedure TForm1.SetItem(tmpItems: TMenuItem);
var
  i: Integer;
begin
  for i := 0 to tmpItems.Count - 1 do
  begin
    if tmpItems[i].Count = 0 then tmpItems[i].OnClick := MainMenu1Click
    else SetItem(tmpItems[i]);
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  SetItem(PopupMenu1.Items);
end;
 
end.
 楼主| 冥界3大法王 发表于 2024-10-10 11:54
image.png
@lies2014
实在是搞不定了,只能打包完整传上来解决了。https://wwgc.lanzouj.com/i968c2c5nmuf


lies2014 发表于 2024-10-10 13:50
冥界3大法王 发表于 2024-10-10 11:54
@lies2014
实在是搞不定了,只能打包完整传上来解决了。https://wwgc.lanzouj.com/i968c2c5nmuf

用我上面的递归函数就能解决

链接:https://pan.baidu.com/s/17VFRFe2BA_s2X7hUouBVZA
提取码:i8ec

实际上问题不在于实现的方式,在于菜单项下面还有子菜单的时候,鼠标移过去是会自动展开下级菜单的,即在代码里自动执行了一个鼠标点击动作,所以就会执行你的点击函数出现弹窗
我的递归代码只在最末级菜单弹窗,因此就避免了这个问题
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-3-5 14:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表