在动作乐队中有一个 TAction 组件。
该组件包含一个名为
GroupIndex: Integer;
然而该领域
RadioItem: Boolean;
不在这里。
- 这是为什么?
- 如何使 TAction 成为复选框?
动作的方向是ActionMainMenuBar 和ActionManager。
在动作乐队中有一个 TAction 组件。
该组件包含一个名为
GroupIndex: Integer;
然而该领域
RadioItem: Boolean;
不在这里。
动作的方向是ActionMainMenuBar 和ActionManager。
尽管 TAction 从 TComponent 派生,但动作并不是 GUI 元素意义上的组件。它旨在链接到 GUI 元素,例如可以是单选按钮、复选框,或者在您的情况下是 TActionMainMenuBar 上的 TActionClientItem。
至于你的问题:
动作的GroupIndex
属性指示该动作的行为是否像单选项目。帮助说:
GroupIndex 用于定义类似于单选按钮的操作组。当 GroupIndex 大于 0 时,它标识动作所属的组。当该组中任何操作的 Checked 属性设置为 true 时,该组中所有其他操作的 Checked 属性设置为 false。也就是说,一次只能检查组中的一项操作。注意:组中的所有操作必须由相同的操作列表或操作管理器列出。
要在带有复选框的 ActionMainMenuBar 中显示菜单项(TActionClientItem):
Checked
为真,Category
属性,Checked
在操作的 OnExecute 事件处理程序中切换属性。要显示与 ActionManager 中的操作链接的普通复选框:不要使用 ActionMainMenuBar,而是使用 ActionToolBar,您可以在其上放置默认的复选框组件。
我不确定您是否需要复选框或单选按钮,但两者都很简单:例如具有以下 Form1.dfm 的简单 VCL 应用程序:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 251
ClientWidth = 588
Color = clBtnFace
ParentFont = True
Menu = MainMenu1
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object CheckBox1: TCheckBox
Left = 216
Top = 32
Width = 97
Height = 17
Action = Action1
TabOrder = 0
end
object RadioGroup1: TRadioGroup
Left = 336
Top = 32
Width = 185
Height = 121
Caption = 'RadioGroup1'
Items.Strings = (
'1'
'2'
'3')
TabOrder = 1
end
object ActionList1: TActionList
Left = 184
Top = 120
object Action1: TAction
Caption = 'Action1'
OnExecute = Action1Execute
end
object Action2: TAction
Caption = 'Action2'
GroupIndex = 1
OnExecute = Action1Execute
end
object Action3: TAction
Caption = 'Action3'
GroupIndex = 1
OnExecute = Action1Execute
end
object Action4: TAction
Caption = 'Action4'
GroupIndex = 1
OnExecute = Action1Execute
end
end
object MainMenu1: TMainMenu
Left = 224
Top = 120
object miTest: TMenuItem
Caption = 'Test'
object miAction1: TMenuItem
Action = Action1
AutoCheck = True
end
object miSep: TMenuItem
Caption = '-'
end
object miAction2: TMenuItem
Action = Action2
AutoCheck = True
GroupIndex = 1
RadioItem = True
end
object miAction3: TMenuItem
Action = Action3
AutoCheck = True
GroupIndex = 1
RadioItem = True
end
object miAction4: TMenuItem
Action = Action4
AutoCheck = True
GroupIndex = 1
RadioItem = True
end
end
end
end
使用这些事件处理程序:
procedure TForm1.Action1Execute(Sender: TObject);
var
actn: TAction absolute Sender;
begin
Assert(Sender is TAction);
actn.Checked := not actn.Checked;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
// Hardcoded association for test purposes:
for i := 0 to Pred(RadioGroup1.ControlCount) do
RadioGroup1.Controls[i].Action := ActionList1.Actions[i + 1];
end;
像我期望的那样工作。
要使操作看起来像菜单上的单选项目,必须在菜单项上设置 RadioItem,而不是在操作上。如果 GroupIndex 为 <> 0,我不知道为什么这不是默认值。
更新: ActionManager 的东西比旧的 ActionLists 更棘手。这个 DFM
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 271
ClientWidth = 588
Color = clBtnFace
ParentFont = True
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object CheckBox1: TCheckBox
Left = 200
Top = 48
Width = 97
Height = 17
Action = Action1
TabOrder = 0
end
object RadioGroup1: TRadioGroup
Left = 336
Top = 32
Width = 185
Height = 121
Caption = 'RadioGroup1'
ItemIndex = 1
Items.Strings = (
'1'
'2'
'3')
TabOrder = 1
end
object ActionMainMenuBar1: TActionMainMenuBar
Left = 0
Top = 0
Width = 588
Height = 24
UseSystemFont = False
ActionManager = ActionManager1
Caption = 'ActionMainMenuBar1'
ColorMap.HighlightColor = clWhite
ColorMap.BtnSelectedColor = clBtnFace
ColorMap.UnusedColor = clWhite
ParentFont = True
PersistentHotKeys = True
Spacing = 0
end
object ActionManager1: TActionManager
ActionBars = <
item
Items = <
item
Items = <
item
Action = Action1
Caption = '&Action1'
end
item
Caption = '-'
end
item
Action = Action2
Caption = 'A&ction2'
end
item
Action = Action3
Caption = 'Ac&tion3'
end
item
Action = Action4
Caption = 'Act&ion4'
end>
Caption = 'T&est'
end>
ActionBar = ActionMainMenuBar1
end>
Left = 184
Top = 160
StyleName = 'XP Style'
object Action1: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action1'
end
object Action2: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action2'
GroupIndex = 1
end
object Action3: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action3'
Checked = True
GroupIndex = 1
end
object Action4: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action4'
GroupIndex = 1
end
end
end
用这个处理程序
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Pred(ActionManager1.ActionCount) do
TAction(ActionManager1.Actions[i]).DisableIfNoHandler := False;
for i := 0 to Pred(RadioGroup1.ControlCount) do
RadioGroup1.Controls[i].Action := ActionManager1.Actions[i + 1];
end;
作品。但是,如果不使用 AutoCheck,我无法让收音机项目工作。