在Inno Setup中将项目添加到列表框的正确方法?

时间:2017-05-24 23:07:37

标签: inno-setup wmi-query pascalscript

我从List all physical printers using WMI query in Inno Setup获得了一个代码,我想将结果添加到列表框中。我在尝试之前尝试过这样做,但我无法添加所有项目。这是我的代码:

var
  Query, AllPrinters: string;
  WbemLocator, WbemServices, WbemObjectSet: Variant;
  Printer: Variant;
  I: Integer;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');
  Query := 'SELECT Name FROM Win32_Printer';
  WbemObjectSet := WbemServices.ExecQuery(Query);
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    for I := 0 to WbemObjectSet.Count - 1 do
    begin
      Printer := WbemObjectSet.ItemIndex(I);
      if not VarIsNull(Printer) then
      begin
        Log(Printer.Name);
        AllPrinters := Printer.Name;
      end;
    end;
  end;
end;

然后在自定义页面上执行以下操作:

ListBoxPrinters.Items.Add(AllPrinters);

enter image description here

2 个答案:

答案 0 :(得分:1)

您始终使用下一个值AllPrinters := Printer.Name;覆盖!

简单地构建AllPrinters字符串,就像那样

....
AllPrinters := '';
....
for I := 0 to WbemObjectSet.Count - 1 do
    begin
      Printer := WbemObjectSet.ItemIndex(I);
      if not VarIsNull(Printer) then
      begin
        Log(Printer.Name);
        AllPrinters := AllPrinters + Printer.Name + #13#10;
      end;
    end;
end;

ListBoxPrinters.Items.Text := AllPrinters;

答案 1 :(得分:1)

您以相同的方式将项目(打印机)添加到列表框中,原始代码将它们添加到日志中:在循环中!

for I := 0 to WbemObjectSet.Count - 1 do
begin
  Printer := WbemObjectSet.ItemIndex(I);
  if not VarIsNull(Printer) then
  begin
    ListBoxPrinters.Items.Add(Printer.Name);
  end;
end;

当然,在迭代打印机之前,您必须使用ListBoxPrinters创建自定义页面。

如果因任何原因无法在创建页面后运行查询,则可以将打印机列表存储到TStringList

var
  Printers: TStringList;
Printers := TStringList.Create;

for I := 0 to WbemObjectSet.Count - 1 do
begin
  Printer := WbemObjectSet.ItemIndex(I);
  if not VarIsNull(Printer) then
  begin
    Printers.Add(Printer.Name);
  end;
end;

准备好列表框后,只需将列表复制到框中:

ListBoxPrinters.Items.Assign(Printers);