Fórum Listar imperssoras em service application #418461
14/06/2012
0
Segue o codigo de como estou tentando listar elas:
var
Contador :Integer;
Printer: TPrinter;
begin
try
for Contador := 0 to Printer.Printers.Count - 1 do
begin
cmbImpressoras.Items.Add(Printer. Printers. Strings[Contador]);
cmbImpressoras.ItemIndex:=0;
end;
finally
Printer.free;
end;
end;Creio que seja algum problema por ser service application pois em uma aplicação normal ele funciona normalmente,gostaria de deixar o usuario listar e escolher para que eu salve a escolhida em um .ini para quando o metodo de impressão for chamado receber os parametros do ini.
Grato.
Darcio Junior
Curtir tópico
+ 0Posts
14/06/2012
Santos
Printer := TPrinter.Create;
tente isso:
var
Contador :Integer;
Printer: TPrinter;
begin
Printer := TPrinter.Create;
try
for Contador := 0 to Printer.Printers.Count - 1 do
begin
cmbImpressoras.Items.Add(Printer. Printers. Strings[Contador]);
cmbImpressoras.ItemIndex:=0;
end;
finally
Printer.free;
end;
end;
Gostei + 0
14/06/2012
Deivison Melo
Adicione um ListView e um botão ao seu formulário e depois criar duas procedures:
********************************************************************************************************************
procedure AdicionaImpressoras(LV: TListView; Servidor, NomeImpressora,
Compartilhamento, Porta, NomeDriver, Comentario: String);
var
i: integer;
begin
with LV do
begin
Items.Add;
i := Items.Count-1;
Items[i].Caption := Servidor;
Items[i].SubItems.Add(NomeImpressora);
Items[i].SubItems.Add(Compartilhamento);
Items[i].SubItems.Add(Porta);
Items[i].SubItems.Add(NomeDriver);
Items[i].SubItems.Add(Comentario);
end;
end;
********************************************************************************************************************
procedure GetPrinterList(List: TListView);
var
Buffer, PrinterInfo: PChar;
Flags, Count, NumInfo: DWORD;
i: Integer;
Level: Byte;
begin
(*
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Flags := PRINTER_ENUM_CONNECTIONS or PRINTER_ENUM_LOCAL;
Level := 4;
end
else
begin
Flags := PRINTER_ENUM_LOCAL;
Level := 2;
end;
*)
Flags := PRINTER_ENUM_CONNECTIONS or PRINTER_ENUM_LOCAL;
Level := 2;
Count := 0;
EnumPrinters(Flags, nil, Level, nil, 0, Count, NumInfo);
if Count > 0 then
begin
GetMem(Buffer, Count);
try
if not EnumPrinters(Flags, nil, Level, PByte(Buffer), Count, Count, NumInfo) then
Exit;
PrinterInfo := Buffer;
for i := 0 to NumInfo - 1 do
begin
case Level of
2: begin
AdicionaImpressoras(List, PPrinterInfo2(PrinterInfo)^.pServerName,
PPrinterInfo2(PrinterInfo)^.pPrinterName,
PPrinterInfo2(PrinterInfo)^.pShareName,
PPrinterInfo2(PrinterInfo)^.pPortName,
PPrinterInfo2(PrinterInfo)^.pDriverName,
PPrinterInfo2(PrinterInfo)^.pComment);
Inc(PrinterInfo, SizeOf(TPrinterInfo2));
end;
4: begin
AdicionaImpressoras(List, PPrinterInfo4(PrinterInfo)^.pServerName,
PPrinterInfo4(PrinterInfo)^.pPrinterName,
null,
null,
null,
null);
Inc(PrinterInfo, SizeOf(TPrinterInfo4));
end;
5: begin
AdicionaImpressoras(List, null,
PPrinterInfo5(PrinterInfo)^.pPrinterName,
null,
PPrinterInfo2(PrinterInfo)^.pPortName,
null,
null);
Inc(PrinterInfo, SizeOf(TPrinterInfo5));
end;
end;
end;
finally
FreeMem(Buffer, Count);
end;
end;
end;
********************************************************************************************************************
Feito isso, adicione no código do botão:
GetPrinterList(LView);
********************************************************************************************************************
Unit completa abaixo:
********************************************************************************************************************
(*******************************************************************************
Retornar informações de impressoras...
Alessandro Ferreira.
(c) The Club, o maior clube de programadores do Brasil, 2003.
www.theclub.com.br
www.desenvolvedor.com.br
*******************************************************************************)
unit Unit1;
interface
uses
Printers, WinSpool,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
LView: TListView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure AdicionaImpressoras(LV: TListView; Servidor, NomeImpressora,
Compartilhamento, Porta, NomeDriver, Comentario: String);
var
i: integer;
begin
with LV do
begin
Items.Add;
i := Items.Count-1;
Items[i].Caption := Servidor;
Items[i].SubItems.Add(NomeImpressora);
Items[i].SubItems.Add(Compartilhamento);
Items[i].SubItems.Add(Porta);
Items[i].SubItems.Add(NomeDriver);
Items[i].SubItems.Add(Comentario);
end;
end;
procedure GetPrinterList(List: TListView);
var
Buffer, PrinterInfo: PChar;
Flags, Count, NumInfo: DWORD;
i: Integer;
Level: Byte;
begin
(*
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Flags := PRINTER_ENUM_CONNECTIONS or PRINTER_ENUM_LOCAL;
Level := 4;
end
else
begin
Flags := PRINTER_ENUM_LOCAL;
Level := 2;
end;
*)
Flags := PRINTER_ENUM_CONNECTIONS or PRINTER_ENUM_LOCAL;
Level := 2;
Count := 0;
EnumPrinters(Flags, nil, Level, nil, 0, Count, NumInfo);
if Count > 0 then
begin
GetMem(Buffer, Count);
try
if not EnumPrinters(Flags, nil, Level, PByte(Buffer), Count, Count, NumInfo) then
Exit;
PrinterInfo := Buffer;
for i := 0 to NumInfo - 1 do
begin
case Level of
2: begin
AdicionaImpressoras(List, PPrinterInfo2(PrinterInfo)^.pServerName,
PPrinterInfo2(PrinterInfo)^.pPrinterName,
PPrinterInfo2(PrinterInfo)^.pShareName,
PPrinterInfo2(PrinterInfo)^.pPortName,
PPrinterInfo2(PrinterInfo)^.pDriverName,
PPrinterInfo2(PrinterInfo)^.pComment);
Inc(PrinterInfo, SizeOf(TPrinterInfo2));
end;
4: begin
AdicionaImpressoras(List, PPrinterInfo4(PrinterInfo)^.pServerName,
PPrinterInfo4(PrinterInfo)^.pPrinterName,
null,
null,
null,
null);
Inc(PrinterInfo, SizeOf(TPrinterInfo4));
end;
5: begin
AdicionaImpressoras(List, null,
PPrinterInfo5(PrinterInfo)^.pPrinterName,
null,
PPrinterInfo2(PrinterInfo)^.pPortName,
null,
null);
Inc(PrinterInfo, SizeOf(TPrinterInfo5));
end;
end;
end;
finally
FreeMem(Buffer, Count);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GetPrinterList(LView);
end;
end.
********************************************************************************************************************
Atenciosamente,
Deivison
Gostei + 0
14/06/2012
Darcio Junior
Printer := TPrinter.Create;
tente isso:
var
Contador :Integer;
Printer: TPrinter;
begin
Printer := TPrinter.Create;
try
for Contador := 0 to Printer.Printers.Count - 1 do
begin
cmbImpressoras.Items.Add(Printer. Printers. Strings[Contador]);
cmbImpressoras.ItemIndex:=0;
end;
finally
Printer.free;
end;
end;
Nossa cara,eu me matando aqui muito obrigado.
@Deivison Não tive tempo de testar sua solução mais obrigado por posta-la, talvez ela me tenha uso.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)