Select the default printer of the system  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 14/03/00

Tip accessed 593 times

 


It is sometimes interesting to be able to select the default printer of Windows. Mainly for those that have problems with QuickReport under Delphi3...
Here you have a code to select the predetermined printer from the system to your pleasure.
You must only put the printer number that you want in the variable ' PrinterNumber'


  • Add Printers and WinSpool in the uses of your form


     procedure TForm1.Button1Click(Sender: TObject);
     var
         NombreImpresora   : string;
         Ok                : boolean;
         MangoPrinter      : Integer;
         PrinterInfo2      : ^TPRINTERINFO2;
         Defaults          : TPRINTERDEFAULTS;
         Ocupa             : DWORD;
         PrinterNumber     : Integer;
     begin
        {Numero de impresora a seleccionar}
        {Printer Number to select}
        PrinterNumber :=0;
        NombreImpresora := Printer.Printers.Strings[PrinterNumber];
        {Eliminamos el "on LPT"}
        if Pos(' on ',NombreImpresora)<>0 then
          NombreImpresora:=Copy (NombreImpresora,1,Pos(' on ',NombreImpresora)-1);
    
        Defaults.DesiredAccess := PRINTER_ALL_ACCESS;
        Defaults.pDatatype := nil;
        Defaults.pDevMode := nil;
        Ok := OpenPrinter(PChar(NombreImpresora), MangoPrinter, @Defaults);
    
        if Ok then
        begin
          {Reservamos memoria, inicialmente 1000 bytes}
          GetMem(PrinterInfo2, 1000);
           Ok := GetPrinter(Integer(MangoPrinter), 2, PrinterInfo2, 1000, @Ocupa);
           {Si no cabe en 1000 bytes, reservamos más}
           if not Ok then
             if (Ocupa > 1000) then
             begin
               FreeMem(PrinterInfo2);
               GetMem(PrinterInfo2, Ocupa);
               GetPrinter(MangoPrinter, 2, PrinterInfo2, Ocupa,@Ocupa);
             end
             else ShowMessage('Error en GetPrinter:' +IntToStr(GetLastError));
    
             {La ponemos como impresora predeterminada para Windows}
             {Set printer like default printer system}
             PrinterInfo2^.Attributes := PrinterInfo2^.Attributes +
                                         PRINTER_ATTRIBUTE_DEFAULT;
             Ok := WinSpool.SetPrinter(MangoPrinter,  2,  PrinterInfo2 , 0);
             if not Ok then
               ShowMessage('Error en SetPrinter:' + IntToStr(GetLastError));
             FreeMem(PrinterInfo2);
           end
        else
           ShowMessage('Error en OpenPrinter:' + IntToStr(GetLastError));
        ClosePrinter(MangoPrinter);
     end;
    




    The trick made it in Delphi3, and there it worked perfectly, nevertheless, in Delphi4 there is a conflict with the variable MangoPrinter when being defined as integer.
    To solve it, simpley define it as THandle instead of integer:


     var
         NombreImpresora   : string;
         Ok                : boolean;
         MangoPrinter      : THandle;
    




    Updated at 14/03/2000 (Integer-THandle)