PrintDialog 可以选择打印机, 如何将选中的打印机设置为windows 系统的缺省打印机, 谢谢!

解决方案 »

  1.   

    转贴:
    窗体上有个TComBox,命名为cb1
    窗体名为Form1procedure 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;