* * *

Author Topic: [SOLVED] Setting printer properties in PrintDialog  (Read 3125 times)

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
[SOLVED] Setting printer properties in PrintDialog
« on: May 31, 2018, 12:38:59 pm »
Hi, I want to change some printer settings before printing. (By default, my printer prints only one side and color, but I want to print double-sided and grayscale). I am using a TPrintDialog. When it is displayed, I use the properties button to access the printer properties dialog box that I attached below. Then, I modify the desired options, I accept and send my document to the printer, but the document is printed in color and on one side only, anyway.

Is there something that I am forgetting? Does this dialog work correctly?

This is the code (very simple):

Code: Pascal  [Select]
  1.   with PrintDialog1 do
  2.   begin
  3.     Copies := 1;
  4.     MinPage := 1;
  5.     MaxPage := doc.PageCount;
  6.     PrintRange := prAllPages;
  7.     if Execute then
  8.     begin
  9.        // Send data to Printer
  10.     end;
  11.   end;                        

Edit: this test was made on Windows 8. I will test on Windows 10 and Linux, too.
« Last Edit: June 07, 2018, 12:23:54 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

jamie

  • Hero Member
  • *****
  • Posts: 785
Re: Setting printer properties in PrintDialog
« Reply #1 on: May 31, 2018, 01:44:12 pm »
Maybe you need to refresh the printer object afterwards ?

Printer.Refresh.


RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #2 on: May 31, 2018, 02:16:20 pm »
No way. Same result after adding "Printer.Refresh". It seems that the changes I make in the settings are lost/forgotten after closing the TPrintDialog.

Code: Pascal  [Select]
  1.   with PrintDialog1 do
  2.   begin
  3.     Copies := 1;
  4.     MinPage := 1;
  5.     MaxPage := doc.PageCount;
  6.     PrintRange := prAllPages;
  7.     if Execute then
  8.     begin
  9.       Printer.Refresh;
  10.        // Send data to Printer
  11.     end;
  12.   end;      
To err is human, but to really mess things up, you need a computer.

wp

  • Hero Member
  • *****
  • Posts: 4808
Re: Setting printer properties in PrintDialog
« Reply #3 on: May 31, 2018, 03:13:36 pm »
IIRC the printer dialogs are only provided for the user to change parameters. The settings, however, are not transferred to the printer, nor are they stored anywhere automatically. It is your responsibility to apply the settings yourself. Example:
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   PrintDialog1.Copies := Printer.Copies;
  4.   if PrintDialog1.Execute then begin
  5.     Printer.Copies := PrintDialog1.Copies;
  6.   end;
  7. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #4 on: May 31, 2018, 03:35:50 pm »
Hello, wp

IIRC the printer dialogs are only provided for the user to change parameters. The settings, however, are not transferred to the printer, nor are they stored anywhere automatically. It is your responsibility to apply the settings yourself. Example:
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   PrintDialog1.Copies := Printer.Copies;
  4.   if PrintDialog1.Execute then begin
  5.     Printer.Copies := PrintDialog1.Copies;
  6.   end;
  7. end;

I check and use the values in PrintDialog1 (Copies, PrintRange, FromPage, ToPage, etc), but I don't know how to access the values changed in the dialog that opens when the user clicks the button labeled "settings". Can I access these values (duplex, color printing, etc.) by checking some properties of Printer? ... If these values are not transferred to the printer, where are they stored?

edit: I do not know how the temporary configuration of the printer works. What happens when the user changes the configuration using the "settings" dialog offered by TPrinterDialog? Where does Lazarus save the changed parameters? Are they automatically applied to the printout or must the programmer access them to apply them manually?
« Last Edit: May 31, 2018, 04:10:01 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

wp

  • Hero Member
  • *****
  • Posts: 4808
Re: Setting printer properties in PrintDialog
« Reply #5 on: May 31, 2018, 04:05:10 pm »
Which "Settings" button?

I am no expert in printing since I try to avoid print-outs as much as possible. But I suspect that modern OS now offer more capabilities than are supported by the TPrintDialog, TPrintSetupDialog and TPageSetupDialog, as well as the TPrinter class. For example execute "Print" of MS Word and you'll see a dialog which is completely different from those provided by the LCL. And I also think that these new dialogs interact strongly with the printer driver making sure that the option "Duplex" will not be offered if the printer cannot print in duplex mode.

So, the only help I can give is to recommand searching the web for OS-specific programming interface to current printer features. On https://stackoverflow.com/questions/28514198/delphi-duplex-printing, for example, you find a discussion of the duplex feature (I did not read it and don't know whether the article is helpful or not).

As for the properties "PrintRange, "FromPage", "ToPage" of the TPrintDialog I think that they are not related to the printer but to your application. This means you must know how many pages your document contains and store the number of the first and last page to be printed somewhere. The PrintDialog just offers for you a possibility to provide a standardized way to change these settings. Then, when the document is printed, you send only those pages to the printer which have the corresponding page numbers. The TPrinter class does not know where these pages are.



« Last Edit: May 31, 2018, 04:16:06 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #6 on: May 31, 2018, 04:13:36 pm »
This "settings" button. (See attached image).

My doubt is: if this settings dialog were not useful, why would it be accessible?
« Last Edit: May 31, 2018, 04:21:54 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

wp

  • Hero Member
  • *****
  • Posts: 4808
Re: Setting printer properties in PrintDialog
« Reply #7 on: May 31, 2018, 04:18:47 pm »
This "settings" button.
I see (I should have seen it by myself...)

But this button leads to a printer-specific dialog. LCL seems to support only a few of the settings provided. As I said, you must search the web for information how to access the printer-specific and OS-specific information. But maybe somebody else knows better than me here...
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #8 on: May 31, 2018, 04:25:18 pm »
Thanks, wp

But this button leads to a printer-specific dialog. LCL seems to support only a few of the settings provided. As I said, you must search the web for information how to access the printer-specific and OS-specific information. But maybe somebody else knows better than me here...

As I said, if this settings dialog is not useful, why is it accessible? The user sees it, he/she changes things and then the programmer cannot make honour to the user's wants? Can I hide this "settings" button?

Edit: I tried to access PrintDialog1.Components, to hide the "settings" button, but ComponentCount is 0.
« Last Edit: May 31, 2018, 04:40:49 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #9 on: May 31, 2018, 06:12:08 pm »
I made tests on Windows 8 and Windows 10 with Lazarus 1.8.4; and Linux Mint with Lazarus 1.8.2. In these 3 cases, I opened the "settings" window using the button in the PrintDialog. Then, I changed the settings to print in grayscale, accepted and printed. In all cases, the document was printed in color.  :(
To err is human, but to really mess things up, you need a computer.

wp

  • Hero Member
  • *****
  • Posts: 4808
Re: Setting printer properties in PrintDialog
« Reply #10 on: May 31, 2018, 07:23:19 pm »
As I said, the settings made in the print dialogs are not transferred to the printer. You must send a grayscale document to the printer if you do not want to print in color. The problem is "only" how to read the color/grayscale setting from the printer driver dialog...
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jamie

  • Hero Member
  • *****
  • Posts: 785
Re: Setting printer properties in PrintDialog
« Reply #11 on: June 01, 2018, 01:54:17 am »
Code: Pascal  [Select]
  1. var
  2.   i, j: integer;
  3.   ppix,ppiy,mmx,mmy: integer;
  4.   XForm: TXForm;
  5.   Device, Driver, Port: array[0..80] of Char;
  6.   DevMode: THandle;
  7.   pDevmode: PDeviceMode;
  8.   insert_comm: wideString;
  9. begin
  10.   with PrintDialog1 do
  11. if Execute then
  12.   begin
  13.  
  14.     with printer do
  15.       begin
  16.  
  17.         GetPrinter(Device, Driver, Port, DevMode);
  18.         if Devmode <> 0 then begin
  19.            // lock it to get pointer to DEVMODE record
  20.           pDevMode := GlobalLock(Devmode);
  21.         if pDevmode <> nil then
  22.           try
  23.             with pDevmode^ do begin
  24.               dmDuplex := DMDUP_VERTICAL;
  25.               dmFields := dmFields or DM_DUPLEX;
  26.             end;
  27.  
  28.             BeginDoc;
  29.  
  30.             ppix:=GetDeviceCaps(Handle,LOGPIXELSX);
  31.             ppiy:=GetDeviceCaps(Handle,LOGPIXELSY);
  32.             mmx:=round(ppix/25.4);
  33.             mmy:=round(ppiy/25.4);
  34.  
  35.             SetGraphicsMode(Canvas.Handle, GM_ADVANCED);
  36.  
  37.  

Something I've found,  You can see  how to access the DeviceMode record to set the fields
this is for windows of course...
I've used code similar to this in the past.

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #12 on: June 01, 2018, 08:51:06 am »
My conclusion is that if I want to allow the user print something in a multiplatform application, I must dessign my own PrintDialog, wich will not have any "fake" and useless "settings" button.  :(
To err is human, but to really mess things up, you need a computer.

wp

  • Hero Member
  • *****
  • Posts: 4808
Re: Setting printer properties in PrintDialog
« Reply #13 on: June 01, 2018, 09:12:23 am »
jamie, your code seems to be for Delphi, it does not compile with Lazarus (TPrinter has no method GetPrinter in Lazarus).
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Setting printer properties in PrintDialog
« Reply #14 on: June 01, 2018, 09:45:58 am »
I think we are discussing about a feature not yet developed.

TPrinter does not have many properties you can check (copies, orientation, and papersize, mainly):
http://lazarus-ccr.sourceforge.net/docs/lcl/printers/tprinter.html

Am I a fool if I think TPrinter must have some other properties like "ColorMode" or "DuplexMode"?

Code: Pascal  [Select]
  1. // Types suggestion
  2. TColorMode = (cmColor, cmGrayscale, cmBW);
  3. TDuplexMode = (dmNone, dmLongEdge, dmShortEdge);
« Last Edit: June 01, 2018, 09:57:04 am by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus