如何通过名称查找组件索引?

时间:2014-03-30 20:05:01

标签: inno-setup

为什么我不能通过使用此行来获取我的组件索引之一?

WizardForm.ComponentsList.FindComponent('core').ComponentIndex

如果我错了,任何人都可以指出我获得该组件索引的方法吗?

此致 开始。

2 个答案:

答案 0 :(得分:3)

目前,无法通过组件ComponentsList参数在Name中找到组件项的索引。 Name参数存储在TSetupComponentEntry结构中,该结构由ComponentsList.ItemObject[i]对象集合保存,但由于Inno Setup Pascal中缺少指针支持,您无法访问它脚本。

如何在ComponentsList中唯一标识组件的唯一方法是Description参数。要通过ComponentsList中的说明查找某个组件的索引,您可以使用:

[Components]
Name: "mycomponent"; Description: "Component description"; Types: full

[Code]
...
var
  ItemIndex: Integer;
begin
  ItemIndex := WizardForm.ComponentsList.Items.IndexOf('Component description');
  ...
end;

答案 1 :(得分:3)

我通过WizardSelectedComponents找到了一种方法。

在var中定义这个String数组,以便保存组件名称:

ComponentsName: array of String;

定义此功能和此程序:

Function StringToArray(const Text: String; const Cut: String): array of String;
var
    i: Integer;
    k: Integer;
Begin
    SetArrayLength(Result, 0);
    if Cut = '' then Cut:= #1310;
    Repeat
        k:= Pos(Cut,Text);
        if k = 1 then
        begin
            Delete(Text, 1, Length(Cut));
            CONTINUE
        end;
        SetArrayLength(Result, GetArrayLength(Result) +1); i:= GetArrayLength(Result) -1;
        if k = 0 then
            Result[i]:=Text
        else begin
            Result[i]:= Copy(Text, 1, k -1); Delete(Text, 1, Length(Result[i]) + Length(Cut));
        end;
    Until Length(Text) * k = 0;
End;

procedure InitializeComponentsName();
var
    I: Integer;
    CheckBack: array of boolean;
begin
    SetArrayLength(CheckBack, WizardForm.ComponentsList.ItemCount);

    for I:=0 to WizardForm.ComponentsList.ItemCount-1 do
    begin
        //Saves state in CheckBack.
        CheckBack[I]:=WizardForm.ComponentsList.Checked[I];
        //Only checks non checked components.
        if not CheckBack[I] then WizardForm.ComponentsList.Checked[I]:=true;
    end;

    //Saves components names in ComponentsName array
    ComponentsName:=StringToArray(WizardSelectedComponents(false), ',');

    //Unchecks components that was uncheck previouly.
    //If we try to check a checked component it may crash the Inno program (tested)
    for I:=0 to WizardForm.ComponentsList.ItemCount-1 do
    begin
        if not CheckBack[I] then WizardForm.ComponentsList.Checked[I]:=false;
    end;

    //LOG components name.
    log('COMPONENTS NAME:');
    for I:=0 to GetArrayLength(ComponentsName) -1 do
    begin
        log(ComponentsName[I]);
    end;

end;

您必须在InitializeWizard上调用此过程。 现在我们在ComponentsName数组中有所有组件名称。 您可以使用getComponentName获取组件名称:

//Returns component name by Index.
function getComponentName(Index: Integer): String;
begin
    if ((Index>=0) and (Index<GetArrayLength(ComponentsName))) then Result:=ComponentsName[Index];
end;

和GetIndexComponent按名称获取索引组件。

//Returns index component by Name. -1 if it doesn't exist.
function GetIndexComponent(ComponentName: String): Integer;
var
    J: Integer;
begin
    Result:= -1;
    for J:=0 to GetArrayLength(ComponentsName)-1 do
    begin
        if (ComponentName=ComponentsName[J]) then
        begin
            Result:=J;
            break;
        end;
    end;
end;

如果向ComponentsList添加另一个组件,则必须调用InitializeComponentsName才能更新ComponentsList数组。