Inno Setup ComponentsList OnClick 事件

Inno Setup ComponentsList OnClick event

我有一个 Inno Setup 安装程序的组件列表,有 19 个不同的选项,我想为 ONE 个组件设置 OnClick 事件。有没有办法做到这一点?或者如果为所有组件都设置了 OnClick 事件,是否可以检查哪个组件触发了事件?

目前,OnClick事件设置如下:

Wizardform.ComponentsList.OnClick := @CheckChange;

我想做这样的事情:

Wizardform.ComponentsList.Items[x].OnClick := @DbCheckChange;

WizardForm.ComponentList 声明为:TNewCheckListBox

Or is there a way to check which component triggered the onclick event if it's set for all components?

大多数组件事件都有一个 Sender 参数指向触发事件的组件对象。但是,在这种情况下,Sender 可能就是 ComponentsList 本身。根据 ComponentsList 实际上被声明为(TListBox,等等),它可能有一个 属性 来指定当前正在 selected/clicked 的项目(ItemIndex, ETC)。或者它甚至可能有一个单独的事件来报告每个项目的点击次数。你没有说 ComponentsList 被声明为什么,所以这里没有人能确切地告诉你要在其中寻找什么。

您不想使用 OnClick,请改用 OnClickCheck

OnClick 用于不改变选中状态的点击(如点击任何项目外;或点击固定项目;或使用键盘更改选择),但主要不用于检查使用键盘。

OnClickCheck 仅在选中状态发生变化时调用,同时适用于键盘和鼠标。

要告诉用户检查了哪个项目,请使用 ItemIndex 属性。用户只能勾选选中的项目。

尽管如果您有组件层次结构或安装类型,安装程序会因 child/parent 项更改或安装类型更改而自动检查的项目不会触发 OnClickCheck(也不是 OnClick)。所以要告诉所有变化,你所能做的就是记住以前的状态并将其与当前状态进行比较,当 WizardForm.ComponentsList.OnClickCheckWizardForm.TypesCombo.OnChange 被调用时。

const
  TheItem = 2; { the item you are interested in }

var
  PrevItemChecked: Boolean;
  TypesComboOnChangePrev: TNotifyEvent;

procedure ComponentsListCheckChanges;
var
  Item: string;
begin
  if PrevItemChecked <> WizardForm.ComponentsList.Checked[TheItem] then
  begin
    Item := WizardForm.ComponentsList.ItemCaption[TheItem];
    if WizardForm.ComponentsList.Checked[TheItem] then
    begin
      Log(Format('"%s" checked', [Item]));
    end
      else
    begin
      Log(Format('"%s" unchecked', [Item]));
    end;

    PrevItemChecked := WizardForm.ComponentsList.Checked[TheItem];
  end;
end;

procedure ComponentsListClickCheck(Sender: TObject);
begin
  ComponentsListCheckChanges;
end;

procedure TypesComboOnChange(Sender: TObject);
begin
  { First let Inno Setup update the components selection }
  TypesComboOnChangePrev(Sender);
  { And then check for changes }
  ComponentsListCheckChanges;
end;

procedure InitializeWizard();
begin
  WizardForm.ComponentsList.OnClickCheck := @ComponentsListClickCheck;

  { The Inno Setup itself relies on the WizardForm.TypesCombo.OnChange, }
  { so we have to preserve its handler. }
  TypesComboOnChangePrev := WizardForm.TypesCombo.OnChange;
  WizardForm.TypesCombo.OnChange := @TypesComboOnChange;

  { Remember the initial state }
  { (by now the components are already selected according to }
  { the defaults or the previous installation) }
  PrevItemChecked := WizardForm.ComponentsList.Checked[TheItem];
end;

有关更通用的解决方案,请参阅 Inno Setup Detect changed task/item in TasksList.OnClickCheck event。尽管使用组件,但也必须触发对 WizardForm.TypesCombo.OnChange 调用的检查。