whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

22 Visitors Online


 
...Save the printer Setting to file?
Autor: Remmelt Koenes
Homepage: http://www.triplesoftware.com/
[ Print tip ]  

Tip Rating (17):  
     


{
  Create a unit PrinterSetup and place code in this unit
  Do the following to save the printer config to file
}

procedure TForm1.Button1Click(Sender: TObject);
var
  
PrinterSetup: TPrinterSetup
begin
  
PrinterSetup := TPrinterSetup.Create;
  PrinterSetup.SaveSetup(FileName);
  //where file name is a string to the location of the File ex.'c:\print.cfg'
  
PrinterSetup.Free;
end

{***********************************}

unit PrinterSetup;

interface

uses
  
printers,
  windows,
  SysUtils,
  Classes,
  WinSpool;

type
  
TPrinterSetup = class

  private
    
Device, Driver, Port: array[0..CCHDEVICENAME] of char;
    DeviceMode: THandle;
    procedure Refresh;

  protected

  public
    procedure 
SaveSetup(FileName: TFilename);
    procedure LoadSetup(FileName: TFilename);
  end;
  TPrinterConfig = record
    
ADevice, ADriver, APort: array[0..CCHDEVICENAME] of char;
    SizeOfDeviceMode: DWORD;
  end;

implementation

procedure 
TPrinterSetup.Refresh;
begin
  
Printer.GetPrinter(Device, Driver, Port, DeviceMode);
end;

procedure TPrinterSetup.SaveSetup(FileName: TFilename);
var
  
StubDevMode: TDeviceMode;
  SetupPrinter: TPrinterConfig;
  FPrinterHandle: THandle;
  fFileConfig: file of TPrinterConfig;
  fFileDevMode: file of Char;
  pDevMode: PChar;
  Contador: Integer;
begin
  
Refresh;
  with SetupPrinter do
  begin
    
StrLCopy(ADevice, Device, SizeOf(ADevice));
    StrLCopy(ADriver, Driver, SizeOf(ADriver));
    StrLCopy(APort, Port, SizeOf(APort));
    OpenPrinter(Device, FPrinterHandle, nil);
    SizeOfDeviceMode := DocumentProperties(0, FPrinterHandle, Device,
      StubDevMode, StubDevMode, 0);
  end;
  AssignFile(fFileConfig, FileName);
  ReWrite(fFileConfig);
  Write(fFileConfig, SetupPrinter);
  CloseFile(fFileConfig);
  AssignFile(fFileDevMode, FileName);
  Reset(fFileDevMode);
  Seek(fFileDevMode, FileSize(fFileDevMode));
  pDevMode := GlobalLock(DeviceMode);
  for Contador := 0 to SetupPrinter.SizeOfDeviceMode - 1 do
  begin
    
Write(fFileDevMode, pDevMode[Contador]);
  end;
  CloseFile(fFileDevMode);
  GlobalUnLock(DeviceMode);
end;


procedure TPrinterSetup.LoadSetup(FileName: TFilename);
var
  
SetupPrinter: TPrinterConfig;
  fFileConfig: file of TPrinterConfig;
  fFileDevMode: file of Char;
  ADeviceMode: THandle;
  pDevMode: PChar;
  Contador: Integer;
begin
  if 
FileExists(FileName) then
  begin
    
AssignFile(fFileConfig, FileName);
    Reset(fFileConfig);
    read(fFileConfig, SetupPrinter);
    CloseFile(fFileConfig);
    AssignFile(fFileDevMode, FileName);
    Reset(fFileDevMode);
    Seek(fFileDevMode, SizeOf(SetupPrinter));
    ADeviceMode := GlobalAlloc(GHND, SetupPrinter.SizeOfDeviceMode);
    pDevMode := GlobalLock(ADeviceMode);
    for Contador := 0 to SetupPrinter.SizeOfDeviceMode - 1 do
    begin
      
read(fFileDevMode, char(pDevMode[Contador]));
    end;
    CloseFile(fFileDevMode);
    GlobalUnLock(ADeviceMode);
    Printer.SetPrinter(SetupPrinter.ADevice, SetupPrinter.ADriver,
      SetupPrinter.APort, ADeviceMode);
  end;
end;

end.

 

Rate this tip:

poor
very good


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