Solved

Setting up a printer from code

Posted on 2001-06-06
9
208 Views
Last Modified: 2010-04-06
Hi

I have created text based reports to be sent to a specific printer, and to a specific paper size.  Different reports (not using QuickReport) require different printer settings.

Is there a way to assign the printer and the paper size in code when the user clicks the button to print the report?  Sometimes the paper size is *custom* (preprinted forms)

The printers involved are on a NT network and are a laserjet and an old dot matrix printer.

Regards John
0
Comment
Question by:jdthedj
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6161944
hi,
heres an example i have of changing the papersize and bin:

unit Unit1;

interface

uses
  Windows, Messages,Printers, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port   : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if hDMode <> 0 then begin
    pDMode := GlobalLock(hDMode);
    if pDMode <> nil then begin

     {Set to legal}
      pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
      pDMode^.dmPaperSize := DMPAPER_LEGAL;

     {Set to custom size}
      pDMode^.dmFields := pDMode^.dmFields or
                          DM_PAPERSIZE or
                          DM_PAPERWIDTH or
                          DM_PAPERLENGTH;
      pDMode^.dmPaperSize := DMPAPER_USER;
      pDMode^.dmPaperWidth := 100 {SomeValueInTenthsOfAMillimeter};
      pDMode^.dmPaperLength := 100 {SomeValueInTenthsOfAMillimeter};

     {Set the bin to use}
      pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
      pDMode^.dmDefaultSource := DMBIN_MANUAL;

      GlobalUnlock(hDMode);
   end;
  end;
  Printer.PrinterIndex := Printer.PrinterIndex;  Printer.BeginDoc;
Printer.Canvas.Font.Name := 'Courier New';      Printer.Canvas.Font.Size := 10;
Printer.Canvas.Font.Color:=ClRed;
 Printer.Canvas.TextOut(100,100, 'Test 1');  Printer.EndDoc;
end;
end.


i advise have a look in windows.pas for more paper sizes under:
{ paper selections } heading.there is quite a lot.
0
 
LVL 9

Accepted Solution

by:
ginsonic earned 50 total points
ID: 6162327
For selecting printer read next article at:
http://www.delphi3000.com/article.asp?ID=1114

For printer set ( all options ) you can use EDSPrint component( source code included on download )
http://www.onedomain.com/EDSPrint.cfm

Or - for first part - can use this code :

unit Chg_prn;

interface
uses WinTypes, WinProcs, Classes, sysutils, printers, dialogs, messages;

procedure ChangeDefaultPrinter;

implementation
procedure ChangeDefaultPrinter;
var szPrinterName, szIniInfo, szSection: PChar ;

begin
  try
   GetMem(szPrinterName,SizeOf(Char) * 256);                            {allocate memory}
   GetMem(szIniInfo,SizeOf(Char) * 256);
   GetMem(szSection,10) ;
   StrPCopy(szPrinterName,                                              {get name for printer selected in printerindex}
       Copy(Printer.Printers[Printer.PrinterIndex], 1,
       Pos('on', Printer.Printers[Printer.PrinterIndex]) - 2 ));
   GetProfileString('DEVICES', szPrinterName, nil, szIniInfo, 254) ;    {locate device info in win.ini}

   if szIniInfo^ <> #0 then
    begin                                                              {if device found, then..}
     StrCat(szPrinterName,',') ;                                        {prepare new device line}
     StrCat(szPrinterName,szIniInfo) ;
     WriteProfileString('Windows','DEVICE',szPrinterName) ;             {update ini file}
     StrCopy(szSection,'Windows') ;
     PostMessage(HWND_BROADCAST,WM_WININICHANGE,0,LongInt(szSection)) ; {notify all apps - ini has changed}
   end ;

   FreeMem(szPrinterName,SizeOf(Char) * 256) ;                          {release memory}
   FreeMem(szIniInfo,SizeOf(Char) * 256) ;
   FreeMem(szSection,10) ;

 except

   on E: EOutOfMemory do ShowMessage(E.Message) ;                       {handles no memory to allocate}
   on E: EInvalidPointer do ShowMessage(E.Message) ;                    {handles bad pointer}

 end ;

end;

end.

0
 
LVL 3

Expert Comment

by:VSF
ID: 6163428
This is a sample on how to print documents on Matrix printers setting the paper size by code.

Adjust paper Length an Width as you want!
Good luck!
VSF
www.enge.cjb.net
www.victory.hpg.com.br
UIN:14016999


procedure TFrmBoletos.CmdImprimirClick(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port   : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
VMULTA,VVALOR:REAL;
begin
    Printer.GetPrinter(Device, Driver, Port, hDMode);
    If hDMode <> 0 then
       begin
       pDMode := GlobalLock( hDMode );
       If pDMode <> nil then
          begin
          pDMode^.dmPaperSize   := DMPAPER_USER;
          pDMode^.dmPaperWidth  := 1600;   // determines paper width
          pDMode^.dmPaperLength := 1020;  //  determines paeper length
          pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE;
          GlobalUnlock( hDMode );
          end;
       end;
       Printer.BeginDoc;
       Printer.Canvas.Pen.Width := 5;
       Printer.Canvas.Font.Name := 'Arial';
       Printer.Canvas.Font.Size := 10;
       Printer.Canvas.TextOut(005,035,'Sample on how to set printer');
       Printer.Canvas.TextOut(620,035,'Use the coordenades to print anywhere');
       Printer.EndDoc;
   End;
end;
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:jdthedj
ID: 6165315
Hello inthe, ginsonic and VSF - thanks for the replies.

All the replies are good, and I now have the problem of who to award the points to.  How would I go about awarding points to more than one person?

John
0
 
LVL 17

Expert Comment

by:inthe
ID: 6165483
hi,
just pick the one which helps the most/suits you the best .
0
 
LVL 17

Expert Comment

by:inthe
ID: 6165520
i wasnt sure from your q if you needed to change the printer itself or just papersizes but if you do you can use code like below to get a list of printer names and use it with ginsonics code for actual changing the printer:

uses printers;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.clear;
listbox1.items.assign(Printer.printers);
end;


then use code me,vsf posted for changing the papersize.(i notice our code looks rather similar..)
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6166396
1. Use the good answers order .
2. Choice the best answer for you .
3. Share the points . I know that Ex-Ex can do that , but I don't remember how :P
Or can delete this questions and put two new 25 points questions one named 'POINTS FOR GINSONIC' and one 'POINTS FOR VSF'.

In any case I'm glad that my answer help you .
See you on net .
0
 
LVL 3

Author Comment

by:jdthedj
ID: 6166398
Thanks again inthe - I really appreciate the help.  I want to give both you and ginsonic some points - maybe if I put in another dummy question with the same title and you can answer it?  Let me know.

I will give the points here to ginsonic.

VSF:  This in no way infers your help is not useful, but inthe's answer and yours are similar, and inthe got in first.

John

0
 
LVL 17

Expert Comment

by:inthe
ID: 6166425
>>dummy question with the same title
>>and you can answer it?  Let me know

i dont mind ;-)



hey vsf's picture is on EOTW :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question