⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printer.htm

📁 对于学习很有帮助
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  Device := ADevice;
  Port := APort;
end;

function TPrinterDevice.IsEqual(ADriver, ADevice, APort: PChar): Boolean;
begin
  Result := (Device = ADevice) and (Port = APort);
end;


procedure TRawprinter.startraw;
var
  CTitle: array[0..31] of Char;
  CMode : Array[0..4] of char;
  DocInfo: TDocInfo;
  r : integer;
begin
  StrPLCopy(CTitle, Title, SizeOf(CTitle) - 1);
  StrPCopy(CMode, 'RAW');
  FillChar(DocInfo, SizeOf(DocInfo), 0);
  with DocInfo do
  begin
    cbSize := SizeOf(DocInfo);
    lpszDocName := CTitle;
    lpszOutput := nil;
    lpszDatatype :=CMode;
  end;
  with TPrinterDevice(Printers.Objects[PrinterIndex]) do
  begin
    DC2 := CreateDC(PChar(Driver), PChar(Device), PChar(Port), nil);
  end;
  SetAbortProc(dc2, AbortProc);
  r:=StartDoc(dc2, DocInfo);
end;

procedure TRawprinter.endraw;
var r : integer;
begin
  r:=windows.enddoc(dc2);
end;

type passrec = packed record
                 l : word;
                 s : Array[0..255] of char;
               end;
var pass : Passrec;
procedure TRawprinter.write(const s : string);
begin
  pass.l:=length(s);
  strpcopy(pass.s,s);
  escape(dc2,PASSTHROUGH,0,@pass,nil);
end;

procedure TRawprinter.writeln;
begin
  pass.l:=2;
  strpcopy(pass.s,#13#10);
  escape(dc2,PASSTHROUGH,0,@pass,nil);
end;

end.
</PRE><HR>

<P><H1><A NAME="printer8">Printing Tricks</P></A></H1>
<P><I>rgilland@ecn.net.au (Robert Gilland)</I></P>
<P><I>"Guy Vandenberg" &lt;guyvdb@MindSpring&gt;</I></P>

You are a genius. After pulling my hair out and downloading anything
that had anything to do with printing in delphi on the net and getting
nowhere fast. Your little piece of code actually made sence to me and
was userfrindly. I put it together with other code other small hints
on printing and I got the below. Use it to your delight. You were the
initiator. <p>

<HR><PRE>const INCHES_PER_MILIMETER : Real  = 0.04;

type
  TOffset =   record
               X,Y: Integer;
              end;

var FDeviceName : String;  {Get the name}
    FPageHeightPixel, FPageWidthPixel : Integer ;  {Page height and Page Width}
    FOrientation : TPrinterOrientation; {Orientation}
    FPrintOffsetPixels : TOffset;
    FPixelsPerMMX,FPixelsPerMMY: Real;
    MMSize, FPageHeightMM : Integer;
    TheReport, TheHead, HeadLine, RecordLine, TFname, TLname :String;

procedure TMissing_Rep.GetDeviceSettings;

var
  retval: integer;
  PixX, PixY: Integer;

begin
    FDeviceName := Printer.Printers[Printer.PrinterIndex];  {Get the name}
    FPageHeightPixel := Printer.PageHeight;                 {Page height}
    FPageWidthPixel := Printer.PageWidth;                   {Page Width}
    FOrientation := Printer.Orientation;
{Orientation}
    {Get the printable area offsets}
    {$IFDEF WIN32}
       FPrintOffsetPixels.X := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX);
       FPrintOffsetPixels.Y := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY);
    {$ELSE}
       retval := Escape(Printer.Handle,GETPRINTINGOFFSET,
                        0, nil, @FPrintOffsetPixels);
    {$ENDIF}
    {Get Pixels per Milimeter Ratio}
    PixX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
    PixY :=  GetDeviceCaps(Printer.Handle, LOGPIXELSY);
    FPixelsPerMMX := INCHES_PER_MILIMETER * PixX;
    FPixelsPerMMY := INCHES_PER_MILIMETER  * PixY;
    FPageHeightMM := Round(FPageHeightPixel/FPixelsPerMMY);
 end;

function TMissing_Rep.PutText(mmX,mmY: Integer; S: string; LeftAlign:
Boolean): boolean;
var
  X, Y: Integer;
  align: WORD;
begin
  if LeftAlign then
    align :=  SetTextAlign(Printer.Handle,TA_BOTTOM or TA_LEFT)
  else
    align :=  SetTextAlign(Printer.Handle,TA_BOTTOM or TA_RIGHT);
  result := FALSE; {Assume fail}
  X := Trunc(mmX * FPixelsPerMMX) - FPrintOffsetPixels.X;
  Y := Trunc(mmY * FPixelsPerMMY) - FPrintOffsetPixels.Y;
  if X < 0 then exit;
  if Y < 0 then exit;
  Printer.Canvas.TextOut(X,Y,S);
  result := TRUE;
end;

procedure TMissing_Rep.Print_ButClick(Sender: TObject);

var PixelSize: Integer;

begin
Print_But.Enabled := False;
if PrintDialog1.Execute then
 begin
 Printer.Canvas.Font := Missing_Rep.Font;
 PixelSize := Printer.Canvas.TextHeight('Yy');
 MMSize := Round(PixelSize/FPixelsPerMMY);
 Printer.Title := 'Breast Cancer Project Missing Report';
 Printer.BeginDoc;                        { begin to send print job to printer }
 PrintGenerator;
 Printer.EndDoc;                 { EndDoc ends and starts printing print job }
 end;
 Print_But.Enabled := True;
 end;

procedure TMissing_Rep.PrintGenerator;

Var
  yLoc , NumRows, TheRow :Integer;

  procedure Heading;
  begin
   yLoc := 20;
   PutText(20, 20, TheHead, TRUE);
   yLoc := yLoc + MMSize;
   PutText(20,  yLoc, StringGrid1.Cells[0,0], TRUE);
   PutText(60,  yLoc, StringGrid1.Cells[1,0], TRUE);
   PutText(100, yLoc, StringGrid1.Cells[2,0], TRUE);
   PutText(120, yLoc, StringGrid1.Cells[3,0], TRUE);
   PutText(150, yLoc, StringGrid1.Cells[4,0], TRUE);
   yLoc := yLoc + MMSize;
 end;

  procedure Footer;
  begin
  PutText(100,FPageHeightMM,InttoStr(Printer.PageNumber), TRUE);
  end;

begin
   Heading;
   TheRow := 1;
   while (TheRow < StringGrid1.RowCount) do
   begin
       if (yLoc > (FPageHeightMM - MMSize)) then
	   begin
		  Footer;
		  Printer.NewPage;
		  Heading;
  	   end;
	 TheGauge.Progress := Round(100 * TheRow/(StringGrid1.RowCount - 1));
	 PutText(20,  yLoc, StringGrid1.Cells[0,TheRow], TRUE);
	 PutText(60,  yLoc, StringGrid1.Cells[1,TheRow], TRUE);
	 PutText(100, yLoc, StringGrid1.Cells[2,TheRow], TRUE);
	 PutText(120, yLoc, StringGrid1.Cells[3,TheRow], TRUE);
	 PutText(150, yLoc, StringGrid1.Cells[4,TheRow], TRUE);
	 yLoc := yLoc + MMSize;
	 TheRow := TheRow + 1;
 end;
Footer;
end;
</PRE><HR>

<P><H1><A NAME="printer9">Passthough escape function</P></A></H1>
<P><I>"Joe C. Hecht" &lt;jhecht@corp.borland.com&gt;</I></P>

Gilberto Beda wrote:
<PRE>> 
someone knows why some windows drivers don't work with a passthrough in
Escape function? 
 
Is it possible to know if a driver printer support the PASSTROUGH
function? I believe Dos apps in 95 use the same function if I command
"copy file &gt; lpt1" Windows95 use the predefint drivers to spool the
report to printer.
</PRE>

Although Delphi's TPrinter unit makes it easy to interface
to a given printer, there are times when you may need
to drop down to the printers level and send device specific
escape sequences. Under sixteen bit versions of Windows, this
was as easy as opening the printer port, but under Windows NT,
directly accessing the hardware is is illegal. One solution
is to use the Windows "PASSTHROUGH" escape to send an escape
sequence directly to the printer. In order to use the
"PASSTHROUGH" escape, it must be supported by the printer
driver. Be forwarned that not all printer drivers will support
this feature.<p>

It's worth noting that the "PASSTHROUGH" escape is documented
as obsolete for thirty-two bit applications. It should be a
number of years before this escape goes by the way, since
it is used in many commercial applications.<p>

The example code presented is not targeted to any specific
printer model. You will need to know the correct escape
sequences to send to the printer you are interfacing to.
Note that you must still call the BeginDoc and EndDoc methods
of TPrinter. During the BeginDoc call, the printer driver
initializes the printer as necessary, and during the EndDoc
call, the printer driver will uninitialize the printer and
eject the page. When you do make your escape call, the printer
may be set for the current windows mapping mode if the printer
supports scaling internaly. Technically, you should not do
anything that would cause the printer memory to be reset,
or eject a page with an escape sequence. In other words,
try to leave the printer in the same state it was in when
you made your escape. This is more important on intellegent
printers (Postscript printers), and not important at all on
a standard TTY line printer, where you can do just about
anything you like, including ejecting pages.<p>

<I>Code Example:</I><p>

You will need to declare a structure to hold the buffer you are
sending. The structure of the buffer is defined as a word containing
the length of the buffer, followed by the buffer containing the data.<p>

Before making the escape call to pass the data, we will use
the escape "QUERYESCSUPPORT" to determine if the "PASSTHROUGH"
escape is supported by the print driver.<p>

Finally, be aware that your data will be inserted directly into
the printers data stream. On some printer models (Postscript),
you may need to add a space to the start and end of your data
to separate your data from the printer drivers data.<p>

(Postscript is a Registered Trademark of Adobe Systems Incorporated) <p>

*)

<HR><PRE>
unit Esc1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, 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

{ add the printers unit }
uses
   Printers;

{$R *.DFM}

{ declare the "PASSTHROUGH" structure }
type TPrnBuffRec = record
  BuffLength : word;
  Buffer : array [0..255] of char;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Buff : TPrnBuffRec;
  TestInt : integer;
  s : string;
begin

{ Test to see if the "PASSTHROUGH" escape is supported }
  TestInt := PASSTHROUGH;
  if Escape(Printer.Handle,
            QUERYESCSUPPORT,
            sizeof(TestInt),
            @TestInt,
            nil) &gt; 0 then begin

  { Start the printout }
    Printer.BeginDoc;

  { Make a string to passthrough }
    s := ' A Test String ';

  { Copy the string to the buffer }
    StrPCopy(Buff.Buffer, s);

  { Set the buffer length }
    Buff.BuffLength := StrLen(Buff.Buffer);

  { Make the escape}
    Escape(Printer.Canvas.Handle,
           PASSTHROUGH,
           0,
           @Buff,
           nil);

  { End the printout }
    Printer.EndDoc;
  end;
end;

end.
</PRE><HR>


<P><H1><A NAME="printer10">Stretched bitmap on TPrinter</P></A></H1>
<P><I>wea@felten.co.at (Alexander Wernhart)</I></P>

<PRE>On Tue, 4 Feb 1997 20:54:43 -0300, Ruy Ponce de Leon Junior
&lt;rplj@di.ufpe.br&gt; wrote:


I'm writing a program that prints a bitmap to the printer
via TPrinter object. The problem occurs when I "stretch"
the bitmap to fit the adequate area on paper. Due to the
stretching (bitblts to Printer's DC), dotted patterns appear
on the <white> bitmap regions, making them almost gray.
This is an obvius undesired effect. Does anybody knows some 
approach to help me?
 
</PRE>

Try this:<p>

<HR><PRE>
procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap:
TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize: Integer;
  BitsSize: Longint;
begin
  GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -