델파이 인쇄 출력
Printers.pas의 사용법
TPrinter
// 생성, 소멸
constructor Create;
destructor Destroy; override;
// 인쇄 제어
procedure Abort; procedure BeginDoc;
procedure EndDoc;
procedure NewPage;
property Canvas: TCanvas ; //ReadOnly
property Copies: Integer;
property Orientation: TPrinterOrientation;
property Title: string ;
// 프린터 제어
procedure GetPrinter(ADevice, ADriver, APort: PChar; var ADeviceMode: THandle);
procedure SetPrinter(ADevice, ADriver, APort: PChar; ADeviceMode: THandle);
property Handle: HDC; //ReadOnly
property PrinterIndex: Integer ;
property Printers: TStrings; //ReadOnly
// 상태 정보
property Aborted: Boolean; //ReadOnly
property Capabilities: TPrinterCapabilities; //ReadOnly
property Fonts: TStrings; //ReadOnly
property PageHeight: Integer; //ReadOnly
property PageWidth: Integer; //ReadOnly
property PageNumber: Integer ; //ReadOnly
property Printing: Boolean; //ReadOnly
나. 전역Procedure, Function
function Printer: TPrinter;
function SetPrinter(NewPrinter: TPrinter): TPrinter;
procedure AssignPrn(var F: Text);
TCanvas의 사용법
// 생성, 소멸
constructor Create;
destructor Destroy; override;
// 직선그리기
procedure LineTo(X, Y: Integer);
procedure MoveTo(X, Y: Integer);
procedure Polyline(const Points: array of TPoint);
procedure PolyBezier(const Points: array of TPoint);
procedure PolyBezierTo(const Points: array of TPoint);
// 사각형 그리기
procedure FillRect(const Rect: TRect);
procedure DrawFocusRect(const Rect: TRect);
procedure FrameRect(const Rect: TRect);
procedure Rectangle(X1, Y1, X2, Y2: Integer);
procedure RoundRect(X1, Y1, X2, Y2, X3, Y3: Integer);
// 원 그리기
procedure Arc(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
procedure Ellipse(X1, Y1, X2, Y2: Integer);
procedure Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
procedure Chord(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
// 다각형 그리기
procedure Polygon(const Points: array of TPoint);
// 채우기
procedure FloodFill(X, Y: Integer; Color: TColor; FillStyle: TFillStyle);
// 비트맵 처리
procedure BrushCopy(const Dest: TRect; Bitmap: TBitmap; const Source: TRect; Color:TColor);
procedure CopyRect(const Dest: TRect; Canvas: TCanvas; const Source: TRect);
procedure Draw(X, Y: Integer; Graphic: TGraphic);
procedure StretchDraw(const Rect: TRect; Graphic: TGraphic);
// 문자열 관련
function TextExtent(const Text: string): TSize;
function TextHeight(const Text: string): Integer;
procedure TextOut(X, Y: Integer; const Text: string);
procedure TextRect(Rect: TRect; X, Y: Integer; const Text: string);
function TextWidth(const Text: string): Integer;
// 점찍기
property Pixels[X, Y: Integer]: TColor;
// 상태 제어
procedure Lock; procedure Unlock;
procedure Refresh; function TryLock: Boolean;
// 이벤트
property OnChange: TNotifyEvent;
property OnChanging: TNotifyEvent;
// 프로퍼티
property ClipRect: TRect; //ReadOnly
property Handle: HDC;
property LockCount: Integer ; //ReadOnly
property CanvasOrientation: TCanvasOrientation ; //ReadOnly
property PenPos: TPoint ;
property TextFlags: Longint;
property Brush: TBrush;
property CopyMode: TCopyMode;
property Font: TFont;
property Pen: TPen;
매핑모드
: 각종 그리기 함수에서 사용하는 좌표의 단위를 나타내는 것을 말한다.
종류와 규격
이름 단위 X증가 방향 Y증가 방향
MM_TEXT Pixel 오른쪽 아래
MM_LOMETRIC 0.1 mm 오른쪽 위
MM_HIMETRIC 0.01 mm 오른쪽 위
MM_LOENGLISH 0.01 inch 오른쪽 위
MM_HIENGLISH 0.001 inch 오른쪽 위
MM_TWIPS 1/20 Point, 1/1440 inch 오른쪽 위
MM_ISOTROPIC 사용자정의 X,Y동일 사용자정의 사용자정의
MM_ANISOTROPIC 사용자정의 사용자정의 사용자정의
사용법
SetMapMode( Printer.Handle, MM_TEXT );
GetMapMode(Printer.Handle );
좌표 설정
- 절대 좌표
용지의 크기와 관계없이 내용의 크기를 일정하게 한다.
- 상대 좌표
용지의 크기에 맞게 내용의 크기도 변한다.
문자 출력
- TextMetric의 구조
tagTEXTMETRIC = record tmHeight: Longint; //전체 높이
tmAscent: Longint; //베이스라인의 윗부분
tmDescent: Longint; //베이스라인의 아랫부분
tmInternalLeading: Longint; //경계 내부의 리딩
tmExternalLeading: Longint; //행간의 엑스트라 리딩
tmAveCharWidth: Longint; //문자의 평균 폭
tmMaxCharWidth: Longint; //가장 큰문자 가로 폭
tmWeight: Longint; //굵기
tmOverhang: Longint; //문자열간의 추가 폭
tmDigitizedAspectX: Longint; //수평 기준
tmDigitizedAspectY: Longint; //수직 기준
tmFirstChar: AnsiChar; //첫번째 문자
tmLastChar: AnsiChar; //마지막 문자
tmDefaultChar: AnsiChar; //글꼴에 없는 문자를 대체할 문자(네모)
tmBreakChar: AnsiChar; //단어 잘림을 정의하는데 사용되는 문자
tmItalic: Byte; //기울임꼴 <> 0
tmUnderlined: Byte; //밑줄 <> 0
tmStruckOut: Byte; //취소선 <> 0
tmPitchAndFamily: Byte; //글꼴의 피치,패미리 정보
tmCharSet: Byte; //문자세트
end;
프린터 제어
// 가로 해상도
function GetXDPI:Integer;
begin
Result:= GetDeviceCaps( Printer.Handle, LOGPIXELSX );
end;
// 세로 해상도
function GetYDPI:Integer;
begin
Result:= GetDeviceCaps( Printer.Handle, LOGPIXELSY );
end;
// 프린터가 가지고 있는 용지 여백
procedure GetOutSide( var ALeft, ATop, ARight, ABottom: Integer );
var
Pt: TPoint;
begin
Escape( Printer.Canvas.Handle, GETPRINTINGOFFSET, 0, nil, @Pt );
ALeft := Pt.X; ATop := Pt.Y;
Escape ( Printer.Canvas.Handle, GETPHYSPAGESIZE, 0, nil, @Pt );
ARight := Pt.X - Printer.PageWidth - ALeft;
ABottom:= Pt.Y - Printer.PageHeight - ATop ;
end;
// 0점 조절 { 0점 : 그리기 함수에서 X,Y축이 0이 되는 지점을 말한다 }
function SetOrgPoint( APoint: TPoint ): TSize;
var
Org: TPoint;
begin
Escape( Printer.Canvas.Handle, GETPRINTINGOFFSET, 0, nil, @Org );
SetWindowOrgEx( Printer.Handle,Org.X-APoint.X,APoint.Y-Org.Y,@Result );
end;
// 매핑모드
procedure TMain_Form.Button10Click(Sender: TObject);
var
OldMap: Integer; OldOrg: TSize;
begin
Printer.BeginDoc;
OldMap := SetMapMode( Printer.Handle, MM_LOMETRIC );
OldOrg := SetOrgPoint( Point( 300, 1500 ) );
try
Printer.Canvas.Rectangle( 0, 0, 1000, -200 );
finally
SetOrgPoint( Point( OldOrg.cx, OldOrg.cy ) );
SetMapMode( Printer.Handle, OldMap ); Printer.EndDoc;
end;
end;
그리기
// 예제 1
procedure DrawLine( Canvas: TCanvas );
begin
with Canvas do
begin
//SetMapMode( MM_TEXT );
Pen.Width := 1;
MoveTo( 100, 100 );
LineTo( 2000, 100 );
Pen.Width := 3;
MoveTo( 100, 200 );
LineTo( 2000, 200 );
Pen.Width := 5;
MoveTo( 100, 400 );
LineTo( 2000, 400 );
Pen.Width := 10;
MoveTo( 100, 600 );
LineTo( 2000, 600 );
Pen.Width := 20;
MoveTo( 100, 800 );
LineTo( 2000, 800 );
Pen.Color := clRed;
Brush.Color := clYellow;
Ellipse( 100, 1000, 2000, 1200 );
Pen.Color := clGreen;
Brush.Color := clBlue;
Rectangle( 100, 1400,11 2000, 1600 );
Pen.Color := clMaroon;
Brush.Color := clAqua;
Rectangle( 100, 1800, 2000, 2000 );
Brush.Color := clNavy;
Brush.Style:= bsDiagCross;
Rectangle( 100, 2200,2000, 2500 );
end;
end;
procedure TMain_Form.Button1Click(Sender: TObject);
begin
Printer.BeginDoc;
DrawLine( Printer.Canvas );
Printer.EndDoc;
end;
매핑모드를 mm, inch 단위나 사용자 정의형으로 바꿔서 그리기
// 예제 2, 3, 4, 5
procedure DrawLine( Canvas: TCanvas );
begin
with Canvas do
begin
SetMapMode( MM_LOMETRIC ); // mm 단위
Pen.Width := 1;
MoveTo( 100, -100 );
LineTo( 2000, -100 );
Pen.Width := 3;
MoveTo( 100, -200 );
LineTo( 2000, -200 );
Pen.Width := 5;
MoveTo( 100, -400 );
LineTo( 2000, -400 );
Pen.Width := 10;
MoveTo( 100, -600 );
LineTo( 2000, -600 );
Pen.Width := 20;
MoveTo( 100, -800 );
LineTo( 2000, -800 );
Pen.Color := clRed;
Brush.Color := clYellow;
Ellipse( 100, -1000, 2000, -1200 );
Pen.Color := clGreen;
Brush.Color := clBlue;
Rectangle( 100, -1400, 2000, -1600 );
Pen.Color := clMaroon;
Brush.Color := clAqua;
Rectangle( 100, -1800, 2000, -2000 );
Brush.Color := clNavy;
Brush.Style := bsDiagCross;
Rectangle( 100, -2200, 2000, -2500 );
end;
end;
procedure TMain_Form.Button1Click(Sender: TObject);
begin
Printer.BeginDoc;
DrawLine( Printer.Canvas );
Printer.EndDoc;
end;
문자출력
// 예제 6 폰트의 Baseline, Ascent, Descent영역 알기
procedure TMain_Form.Button2Click(Sender: TObject);
var
Metrics : TTextMetric;
begin
Printer.BeginDoc;
with Printer.Canvas do
begin
Brush.Style := bsClear;
SetMapMode( Handle, MM_LOMETRIC );
Font.Name := '굴림체';
Font.Height := 300;
GetTextMetrics( Handle, Metrics );
TextOut( 50, -500 + Metrics.tmAscent, 'Delphi델파이' );
Pen.Width := 2; MoveTo( 0, -500 );
LineTo( 2000, -500 );
Pen.Width := 1;
MoveTo( 0, -500 + Metrics.tmAscent );
LineTo( 2000, -500 + Metrics.tmAscent );
MoveTo( 0, -500 - Metrics.tmDescent );
LineTo( 2000, -500 - Metrics.tmDescent );
end;
Printer.EndDoc;
end;
// 예제 7-1 베이스라인에 맞추어 줄단위로 출력하기
procedure TextOutBase( X, Y: Integer; Text: String );
var
Metrics : TTextMetric;
begin
with Printer.Canvas do
begin
GetTextMetrics( Handle, Metrics );
TextOut( X, Y + Metrics.tmAscent, Text );
end;
end;
procedure TMain_Form.Button3Click(Sender: TObject);
const
Text1 = 'Delphi';
Text2 = 'print';
Text3 = 'Superpage';
Text4 = 'Yang Byoung Kyu';
Text5 = '델마당';
var
Len : Integer;
begin
Printer.BeginDoc;
with Printer.Canvas do
begin
SetMapMode( Handle, MM_LOMETRIC );
Font.Name := 'Times New Roman';
Brush.Style := bsClear;
Len := 100;
Font.Height := 200;
TextOutBase( Len, -300, Text1 );
Inc( Len, TextWidth( Text1 + ' ' ) );
Font.Height := 120;
TextOutBase( Len, -300, Text2 );
Inc( Len, TextWidth( Text2 + ' ' ) );
Font.Height := 60;
TextOutBase( Len, -300, Text3 );
Inc( Len, TextWidth( Text3 + ' ' ) );
Font.Height := 50;
TextOutBase( Len, -300, Text4 );
Inc( Len, TextWidth( Text4 + ' ' ) );
Font.Name := '굴림';
Font.Height := 100;
TextOutBase( Len, -300, Text5 );
MoveTo( 0, -300 );
LineTo( 2000, -300 );
end;
Printer.EndDoc;
end;
//예제 7-2 박스안에 출력하기
procedure TMain_Form.Button4Click(Sender: TObject);
const
DrawFormat : array[0..8] of UINT = (
DT_LEFT or DT_TOP, DT_CENTER or DT_TOP,
DT_RIGHT or DT_TOP, DT_LEFT or DT_VCENTER,
DT_CENTER or DT_VCENTER, DT_RIGHT or DT_VCENTER,
DT_LEFT or DT_BOTTOM, DT_CENTER or DT_BOTTOM,
DT_RIGHT or DT_BOTTOM );
Texts: array[0..8] of String = ( '왼쪽 위', '중간 위', '오른쪽 위',
'왼쪽 중간','중간', '오른쪽 중간',
'왼쪽 아래','중간 아래','오른쪽 아래');
var
ARect: TRect; i: Integer;
begin
ARect := Rect( 0, -600, 1000, -1600 );
Printer.BeginDoc;
with Printer.Canvas do
begin
SetMapMode( Handle, MM_LOMETRIC );
Font.Name := '굴림체';
Font.Height := 50;
Brush.Style := bsClear;
for i:= 0 to 8 do
DrawText( Handle, PChar( Texts[i] ), Length( Texts[i] ), ARect, DT_SINGLELINE or DrawFormat[i] );
with ARect do
Rectangle( Left, Top, Right, Bottom );
end;
Printer.EndDoc;
end;
//예제 7-3 DrawText를 이용해서 좌,우, 중앙 정렬하기
procedure TMain_Form.Button5Click(Sender: TObject);
const
DrawFormat: array[0..2] of UINT = ( DT_LEFT, DT_CENTER, DT_RIGHT );
Texts: array[0..4] of String = ( 'Delphi', 'print', 'Superpage', 'YYY MMM DDD', '델파이' );
var
ARect: TRect; i, j: Integer;
begin
ARect := Rect( 200, 0, 1800, -2000 );
Printer.BeginDoc;
with Printer.Canvas do
begin
SetMapMode( Handle, MM_LOMETRIC );
Font.Name := '굴림체';
Font.Height := 50;
Brush.Style := bsClear;
for i:= 0 to 4 do
begin
for j:= 0 to 2 do
DrawText( Handle, PChar( Texts[i] ), Length( Texts[i] ), ARect, DT_SINGLELINE or DT_BOTTOM or DrawFormat[j] );
Dec( ARect.Bottom, 100 );
end;
end;
Printer.EndDoc;
end;
// 예제 8 장평, 각도 조절하기
procedure TMain_Form.Button6Click(Sender: TObject);
var
LogFont: TLogFont;
OldFont, NewFont: HFONT;
begin
Printer.BeginDoc;
with Printer.Canvas do
begin
SetMapMode( Handle, MM_LOMETRIC );
Font.Name := '굴림체';
Brush.Style := bsClear;
if GetObject(Font.Handle, SizeOf(LogFont), @LogFont) <> 0 then
begin
with LogFont do
begin
lfHeight := 100;
lfWidth := 70;
lfEscapement := -300;
end;
NewFont:= CreateFontIndirect( LogFont );
OldFont := SelectObject( Handle, NewFont );
TextOut( 1200, -200, '글씨를 쓰자' );
NewFont := SelectObject(Canvas.Handle,OldFont);
DeleteObject(NewFont);
end;
end;
Printer.EndDoc;
end;
음영처리
//RGB를 이용한 음영
procedure TMain_Form.Button7Click(Sender: TObject);
var
i: Integer;
begin
Printer.BeginDoc;
SetMapMode( Printer.Handle, MM_LOMETRIC );
with Printer.Canvas do
begin
for i:= 0 to 255 do
begin
Pen.Color:= RGB( i, i, i );
Brush.Color:= Pen.Color;
Brush.Style:= bsSolid;
Rectangle( 0, -( i * 10 ), 500, -( ( i+1 ) * 10 ) );
Brush.Style:= bsClear;
if i mod 10 = 0 then
TextOut( 550, -( i * 10 ), Format( 'RGB( %d, %d, %d );', [i,i,i] ) );
end;
end;
Printer.EndDoc;
end;
//비트맵 브러시를 이용한 음영
procedure TMain_Form.Button9Click(Sender: TObject);
var
i: Integer;
begin
Printer.BeginDoc;
SetMapMode( Printer.Handle, MM_LOMETRIC );
with Printer.Canvas do
begin
Brush.Bitmap := Image_Brush1.Picture.Bitmap;
Rectangle( 1700, 0, 2000, -400 );
Brush.Bitmap := Image_Brush2.Picture.Bitmap;
Rectangle( 1700, -400, 2000, -800 );
Brush.Bitmap := Image_Brush3.Picture.Bitmap;
Rectangle( 1700, -800, 2000, -1200 );
Brush.Bitmap := Image_Brush4.Picture.Bitmap;
Rectangle( 1700, -1200, 2000, -1600 );
end;
end;
'Delphi Tip > 출력' 카테고리의 다른 글
바코드프린터 출력 참고 (0) | 2024.06.02 |
---|---|
프린터 목록 불러오기 / 기본프린터 불러오기 (0) | 2023.11.24 |
델파이 인쇄 출력 10편 (0) | 2023.09.07 |
델파이 인쇄 출력 9편 (0) | 2023.09.06 |
델파이 인쇄 출력 8편 (0) | 2023.09.05 |
댓글