Inno Setup Detect changed task/item in TasksList.O

2019-02-19 07:17发布

问题:

I'm stuck on simple situation with OnClickCheck property. The problem is that I see a Msgbox every time I turn on backup task, but also (while it's switched on) OnClickCheckappeared on pressing uninst task too! Seems that OnClickCheck checks all clicks, but I need to check click only on the first task.

It is logical to add to "WizardForm.TasksList.OnClickCheck" exact number of task (WizardForm.TasksList.OnClickCheck[0]), but compiler doesn't agree with it.

[Tasks]
Name: backup; Description: do backup
Name: uninst; Description: do not create uninstaller

[Code]

procedure TaskOnClick(Sender: TObject); 
begin
  if IsTaskSelected('backup') then 
  begin
    MsgBox('backup task has been checked.', mbInformation, MB_OK) 
  end;
end;

procedure InitializeWizard();
begin
  WizardForm.TasksList.OnClickCheck := @TaskOnClick;
end;

回答1:

There's no way tell exactly what task (list item) was changed in the OnClickChange event.

To tell which item was checked by the user, you can use the ItemIndex property. The user can check only the selected item.

Though if you have a task hierarchy, even unselected task can be toggled automatically by the installer due to a change in child/parent items. So to tell all changes, all you can do is to remember the previous state and compare it against the current state, when the OnClickCheck is called.

var
  TasksState: array of TCheckBoxState;

procedure TasksClickCheck(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to WizardForm.TasksList.Items.Count - 1 do
  begin
    if TasksState[I] <> WizardForm.TasksList.State[I] then
    begin
      Log(Format('Task %d state changed from %d to %d',
                 [I, TasksState[I], WizardForm.TasksList.State[I]]));
      TasksState[I] := WizardForm.TasksList.State[I];
    end;
  end;
end;

procedure CurPageChanged(CurPageID: Integer);
var
  I: Integer;
begin
  if CurPageID = wpSelectTasks then
  begin
    { Only now is the task list initialized (e.g. based on selected setup }
    { type and components). Remember what is the current/initial state. }
    SetArrayLength(TasksState, WizardForm.TasksList.Items.Count);
    for I := 0 to WizardForm.TasksList.Items.Count - 1 do
      TasksState[I] := WizardForm.TasksList.State[I];
  end;
end;

procedure InitializeWizard();
begin
  WizardForm.TasksList.OnClickCheck := @TasksClickCheck;
end;


标签: inno-setup