精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>报表和打印>>打印>>Re: 如何打印form??

主题:Re: 如何打印form??
发信人: teleme@GZ()
整理人: teleme(2001-03-25 20:35:44), 站内信件

标  题: Re: 如何打印form??
发信站: 网易虚拟社区 (Tue Nov 28 13:34:44 2000), 站内信件

Q:  How do I print a form?

A:  Prints all visible TLabel, TEdit, TMemo, TDBText, TDBEdit and TDBM
emo components on the form with proper place, size and font. Set the F
orm Scrollbar.Range to 768 Horz and 1008 Vert for a 8 X 10.5 page at 9
6formPPI.

USES Printers;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  C : array[0..255] of char;
  CLen, ScaleX, ScaleY, I : integer;
  Format : Word;  DC : HDC;
  MComp : TMemo;  R: TRect;
begin

  Printer.BeginDoc;
  DC := Printer.Canvas.Handle;
  ScaleX := GetDeviceCaps(DC, LOGPIXELSX) div PixelsPerInch;
  ScaleY := GetDeviceCaps(DC, LOGPIXELSY) div PixelsPerInch;
  for I := 0 to ComponentCount-1 do
    if (Components[I] is TCustomLabel) or (Components[I] is TCustomEdi
t) then
    begin
      MComp := TMemo(Components[I]);
      if (MComp.visible) then
      begin
        Printer.Canvas.Font := MComp.Font;
        DC := Printer.Canvas.Handle; {so DrawText knows about font}

        R := MComp.BoundsRect;
        R.Top := (R.Top + VertScrollBar.Position) * ScaleY;
        R.Left := (R.Left + HorzScrollBar.Position) * ScaleX;
        R.Bottom := (R.Bottom + VertScrollBar.Position) * ScaleY;
        R.Right := (R.Right + HorzScrollBar.Position) * ScaleY;
        if (not(Components[I] is TCustomLabel)) and (MComp.BorderStyle
 = bsSingle)
          then Printer.Canvas.Rectangle(R.Left,R.Top,R.Right,R.Bottom)
;
        Format := DT_LEFT;
        if (Components[I] is TEdit) or (Components[I] is TCustomMaskEd
it) then

          Format := Format or DT_SINGLELINE or DT_VCENTER
        else
        begin
          if MComp.WordWrap then Format := DT_WORDBREAK;
          if MComp.Alignment = taCenter then Format := Format or DT_CE
NTER;
          if MComp.Alignment = taRightJustify then Format := Format or
 DT_RIGHT;
          R.Bottom := R.Bottom + Printer.Canvas.Font.Height + 1;
        end;
        CLen := MComp.GetTextBuf(C,255);
        R.Left := R.Left + ScaleX + ScaleX;
        DrawText(DC, C, CLen, R, Format);

      end;
    end;
  Printer.EndDoc;
end;


--
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.106.104.36]

[关闭][返回]