was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

34 Visitors Online


 
...globale Drucker Einstellungen speichern und laden?
Autor: Rob Schoenaker
[ Tip ausdrucken ]  

Tip Bewertung (19):  
     


{ Changing the properties of a global printer object is a tedious task.
The code below allows to save current settings of any installed printer
to a stream using the SavePrinterInfo procedure. These settings include
vendor specific settings. }

{ Using LoadPrinterInfo, one can retrieve saved settings and apply these
to the printer. }

unit PrinterIO;

interface

uses
  
Classes;

procedure SavePrinterInfo(APrinterName: PChar; ADestStream: TStream);
procedure LoadPrinterInfo(APrinterName: PChar; ASourceStream: TStream);

implementation

uses
  
Windows, SysUtils, WinSpool;

procedure SavePrinterInfo(APrinterName: PChar; ADestStream: TStream);
var
  
HPrinter : THandle;
  InfoSize,
  BytesNeeded: Cardinal;
  PI2: PPrinterInfo2;
  PrinterDefaults: TPrinterDefaults;
begin
  with 
PrinterDefaults do
  begin
    
DesiredAccess := PRINTER_ACCESS_USE;
    pDatatype := nil;
    pDevMode := nil;
  end;
  if OpenPrinter(APrinterName, HPrinter, @PrinterDefaults) then
  try
    
SetLastError(0);
    //Determine the number of bytes to allocate for the PRINTER_INFO_2 construct...
    
if not GetPrinter(HPrinter, 2, nil, 0, @BytesNeeded) then
    begin
      
//Allocate memory space for the PRINTER_INFO_2 pointer (PrinterInfo2)...
      
PI2 := AllocMem(BytesNeeded);
      try
        
InfoSize := SizeOf(TPrinterInfo2);
        if GetPrinter(HPrinter, 2, PI2, BytesNeeded, @BytesNeeded) then
          
ADestStream.Write(PChar(PI2)[InfoSize], BytesNeeded - InfoSize);
      finally
        
FreeMem(PI2, BytesNeeded);
      end;
    end;
  finally
    
ClosePrinter(HPrinter);
  end;
end;

procedure LoadPrinterInfo(APrinterName: PChar; ASourceStream: TStream);
var
  
HPrinter : THandle;
  InfoSize,
  BytesNeeded: Cardinal;
  PI2: PPrinterInfo2;
  PrinterDefaults: TPrinterDefaults;
begin
  with 
PrinterDefaults do
  begin
    
DesiredAccess := PRINTER_ACCESS_USE;
    pDatatype := nil;
    pDevMode := nil;
  end;
  if OpenPrinter(APrinterName, HPrinter, @PrinterDefaults) then
  try
    
SetLastError(0);
    //Determine the number of bytes to allocate for the PRINTER_INFO_2 construct...
    
if not GetPrinter(HPrinter, 2, nil, 0, @BytesNeeded) then
    begin
      
//Allocate memory space for the PRINTER_INFO_2 pointer (PrinterInfo2)...
      
PI2 := AllocMem(BytesNeeded);
      try
        
InfoSize := SizeOf(TPrinterInfo2);
        if GetPrinter(HPrinter, 2, PI2, BytesNeeded, @BytesNeeded) then
        begin
          
PI2.pSecurityDescriptor := nil;
          ASourceStream.ReadBuffer(PChar(PI2)[InfoSize], BytesNeeded - InfoSize);
          // Apply settings to the printer
          
if DocumentProperties(0, hPrinter, APrinterName, PI2.pDevMode^,
                                PI2.pDevMode^, DM_IN_BUFFER or DM_OUT_BUFFER) = IDOK then
          begin
            
SetPrinter(HPrinter, 2, PI2, 0);  // Ignore the result of this call...
          
end;
        end;
      finally
        
FreeMem(PI2, BytesNeeded);
      end;
    end;
  finally
    
ClosePrinter(HPrinter);
  end;
end;

end.

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners