没有问题,我现在的系统是在三台不同的打印机(LQ300K+,LQ635K,HP2200LaserJet)不断的切换。给一个动态设置函数:
procedure TPublic.SetDefaultPrinter(PrinterName: String);
var
j: Integer;
Device : PChar;
Driver : Pchar;
Port : Pchar;
HdeviceMode: Thandle;
aPrinter : TPrinter;
begin
Printer.PrinterIndex := -1;
getmem(Device, 255) ;
getmem(Driver, 255) ;
getmem(Port, 255) ;
aPrinter := TPrinter.create;
for j := 0 to Printer.printers.Count-1do
begin
if Printer.printers[j] = PrinterName then
begin
aprinter.printerindex := j;
aPrinter.getprinter(device, driver, port, HdeviceMode) ;
StrCat(Device, ',') ;
StrCat(Device, Driver ) ;
StrCat(Device, Port ) ;
WriteProfileString('windows', 'device', Device) ;
StrCopy( Device, 'windows' ) ;
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0 , Longint(@Device)) ;
end;
end;
Freemem(Device, 255) ;
Freemem(Driver, 255) ;
Freemem(Port, 255) ;
aPrinter.Free;
end;
注意: 在调用本函数给PrinterName赋值时一定要与你的系统内相应的打印机名称相同