窗体上有个TComBox,命名为cb1
窗体名为Form1
procedure TForm1.FormCreate(Sender: TObject);
begin
cb1.items.assign(printer.printers);
end;
procedure TForm1.Button1Click(Sender: TObject);
var LPrinter:string;
PrinIniFile:Tinifile;
LStr:string;
begin
LStr:=printer.Printers[cb1.itemindex];
delete(Lstr,pos(' on ',Lstr),Length(LStr));
PrinIniFile:=TIniFile.Create('WIN.ini');
try
LPrinter:=PrinIniFile.ReadString('Devices',LStr,'');
PrinIniFile.writestring('windows','device',LStr+','+LPrinter);
finally
PrinIniFile.free;
end;
end;
***********************
uses printers;
procedure SetDefaultPrinter(const printerindex:integer);
Var
FHandle : THandle;
HPrt : THandle;
PrtInfo5: PPrinterInfo5;
FDevice: array[0..79] of char;
FDriver: array[0..79] of char;
FPort: array[0..79] of char;
begin
{printerindex为选中打印机的索引,如果使用打印机名称,则此句可忽略}
Printer.PrinterIndex := printerindex;
Printer.GetPrinter (FDevice, FDriver, FPort, FHandle);
OpenPrinter(FDevice, HPrt, nil);
if HPrt = 0 then
raise(Exception.Create('不能打开打印机'));
try
PrtInfo5 := GetPrinterInfo5(HPrt);
PrtInfo5.Attributes := PrtInfo5.Attributes +
PRINTER_ATTRIBUTE_DEFAULT;
SetPrinter(HPrt,5,PrtInfo5,PRINTER_CONTROL_SET_STATUS);
FreeMem(PrtInfo5);
finally
ClosePrinter(HPrt);
end;
end;