Author Topic: Accessing Printer Information  (Read 1500 times)

SFA

  • Full Member
  • ***
  • Posts: 227
  • *
    • Stanley Fyffe Associates
Accessing Printer Information
« on: July 15, 2015, 08:35:26 AM »
I am stuck again. The paper sizes I often use (ISO A1 and ISO A0) are not in the list and return 'unknown'.  Is there an answer?

Thank you,

Stan
p. s. Here is the list from the help file.

dcPaperUnknown = -1,
dcPaper10x14 = 0,
dcPaper11x17 = 1,
dcPaperLetter = 2,
dcPaperLetterSmall = 3,
dcPaperLegal = 4,
dcPaperExecutive = 5,
dcPaperA3 = 6,
dcPaperA4 = 7,
dcPaperA4Small = 8,
dcPaperA5 = 9,
dcPaperB4 = 10,
dcPaperB5 = 11,
dcPaperCSheet = 12,
dcPaperDSheet = 13,
dcPaperESheet = 14,
dcPaperFanfoldLegalGerman = 15,
dcPaperFanfoldStdGerman = 16,
dcPaperFanfoldUS = 17,
dcPaperFolio = 18,
dcPaperLedger = 19,
dcPaperNote = 20,
dcPaperQuarto = 21,
dcPaperStatement = 22,
dcPaperTabloid = 23,
dcPaperEnvelope9 = 24,
dcPaperEnvelope10 = 25,
dcPaperEnvelope11 = 26,
dcPaperEnvelope12 = 27,
dcPaperEnvelope14 = 28,
dcPaperEnvelopeB4 = 29,
dcPaperEnvelopeB5 = 30,
dcPaperEnvelopeB6 = 31,
dcPaperEnvelopeC3 = 32,
dcPaperEnvelopeC4 = 33,
dcPaperEnvelopeC5 = 34,
dcPaperEnvelopeC6 = 35,
dcPaperEnvelopeC65 = 36,
dcPaperEnvelopeDL = 37,
dcPaperEnvelopeItaly = 38,
dcPaperEnvelopeMonarch = 39,
dcPaperEnvelopePersonal = 40,
dcPaperCustom = 41



SEMPER IN EXCRETIA

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4092
Re: Accessing Printer Information
« Reply #1 on: July 15, 2015, 08:53:57 AM »
not in the DesignCAD sdk, sorry. Though I'm not sure why those aren't on the list. There are Windows API calls that can handle this, but I'll have to dig 'em up before I can post an example.
2016 marks my 24th year in DesignCAD-Land!

SFA

  • Full Member
  • ***
  • Posts: 227
  • *
    • Stanley Fyffe Associates
Re: Accessing Printer Information
« Reply #2 on: July 15, 2015, 09:13:29 AM »
DT,

Thank you very much. This is way beyond the call of duty.

Stan
SEMPER IN EXCRETIA

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4092
Re: Accessing Printer Information
« Reply #3 on: July 15, 2015, 10:08:53 AM »
Here is a link with a number of Delphi examples on how to perform various printing tasks:

http://www.swissdelphicenter.ch/torry/printing.php

Since they're Delphi, they ought to work ok with Lazarus, but I haven't tested any of them.

Another very useful Delphi link:
http://www.efg2.com/Lab/
or more specifically for printer info:
http://www.efg2.com/Lab/Library/Delphi/Printing/index.html
« Last Edit: July 15, 2015, 11:06:35 AM by DrollTroll »
2016 marks my 24th year in DesignCAD-Land!

SFA

  • Full Member
  • ***
  • Posts: 227
  • *
    • Stanley Fyffe Associates
Re: Accessing Printer Information
« Reply #4 on: July 18, 2015, 04:26:27 AM »
I joined the Lazarus forum (which is a simple machines forum just like this one) and think I may now have the necessary (Delphi) code to read and write the printer settings from the registry - see below- but I have no idea how to fit it into a Lazarus application.

I would be really grateful if DT or anyone else could help. We could end up with a really useful little add-on to DesignCAD!

Thank you.

Stan

EXPLANATION
Windows remembers only changes made to the default settings, using the printer icon in the control panels printers applet. A solution for you would be to save the printers devicemode record, the public part at least:

CODE


uses
  printers, registry;

{$R *.dfm}

procedure SaveCurrentPrinterSettings(reg: TRegistry);
var
  Device, Driver, Port: array[0..80] of Char;
  DevMode: THandle;
  pDevmode: PDeviceMode;
begin
  Printer.GetPrinter(Device, Driver, Port, DevMode);
  if Devmode <> 0 then
  begin
    {lock it to get pointer to DEVMODE record}
    pDevMode := GlobalLock(Devmode);
    if pDevmode <> Nil then
    try
      reg.WriteBinaryData(device, pDevmode^, sizeof(TDevicemode));
    finally
      {unlock devmode handle.}
      GlobalUnlock(Devmode);
    end;
  end;
end;

procedure LoadCurrentPrinterSettings(reg: TRegistry);
var
  Device, Driver, Port: array[0..80] of Char;
  DevMode: THandle;
  pDevmode: PDeviceMode;
begin
  Printer.GetPrinter(Device, Driver, Port, DevMode);
  if Devmode <> 0 then
  begin
    {lock it to get pointer to DEVMODE record}
    pDevMode := GlobalLock(Devmode);
    If pDevmode <> Nil then
    try
      reg.ReadBinaryData(device, pDevmode^, sizeof(TDevicemode));
    finally
      {unlock devmode handle.}
      GlobalUnlock(Devmode);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  reg: TRegistry;
begin
  reg:= TRegistry.Create(KEY_READ OR KEY_WRITE);
  try
    reg.RootKey := HKEY_CURRENT_USER;
    if reg.OpenKey('Software\PB\Test', true) then
    try
      SaveCurrentPrinterSettings(reg);
    finally
      reg.CloseKey;
    end
  finally
    reg.free
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  reg: TRegistry;
begin
  reg:= TRegistry.Create(KEY_READ);
  try
    reg.RootKey := HKEY_CURRENT_USER;
    if reg.OpenKey('Software\PB\Test', false) then
    try
      LoadCurrentPrinterSettings(reg);
    finally
      reg.CloseKey;
    end
  finally
    reg.free
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  printdialog1.execute
end;
SEMPER IN EXCRETIA

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4092
Re: Accessing Printer Information
« Reply #5 on: July 20, 2015, 08:33:42 AM »
I'll take a look as soon as I can, but I'm in the middle of a serious project just now. I might be able to take a look next week.

DT
2016 marks my 24th year in DesignCAD-Land!

SFA

  • Full Member
  • ***
  • Posts: 227
  • *
    • Stanley Fyffe Associates
Re: Accessing Printer Information
« Reply #6 on: July 20, 2015, 09:41:34 AM »
Thank you. That would be wonderful.

Regards,

Stan
SEMPER IN EXCRETIA

SFA

  • Full Member
  • ***
  • Posts: 227
  • *
    • Stanley Fyffe Associates
Re: Accessing Printer Information
« Reply #7 on: August 05, 2015, 08:30:58 AM »
DT,

I assume you have been kept pretty busy by the launch of DCmax25 but wonder if you (or any one else who can work Lazarus) could have a look at this one?

Thank you.

Stan
SEMPER IN EXCRETIA