게시판에서 컴포넌트 프린트에 관한 글과 소스 보고...
stringGrid까지는 프린트를 했는데요.
DbGrid 를 프린트 할 수 있는 방법은 없나요?
아래 소스를 참조했는데....이런식으로 DBGrid를 프린트 할 수 있는 방법은 없나요?
제발 도와주세요..
function RightPos(iLen: Integer; sTxt: String): Double;
const
iSca: Integer = 5;
iDiv: Integer = 5; // Side
iVal: Integer = 1;
begin
iVal := Printer.Canvas.TextWidth(sTxt);
result := (iLen - (iVal / iSca) - 8) + ((iVal / iSca) / iDiv);
end;
function CenterPos(iJop, iLen: Integer; sTxt: String): Double;
const
iSca: Integer = 5;
iDiv: Integer = 10; // Center
iVal: Integer = 1;
begin
if iJop = 0 then iVal := Printer.Canvas.TextWidth(sTxt);
if iJop = 1 then iVal := Printer.Canvas.TextHeight(sTxt);
result := ((iLen - (iVal / iSca)) / 2) + ((iVal / iSca) / iDiv);
end;
procedure TPrintFM.PrintObject(Sender: TObject);
var
LabelName: TLabel;
HeaderName: THeader;
StringGridName: TStringGrid;
pPnt: TPoint;
PDC: HDC;
dScaleX, dScaleY: Double;
i, j: Integer;
Q, R: TRect;
begin
pPnt.x := 1082; pPnt.y := 760; // Print Area of A4 Size
pPnt.x := Trunc((pPnt.x - 1016) / 2); // Panel Width = 1016
pPnt.y := Trunc((pPnt.y - 740) / 2); // Panel Height = 740
PDC := Printer.Canvas.Handle;
dScaleX := GetDeviceCaps(PDC, LOGPIXELSX) / 96;
dScaleY := GetDeviceCaps(PDC, LOGPIXELSY) / 96;
if Sender is TLabel then begin
LabelName := (Sender as TLabel);
with Printer.Canvas do begin // < Label Print >
Q := LabelName.BoundsRect;
Q.Left := Trunc((Q.Left + pPnt.x) * dScaleX);
Q.Top := Trunc((Q.Top + pPnt.y) * dScaleY);
Font := LabelName.Font;
TextOut(Q.Left, Q.Top, LabelName.Caption);
end;
end;
if Sender is THeader then begin
HeaderName := (Sender as THeader);
with Printer.Canvas do begin // < Header Print >
Q := HeaderName.BoundsRect;
Q.Left := Trunc((Q.Left + pPnt.x) * dScaleX);
Q.Top := Trunc((Q.Top + pPnt.y) * dScaleY);
Q.Right := Trunc((Q.Right + pPnt.x) * dScaleX);
Q.Bottom := Trunc((Q.Bottom + pPnt.y) * dScaleY);
Pen.Width := HeaderName.Height;
Rectangle(Q.Left, Q.Top, Q.Right, Q.Bottom);
Pen.Width := 8;
MoveTo(Q.Left, Q.Bottom); LineTo(Q.Right, Q.Bottom);
end;
end;
if Sender is TStringGrid then begin
StringGridName := (Sender as TStringGrid);
with Printer.Canvas do begin // < StringGrid Print >
Q := StringGridName.BoundsRect;
Q.Left := Trunc((1016 - StringGridName.Width) / 2) + pPnt.x;
Q.Top := Q.Top + pPnt.y;
Font := StringGridName.Font;
Pen.Width := StringGridName.GridLineWidth;
for i := 0 to StringGridName.ColCount - 1 do
for j := 0 to StringGridName.RowCount - 1 do begin
R := StringGridName.CellRect(i, j);
R.Left := Trunc((R.Left + Q.Left) * dScaleX);
R.Top := Trunc((R.Top + Q.Top) * dScaleY);
R.Right := Trunc((R.Right + Q.Left + StringGridName.GridLineWidth) * dScaleX);
R.Bottom := Trunc((R.Bottom + Q.Top + StringGridName.GridLineWidth) * dScaleY);
if (i < StringGridName.FixedCols) or (j < StringGridName.FixedRows) then Brush.Color := StringGridName.FixedColor
else begin
Brush.Style := bsClear;
SetBKColor(PDC, ColorToRGB(clWhite));
end;
Rectangle(R.Left, R.Top, R.Right, R.Bottom);
R.Left := R.Left + Trunc(8 * dScaleX);
// R.Left := R.Left + Trunc(RightPos(StringGridName.ColWidths[i], StringGridName.Cells[i, j]) * dScaleX);
R.Top := R.Top + Trunc(CenterPos(1, StringGridName.DefaultRowHeight, StringGridName.Cells[i, j]) * dScaleY);
TextOut(R.Left, R.Top, StringGridName.Cells[i, j]);
end;
Pen.Width := 8;
Rectangle(Trunc(Q.Left*dScaleX), Trunc(Q.Top*dScaleY), R.Right, R.Bottom);
end;
end;
end;
> 게시판에서 컴포넌트 프린트에 관한 글과 소스 보고...
> stringGrid까지는 프린트를 했는데요.
> DbGrid 를 프린트 할 수 있는 방법은 없나요?
> 아래 소스를 참조했는데....이런식으로 DBGrid를 프린트 할 수 있는 방법은 없나요?
> 제발 도와주세요..
>
> function RightPos(iLen: Integer; sTxt: String): Double;
> const
> iSca: Integer = 5;
> iDiv: Integer = 5; // Side
> iVal: Integer = 1;
> begin
> iVal := Printer.Canvas.TextWidth(sTxt);
> result := (iLen - (iVal / iSca) - 8) + ((iVal / iSca) / iDiv);
> end;
>
> function CenterPos(iJop, iLen: Integer; sTxt: String): Double;
> const
> iSca: Integer = 5;
> iDiv: Integer = 10; // Center
> iVal: Integer = 1;
> begin
> if iJop = 0 then iVal := Printer.Canvas.TextWidth(sTxt);
> if iJop = 1 then iVal := Printer.Canvas.TextHeight(sTxt);
> result := ((iLen - (iVal / iSca)) / 2) + ((iVal / iSca) / iDiv);
> end;
>
> procedure TPrintFM.PrintObject(Sender: TObject);
> var
> LabelName: TLabel;
> HeaderName: THeader;
> StringGridName: TStringGrid;
> pPnt: TPoint;
> PDC: HDC;
> dScaleX, dScaleY: Double;
> i, j: Integer;
> Q, R: TRect;
> begin
> pPnt.x := 1082; pPnt.y := 760; // Print Area of A4 Size
> pPnt.x := Trunc((pPnt.x - 1016) / 2); // Panel Width = 1016
> pPnt.y := Trunc((pPnt.y - 740) / 2); // Panel Height = 740
>
> PDC := Printer.Canvas.Handle;
> dScaleX := GetDeviceCaps(PDC, LOGPIXELSX) / 96;
> dScaleY := GetDeviceCaps(PDC, LOGPIXELSY) / 96;
>
> if Sender is TLabel then begin
> LabelName := (Sender as TLabel);
> with Printer.Canvas do begin // < Label Print >
> Q := LabelName.BoundsRect;
> Q.Left := Trunc((Q.Left + pPnt.x) * dScaleX);
> Q.Top := Trunc((Q.Top + pPnt.y) * dScaleY);
> Font := LabelName.Font;
> TextOut(Q.Left, Q.Top, LabelName.Caption);
> end;
> end;
>
> if Sender is THeader then begin
> HeaderName := (Sender as THeader);
> with Printer.Canvas do begin // < Header Print >
> Q := HeaderName.BoundsRect;
> Q.Left := Trunc((Q.Left + pPnt.x) * dScaleX);
> Q.Top := Trunc((Q.Top + pPnt.y) * dScaleY);
> Q.Right := Trunc((Q.Right + pPnt.x) * dScaleX);
> Q.Bottom := Trunc((Q.Bottom + pPnt.y) * dScaleY);
> Pen.Width := HeaderName.Height;
> Rectangle(Q.Left, Q.Top, Q.Right, Q.Bottom);
> Pen.Width := 8;
> MoveTo(Q.Left, Q.Bottom); LineTo(Q.Right, Q.Bottom);
> end;
> end;
>
> if Sender is TStringGrid then begin
> StringGridName := (Sender as TStringGrid);
> with Printer.Canvas do begin // < StringGrid Print >
> Q := StringGridName.BoundsRect;
> Q.Left := Trunc((1016 - StringGridName.Width) / 2) + pPnt.x;
> Q.Top := Q.Top + pPnt.y;
> Font := StringGridName.Font;
> Pen.Width := StringGridName.GridLineWidth;
> for i := 0 to StringGridName.ColCount - 1 do
> for j := 0 to StringGridName.RowCount - 1 do begin
> R := StringGridName.CellRect(i, j);
> R.Left := Trunc((R.Left + Q.Left) * dScaleX);
> R.Top := Trunc((R.Top + Q.Top) * dScaleY);
> R.Right := Trunc((R.Right + Q.Left + StringGridName.GridLineWidth) * dScaleX);
> R.Bottom := Trunc((R.Bottom + Q.Top + StringGridName.GridLineWidth) * dScaleY);
> if (i < StringGridName.FixedCols) or (j < StringGridName.FixedRows) then Brush.Color := StringGridName.FixedColor
> else begin
> Brush.Style := bsClear;
> SetBKColor(PDC, ColorToRGB(clWhite));
> end;
> Rectangle(R.Left, R.Top, R.Right, R.Bottom);
> R.Left := R.Left + Trunc(8 * dScaleX);
> // R.Left := R.Left + Trunc(RightPos(StringGridName.ColWidths[i], StringGridName.Cells[i, j]) * dScaleX);
> R.Top := R.Top + Trunc(CenterPos(1, StringGridName.DefaultRowHeight, StringGridName.Cells[i, j]) * dScaleY);
> TextOut(R.Left, R.Top, StringGridName.Cells[i, j]);
> end;
> Pen.Width := 8;
> Rectangle(Trunc(Q.Left*dScaleX), Trunc(Q.Top*dScaleY), R.Right, R.Bottom);
> end;
> end;
> end;
>
저런... 엄청난 코딩이군요..
DBGrid 출력은 훨씬 쉬운데..
Component중 QReport라는 탭의 QuickRep Component를 이용하시면 DBGrid출력은 코딩이 거의 필요없을 정도로 간단해집니다.
우선form을 하나 만드시고, 폼에 QuickRep Component를 놓으신 후
implementation밑에 uses DBGrid가 있는 폼의 Unit명;
코딩추가하시고 저장.
QuickRep Component의 DataSet Property에 DBGrid에 걸려있는 Query를 선택해주십시오.
QuickRep Component위에 다시 QReport 탭의 QRBand를 갖다 놓으시고 속성중 BandType을 rbDetail로 하십시오.
이 Band에 QRDBText component 갖다 놓으시고 속성중 DataSource, DataField를 선택해주시면됩니다.
DBGrid가 있는 화면에서 ButtonClick이벤트에 출력폼.QuickRep 콤포넌트명.Print;
하시면 바로 프린터로 출력됩니다.
짧게 쓸려고 하다보니..^^;;;;;;
설명이 부족하시면 메일 주세용