Variante1: Nur für das eigene Programm, nicht systemweit
uses printers;
procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.items := printer.printers;
listbox1.itemindex := printer.printerindex;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
if listbox1.items.count > 0 then
printer.printerindex := listbox1.itemindex;
end;
// -----------------------------------------------------------
Variante2: Für den System-Drucker
uses winspool, IniFiles;
var
pri: array of _PRINTER_INFO_2A;
pcbN: DWord;
procedure TForm1.FormCreate(Sender: TObject);
var
pcR: DWord;
i: integer;
begin
EnumPrinters(PRINTER_ENUM_LOCAL, nil, 2, nil, 0, pcbN, pcR);
getmem(pri, pcbN);
EnumPrinters(PRINTER_ENUM_LOCAL, nil, 2, pri, pcbN, pcbN, pcR);
for i := 0 to pcR - 1 do
begin
listbox1.items.add(pri[i].pPrinterName);
if pri[i].attributes and PRINTER_ATTRIBUTE_DEFAULT <> 0 then
listbox1.itemindex := i;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
freemem(pri, pcbN);
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
s: string;
ini: TIniFile;
begin
with listbox1 do
begin
if items.count > 0 then
begin
s := pri[itemindex].pPrinterName + ',' + pri[itemindex].pDriverName +
',' + pri[itemindex].pPortName;
ini := TInifile.create('win.ini');
ini.writestring('windows', 'device', s);
ini.free;
s := 'windows';
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, longint(pchar(s)));
end;
end;
end;