2

如何在 TStrings 中获取所有已安装的组件?

我认为此代码仅在包中有效:

    uses TypInfo, ToolIntf, Exptintf;

procedure GetComponentNames(lst: TStrings);
var
  i, k: Integer;
  CRef: TClass;
  strName: ShortString;
begin
  lst.Clear;
  for i := 0 to ToolServices.GetModuleCount-1 do
  begin
    for k := 0 to ToolServices.GetComponentCount(i)-1 do
      begin
       CRef := TClass(GetClass(ToolServices.GetComponentName(i, k)));
       while CRef <> nil do
       begin
         strName := CRef.ClassName;
         if lst.IndexOf(strName) = -1 then
           lst.Add(strName);
         if str <> 'TComponent' then
          CRef := CRef.ClassParent
         else
           CRef := nil;
       end;
      end;
  end;    
end;

或者:

uses ToolsApi;

{....}

var
  a, i: Integer;
begin
  with (BorlandIDEServices as IOTAPackageServices) do
  begin
    for a := 0 to GetPackageCount - 1 do
    begin
      for i := 0 to GetComponentCount(a) - 1 do
      begin
        {get each component name with GetComponentName(a, i);}
        // DoSomething   
      end;
    end;
  end;
end;

感谢帮助。

4

1 回答 1

4

这个例子没有使用 OpenAPI,它使用了 Registry。它可以工作,但它也列出了其他隐藏项目中的非可视组件。

procedure GetComponentNames(lst: TStrings);
var
  i, j, iPos: Integer;
  Reg: TRegistry;
  sComponent: String;
  slValues, slData: TStrings;
begin
  Reg := TRegistry.Create;
  slValues := TStringList.Create;
  slData := TStringList.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    Reg.OpenKey('Software\Borland\Delphi\6.0\Palette', False); // Change reg key where appropriate
    Reg.GetValueNames(slValues);
    for i := 0 to Pred(slValues.Count) do
    begin
      lst.Append(slValues[i]);
      lst.Append('----------');
      slData.Delimiter := ';';
      slData.DelimitedText := Reg.ReadString(slValues[i]);
      for j := 0 to Pred(slData.Count) do
      begin
        sComponent := slData[j];
        iPos := Pos('.', sComponent);
        if (iPos > 0) then
          Delete(sComponent, 1, iPos);
        lst.Append(sComponent);
      end;
    end;
  finally
    slData.Free;
    slValues.Free;
    Reg.Free;
  end; {try..finally}
end;

我并不是说这是理想的,但它确实为您提供了一份清单和一个先机。

于 2009-10-12T15:34:14.663 回答