We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

how to use TPrintDialog to print?

lhlplmy
lhlplmy asked
on
Medium Priority
3,043 Views
Last Modified: 2010-04-04
how to use TPrintDialog to print contents ,but the print dialog form don't appear?
Comment
Watch Question

Commented:
hi lhlplmy,

Can you be more specific? Because this is well explained in delphi help :

This example uses a button, a Page Control, and a Print dialog box on a form. When the user clicks the button, the print dialog is displayed.  The user can select any subset of the pages in the page control for printing.  The selected pages are then printed.
To run this example successfully, you must add the Printers unit to the uses clause of your unit.

procedure TForm1.Button1Click(Sender:TObject);

var
  I, Start, Stop: Integer;
begin
PrintDialog1.Options := [poPageNums, poSelection];
PrintDialog1.FromPage := 1;
PrintDialog1.MinPage := 1;
PrintDialog1.ToPage := PageControl1.PageCount;
PrintDialog1.MaxPage := PageControl1.PageCount;
if PrintDialog1.Execute then
  begin
  { determine the range the user wants to print }
  with PrintDialog1 do
  begin
    if PrintRange = prAllPages then

      begin
      Start := MinPage - 1;
      Stop := MaxPage - 1;
      end
    else if PrintRange = prSelection then
      begin
      Start := PageControl1.ActivePage.PageIndex;
      Stop := Start;
      end
    else  { PrintRange = prPageNums }
      begin
      Start := FromPage - 1;
      Stop := ToPage - 1;
      end;
  end;
  { now, print the pages }
  with Printer do

    begin
    BeginDoc;
    for I := Start to Stop do
      begin
      PageControl1.Pages[I].PaintTo(Handle, 10, 10);
      if I <> Stop then
        NewPage;
      end;
    EndDoc;
    end;
  end;
end;

So for easy example :

1. put a button on the form
2. add the Printers unit to the uses clause of your unit

3. In the click event of the button add following code :

if Printdialog1.Excute then begin
 // do print stuff here
end
 else // user aborted print

Execute starts the printdialog and returns a boolean variable :

* false user clicked on cancel
* true user clicked on OK.

Regards, Zif.

Author

Commented:
i want the printer dialog form don't appear in the desktop.how to do it?

Commented:
lhlplmy,

Sorry misunderstanding of your Q.
 Euhm, if you don't want to show it up... Why then do you want to use it?

Zif.

Commented:
Hi,
Look at question Q.10071763.
In there is an example of mine of how to get/set printer properties w/o the printdialog.

Zif, so overworked you can't read? ;-)

/// John

Commented:
BTW, check at the end, not at the beginning...

/// John

Commented:
erajoj, reading is ok, but understanding not. :-) Thought that lhlplmy wanted to see the dialog, but that it didn't show on the screen. Zif.
333

Commented:
Use TPrinter instead of TPrintDialog

Author

Commented:
i want to get the contents on my canon printer , but the PrinterDialog won't appear on the screen ,otherwise i should press the 'ok' button to close the PrinterDialog ,then my canon printer work.

thank you everyone.

Commented:
Huh?
Sorry! I still don't seem to understand what you want.

/// John

Commented:
lhlplmy, what is your native language? your english is good but a little bit confusing.

please, just explain what you want :

1. Get your printer properties
   or Get your paper properties
   --> what do you want to do with it?
2. Print some stuff?

.

Zif.
???

what do you mean by "contents" - the settings or maybe the queue entries?

is it possible that "PrinterDialog" in your words does not mean "TPrintDialog"??

i am sure we will be able to help you, just help us with the understanding of your question.

so long,

Black Death.

Author

Commented:
     In os win95 ,the priter dialog will appear when you printe a file in Microsoft Word ,you can choose what printer to use ,how many copies you wanted ,and from which page to printe .Then
you press the 'ok' button to let the printer printe the file.
     In my program , i want to print some information(or ascii file) in memo , and i want  to  control the print  variables (about what printer to use ,how many copies ,and so on) in my program . And when i press 'print' button ,the print dialog won't appear,and  the printer print the file  . Maybe i can control the 'ok' button in print dialog , but i don't know how to do .So
the question  is that  how to avoid the printer dialog appearing  when you call the procedure TPrintDialog.Execute to print file in memo and the printer can print my file.

Commented:
If you want to do this, you don't need to use the printdialog. The printdialog is just intended to let the user specify the printer stuff. You don't want to let the user choose, so you don't need the printerdialog! Zif.

Commented:
lhlplmy,

You've to use the Printer unit only :

use getprinter, setprinter to get or set the printer.

To print use begindoc, enddoc :

  with Printer do
    begin
     BeginDoc;
       // put all the needed print stuff here
       // i.e. what you want to print etc.
     EndDoc;
      // with EndDoc the printer starts printing
   end;

Zif.
or use property printerindex to select the printer.
(-1 = default printer)

Black Death.
say, guys:

on my screen some weird solid black rectancles appear on the right of the text boxes, while these are of half the usual width. is this the same with yours?

Commented:
same problem here.

Author

Commented:
ok , thank you ,zifnab.
you 'd better lock the question.

and I have another problem waiting for answer,can you help me?

Commented:
lhlplmy,

 Is everything working now?

Zif.

Author

Commented:
zif,
thank you .It's ok now.


Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Adjusted points to 90

Commented:
I have a problem with the qreport.printersetting.printerindex
Because when i use it, i lose a the printer setup i determine in the properties of the qreport (page size ...) the print is not well.
But when i put this printer by default in windows before lunching my application it works well
What the problem ??

Thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.