2017-05-24 21 views
0

我从List all physical printers using WMI query in Inno Setup得到了一个代码,我想将结果添加到列表框中。我曾试图在询问之前这样做,但我无法添加所有项目。这是我的代码:将项目添加到Inno安装程序中的列表框的正确方法?

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

回答

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); 
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; 
相关问题