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

📄 printer.htm

📁 对于学习很有帮助
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  Header := MemAlloc(HeaderSize);
  Bits := MemAlloc(BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
        DestRect.Right, DestRect.Bottom,
        0, 0, ABitmap.Width, ABitmap.Height, Bits,TBitmapInfo(Header^),
        DIB_RGB_COLORS, SRCCOPY);
    { you might want to try DIB_PAL_COLORS instead, but this is well
      beyond the scope of my knowledge. }
  finally
    MemFree(Header, HeaderSize);
    MemFree(Bits, BitsSize);
  end;
end;

{ Print a Bitmap using the whole Printerpage }
procedure PrintBitmap(ABitmap: TBitmap);
var
  relheight, relwidth: integer;
begin
  screen.cursor := crHourglass;
  Printer.BeginDoc;
  if ((ABitmap.width / ABitmap.height) > (printer.pagewidth /printer.pageheight)) then
  begin
    { Stretch Bitmap to width of Printerpage }
    relwidth := printer.pagewidth;
    relheight := MulDiv(ABitmap.height, printer.pagewidth,ABitmap.width);
  end else
  begin
    { Stretch Bitmap to height of Printerpage }
    relwidth := MulDiv(ABitmap.width, printer.pageheight, ABitmap.height);
    relheight := printer.pageheight;
  end;
  DrawImage(Printer.Canvas, Rect(0, 0, relWidth, relHeight), ABitmap);
  Printer.EndDoc;
  screen.cursor := crDefault;
end;
</PRE><HR>

<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<P><H1><A NAME="printer11">How to print exact sizes<IMG SRC="../images/new.gif" WIDTH=28 HEIGHT=11 BORDER=0 ALT=" [NEW]"></P></A></H1>
<P><I>From: "Earl F. Glynn" &lt;EarlGlynn@worldnet.att.net&gt;</I></P>

The following sample UNIT shows how to use GetDeviceCaps to obtain
much information about your printer, including the HORZRES and
VERTRES (horizontal and vertical resolution in pixels) and the dimensions
in
inches.  Or, use the LOGPIXELSX and LOGPIXELSY values for the dot 
density/inch in the horizontal and vertical dimensions. <p>

In addition to the info about a printer, the example below shows how to
print a bitmap in its "natural" size, or at a specific location with a
specific
size on the page.  I think this should give you some clues about how
to solve your problem.<p>

The example also shows to plot a sine wave at a particular location with a
given size (all in inches).  Since we in the U.S. are incapable of
converting
inches to metric (I jest), you'll have to figure that part out yourself.<p>
 

<HR><PRE>unit Tstpr2fm;

{Sample usage of Printer object from TPrinter Unit.  Use verbose style
below
 to simplify future reference.

 Shows values returned by GetDeviceCaps Windows API function.

 efg, 19 September 1996}

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Print: TButton;
    Image1: TImage;
    procedure PrintClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
  USES
    Printers;

  {WINAPI GetDeviceCaps Constants from C++ windows.h and wingdi.h}

  {The indivdual constants are defined here for reference only}
  CONST
     DRIVERVERSION   =   0;
     TECHNOLOGY      =   2;  {See windows.h for mask values}
     HORZSIZE        =   4;
     VERTSIZE        =   6;
     HORZRES         =   8;
     VERTRES         =  10;
     BITSPIXEL       =  12;
     PLANES          =  14;
     NUMBRUSHES      =  16;
     NUMPENS         =  18;
     NUMMARKERS      =  20;
     NUMFONTS        =  22;
     NUMCOLORS       =  24;
     PDEVICESIZE     =  26;
     CURVECAPS       =  28;  {See windows.h for mask values}
     LINECAPS        =  30;  {See windows.h for mask values}
     POLYGONALCAPS   =  32;  {See windows.h for mask values}
     TEXTCAPS        =  34;  {See windows.h for mask values}
     CLIPCAPS        =  36;  {See windows.h for mask values}
     RASTERCAPS      =  38;  {See windows.h for mask values}
     ASPECTX         =  40;
     ASPECTY         =  42;
     ASPECTXY        =  44;

     LOGPIXELSX      =  88;
     LOGPIXELSY      =  90;

     SIZEPALETTE     = 104;
     NUMRESERVED     = 106;
     COLORRES        = 108;

     PHYSICALWIDTH   = 110;   {See wingdi.h for definition}
     PHYSICALHEIGHT  = 111;   {See wingdi.h for definition}
     PHYSICALOFFSETX = 112;   {See wingdi.h for definition}
     PHYSICALOFFSETY = 113;   {See wingdi.h for definition}
     SCALINGFACTORX  = 114;   {See wingdi.h for definition}
     SCALINGFACTORY  = 115;   {See wingdi.h for definition}

    DeviceCapsString:  ARRAY[1..34] OF STRING =
     ('DRIVERVERSION',  'TECHNOLOGY',     'HORZSIZE',
      'VERTSIZE',       'HORZRES',        'VERTRES',
      'BITSPIXEL',      'PLANES',         'NUMBRUSHES',
      'NUMPENS',        'NUMMARKERS',     'NUMFONTS',
      'NUMCOLORS',      'PDEVICESIZE',    'CURVECAPS',
      'LINECAPS',       'POLYGONALCAPS',  'TEXTCAPS',
      'CLIPCAPS',       'RASTERCAPS',     'ASPECTX',
      'ASPECTY',        'ASPECTXY',       'LOGPIXELSX',
      'LOGPIXELSY',     'SIZEPALETTE',    'NUMRESERVED',
      'COLORRES',       'PHYSICALWIDTH',  'PHYSICALHEIGHT',
      'PHYSICALOFFSETX','PHYSICALOFFSETY','SCALINGFACTORX',
      'SCALINGFACTORY');

   DeviceCapsIndex:  ARRAY[1..34] OF INTEGER =
     (  0,   2,   4,   6,   8,  10,  12,  14,  16,  18,
       20,  22,  24,  26,  28,  30,  32,  34,  36,  38,
       40,  42,  44,  88,  90, 104, 106, 108, 110, 111,
      112, 113, 114, 115);

  {$R *.DFM}

FUNCTION iPosition(const i:  INTEGER):  INTEGER;
BEGIN
  RESULT := Integer(i * LongInt(Printer.PageWidth)  DIV 1000)
END {iPosition};


FUNCTION jPosition(const j:  INTEGER):  INTEGER;
BEGIN
  RESULT := Integer(j * LongInt(Printer.PageHeight) DIV 1000)
END {jPosition};


procedure TForm1.PrintClick(Sender: TObject);
  VAR
    DestinationRectangle:  TRect;
    GraphicAspectRatio  :  DOUBLE;
    i                   :  INTEGER;
    j                   :  INTEGER;
    iBase               :  INTEGER;
    iPixelsPerInch      :  WORD;
    jBase               :  INTEGER;
    jDelta              :  INTEGER;
    jPixelsPerInch      :  WORD;
    OffScreen           :  TBitMap;
    PixelAspectRatio    :  DOUBLE;
    SourceRectangle     :  TRect;
    TargetRectangle     :  TRect;
    value               :  INTEGER;
    x                   :  DOUBLE;
    y                   :  DOUBLE;
begin
  Printer.Orientation := poLandscape;
  Printer.BeginDoc;

  {Draw a rectangle to show the margins}
  Printer.Canvas.Rectangle(0,0, Printer.PageWidth, Printer.PageHeight);

  {Properties of Printer and Page}
  Printer.Canvas.Font.Name := 'Times New Roman';
  Printer.Canvas.Font.Size := 12;
  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextOut(iPosition(50), jPosition(40), 'Printer/Page Properties');

  Printer.Canvas.Font.Style := [];
  Printer.Canvas.Font.Size := 10;
  iBase := iPosition(50);
  jBase := 60;
  jDelta := 18;
  Printer.Canvas.TextOut(iPosition(50), jPosition(jBase),
    Printer.Printers.Strings[Printer.PrinterIndex]);
  INC (jBase, jDelta);

  Printer.Canvas.TextOut(iBase, jPosition(jBase),
    'Pixels:  ' + IntToStr(Printer.PageWidth) + ' X ' +
                  IntToStr(Printer.PageHeight));
  INC (jBase, jDelta);

  Printer.Canvas.TextOut(iBase, jPosition(jBase),
    'Inches:  ' + FormatFloat('0.000',
                  Printer.PageWidth / Printer.Canvas.Font.PixelsPerInch) + ' X ' +
                  FormatFloat('0.000',
                  Printer.PageHeight / Printer.Canvas.Font.PixelsPerInch)); 
  INC (jBase, 2*jDelta);

  Printer.Canvas.TextOut(iBase, jPosition(jBase),
    'Font:  ' + Printer.Canvas.Font.Name + '   Size:  ' +
    IntToStr(Printer.Canvas.Font.Size));
  INC (jBase, jDelta);


  Printer.Canvas.TextOut(iBase, jPosition(jBase),
    'PixelsPerInch:  ' + IntToStr(Printer.Canvas.Font.PixelsPerInch));
  INC (jBase, jDelta);

  Printer.Canvas.TextOut(iBase, jPosition(jBase),
    '''TEXT'':  ' + IntToStr(Printer.Canvas.TextWidth('TEXT')) + ' X ' +
                    IntToStr(Printer.Canvas.TextHeight('TEXT')) + '
pixels');

  {GetDeviceCaps Values}
  INC (jBase, 2*jDelta);
  Printer.Canvas.Font.Size := 12;
  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextOut(iBase, jPosition(jBase), 'GetDeviceCaps');
  INC (jBase, jDelta);

  Printer.Canvas.Font.Size := 10;
  Printer.Canvas.Font.Style := [];

  FOR j := LOW(DeviceCapsIndex) TO HIGH(DeviceCapsIndex) DO
  BEGIN
    value := GetDeviceCaps(Printer.Handle, DeviceCapsIndex[j]);
    Printer.Canvas.TextOut(iBase, jPosition(jBase), DeviceCapsString[j]);

    IF   (DeviceCapsIndex[j] < 28) OR (DeviceCapsIndex[j] > 38)
    THEN Printer.Canvas.TextOut(iPosition(250), jPosition(jBase), Format('%-8d',  [value]))
    ELSE Printer.Canvas.TextOut(iPosition(250), jPosition(jBase), Format('%.4x', [value]));

    INC (jBase, jDelta);

  END;

  {Put image in lower left corner}
  Printer.Canvas.Draw (iPosition(300), jPosition(100),
                       Form1.Image1.Picture.Graphic);

  {Place same image, 1" wide with appropriate height at location
   4" over and 1" down from top left}
  GraphicAspectRatio := Form1.Image1.Picture.Height /
                        Form1.Image1.Picture.Width;

  iPixelsPerInch := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
  jPixelsPerInch := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
  PixelAspectRatio := jPixelsPerInch / iPixelsPerInch;

  TargetRectangle := Rect(4*iPixelsPerInch,  {4"}
                            jPixelsPerInch,  {1"}
                          6*iPixelsPerInch,  {6" -- 2" wide}
                            jPixelsPerInch +
                          TRUNC(2*iPixelsPerInch * GraphicAspectRatio *
                                PixelAspectRatio));

  Printer.Canvas.TextOut(4*iPixelsPerInch, jPixelsPerInch -
                         Printer.Canvas.TextHeight('X'),
                         '2" wide at (4", 1")');
  Printer.Canvas.StretchDraw (TargetRectangle, Form1.Image1.Picture.Graphic);

  {Write to offscreen bitmap and then copy to Printer Canvas}
  SourceRectangle := Rect (0,0, 3*iPixelsPerInch-1,  2*jPixelsPerInch-1);

  {This should not work!  Rectangle = Left, Top, Right, Bottom
   Top and Bottom are reversed?}
  DestinationRectangle := Rect(4*iPixelsPerInch,    6*jPixelsPerInch,
                               7*iPixelsPerInch-1,  4*jPixelsPerinch-1);

  Printer.Canvas.TextOut(4*iPixelsPerInch, 4*jPixelsPerInch -
                         Printer.Canvas.TextHeight('X'),
                         IntToStr(3*iPixelsPerInch) + ' pixels by ' +
                         IntToStr(2*jPixelsPerInch) + ' pixels -- ' +
                         '3"-by-2" at (4",4")');

  OffScreen := TBitMap.Create;
  TRY
    OffScreen.Width  := SourceRectangle.Right  + 1;
    OffScreen.Height := SourceRectangle.Bottom + 1;
    WITH  OffScreen.Canvas DO
    BEGIN
      Pen.Color := clBlack;
      Brush.Color := clWhite;
      Rectangle(0,0, 3*iPixelsPerInch-1, 2*jPixelsPerInch-1);
      Brush.Color := clRed;
      MoveTo (0,0);
      LineTo (3*iPixelsPerInch-1, 2*jPixelsPerInch-1);

      Brush.Color := clBlue;
      MoveTo (0,0);
      FOR i := 0 TO 3*iPixelsPerInch - 1 DO
      BEGIN
        x := 12*PI*(i / (3*iPixelsPerInch - 1));
        y := jPixelsPerInch + jPixelsPerInch*SIN(x);
        LineTo (i, TRUNC(y));
      END

    END;

    Printer.Canvas.CopyRect(DestinationRectangle, OffScreen.Canvas,
                            SourceRectangle);
  FINALLY
    OffScreen.Free
  END;

  {List the fonts for this printer}
  iBase := iPosition(750);
  Printer.Canvas.Font.Name := 'Times New Roman';
  Printer.Canvas.Font.Size := 12;
  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextOut(iBase, jPosition(40), 'Fonts');

  Printer.Canvas.Font.Style := [];
  Printer.Canvas.Font.Size := 10;
  jDelta := 16;
  FOR j := 0 TO Printer.Fonts.Count - 1 DO
  BEGIN
    Printer.Canvas.TextOut(iBase, jPosition(60 + jDelta*j), Printer.Fonts.Strings[j])
  END;

  Printer.EndDoc;

end;


end.
</PRE><HR>


<HR SIZE="6" color="#00FF00">
<FONT SIZE="2">
<a href="mailto:rdb@ktibv.nl">Please email me</a> and tell me if you liked this page.<BR>
<SCRIPT LANGUAGE="JavaScript">
<!--
	document.write("Last modified " + document.lastModified);
// -->
</SCRIPT><P>
<TABLE BORDER=0 ALIGN="CENTER">
<TR>
	<TD>This page has been created with </TD>
	<TD> <A HREF="http://www.dexnet.com./homesite.html"><IMG SRC="../images/hs25ani.gif" WIDTH=88 HEIGHT=31 BORDER=0 ALT="HomeSite 2.5b">
</A></TD>
</TR>
</TABLE>

</FONT>


</BODY>
</HTML>

⌨️ 快捷键说明

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