你的意思是显示。输入,打印, 三合一?
这需要写个控件,
其中用到三个方法
DrawTo(ACanvas: TCanvas, ForPrint: boolean);
paint;
print;
在paint和print都调用DrawTo;
还是挺烦的。
另外,打印也需要用两个方法, 一个套打,一个全打,
我做个类类似的控件,可见即所得,还可以放大/缩小显示; 代码比较多
我这里给出一个基类,你自己继承一个就可以了; 改写override DrawTo就可以啦,当然还要加上两个不同的打印方法;
TZoomableCtrl = class(TCustomcontrol)
private
FReadOnly: boolean;
Fzoom: integer;
FOrientation: TprinterOrientation;
FMarginPc: TMargins;
FdesignShowFrame: boolean;
procedure OrientationChange;
procedure SetZoom(const Value: integer);
procedure SetOrientation(const Value: TprinterOrientation);
procedure WmSetFocus(var msg: TMessage);
message WM_SETFOCUS;
procedure WmKillFocus(var msg: TMessage);
message WM_KILLFOCUS;
procedure SetMarginPc(const Value: TMargins);
procedure SetdesignShowFrame(const Value: boolean);
procedure WmErasebkgnd(var msg: TMessage);
message WM_ERASEBKGND;
procedure WMGetDlgCode(var Msg: TWMGetDlgCode);
message WM_GETDLGCODE;
protected
FClipRect: TRect;
FCanvas: TCanvas;
procedure DrawTo(ACanvas: TCanvas;
ARect: TRect;
ForPrint: boolean);
virtual;
procedure DrawLogo(ACanvas: TCanvas;
R: TRect;
Logo: TBitMap);
procedure makeSureRectInSignt(R: TRect);
function pcToRealSize(pcValue: integer): integer;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure paint;
override;
procedure Print;
virtual;
procedure saveAsJpeg(FileName: string;
showDlg: boolean);
function PaintRect: TRect;
function HeadRect: TRect;
function FootRect: TRect;
published
property ZoomPercent: integer Read Fzoom write SetZoom;
property ReadOnly: boolean read FReadOnly write FReadOnly;
property Orientation: TprinterOrientation read FOrientation write SetOrientation;
property MarginPc: TMargins read FMarginPc write SetMarginPc;
property designShowFrame: boolean read FdesignShowFrame write SetdesignShowFrame;
property Font;
property Align;
property popupMenu;
property Enabled;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnDblClick;
property showhint;
end;
{ TZoomableCtrl }
constructor TZoomableCtrl.Create(AOwner: TComponent);
begin
inherited create(AOwner);
FMarginPc:=TMargins.Create(self);
do
ubleBuffered:=true;
height:=2970 div 5;
Width:=2100 div 5;
Fzoom:=60;
FOrientation:=poPortrait;
end;
destructor TZoomableCtrl.Destroy;
begin
FMarginPc.Free;
inherited;
end;
procedure TZoomableCtrl.DrawLogo(ACanvas: TCanvas;
R: TRect;
Logo: TBitMap);
var
RLogo: TRect;
tmpR: TRect;
s: string;
LogoH: integer;
begin
S:='¶«Ý¸ºñ½Ö°®¸ßËܽº³§';
logoH:=pcToRealSize(30);
ACanvas.Font.Height:=pcToRealSize(25);
Rlogo:=bounds(R.Left,R.Top,logoH, LogoH);
offsetRect(RLogo, (R.Right-R.Left-ACanvas.textWidth(s)-Rlogo.Right+RLogo.Left) div 2,0);
tmpR:=Rect(RLogo.Right,RLogo.Top,R.Right,Rlogo.Bottom-(R.Bottom-R.Top) div 300);
with ACanvas.Brush, ACanvasdo
begin
StretchDraw(RLogo,logo);
drawText(handle,pchar(S),length(s),tmpR, DT_SINGLELINE or DT_LEFT or DT_BOTTOM);
end;
end;
procedure TZoomableCtrl.DrawTo(ACanvas: TCanvas;
ARect: TRect;
ForPrint: boolean);
begin
FCanvas:=ACanvas;
ACanvas.Font:=Font;
ACanvas.Brush.Color:=clWhite;
ACanvas.FillRect(ARect);
end;
function TZoomableCtrl.FootRect: TRect;
begin
Result:=PaintRect;
Result.Top:=Result.Bottom - pcToRealSize(FMarginPc.FFootFeightpc);
end;
function TZoomableCtrl.HeadRect: TRect;
begin
Result:=PaintRect;
Result.Bottom:=Result.Top +pcToRealSize(FMarginPc.FHeadHeightPc);
end;
procedure TZoomableCtrl.makeSureRectInSignt(R: TRect);
var
LT, RB: Tpoint;
begin
LT:=ClientToParent(R.TopLeft);
RB:=ClientToParent(R.BottomRight);
if LT.X<8 then
Left:=Left-LT.X+8;
if LT.Y<8 then
Top:=Top-LT.Y+8;
if RB.X>parent.ClientWidth-8 then
Left:=Left-(RB.X-parent.ClientWidth)-8;
if RB.Y>parent.ClientHeight-8 then
Top:=Top-(RB.Y-parent.ClientHeight)-8;
end;
procedure TZoomableCtrl.OrientationChange;
var
w, h, L, T : integer;
begin
L:=0;
T:=0;
W:=1073*FZoom div 100;
h:=780*FZoom div 100;
if FOrientation=poLandscape then
begin
if w<parent.ClientWidth then
L:=(parent.ClientWidth-w) div 2;
if h<parent.ClientHeight then
T:=(parent.ClientHeight-h) div 2;
SetBounds(L, T, w, h);
end else
begin
if w<parent.ClientHeight then
L:=(parent.ClientHeight-w) div 2;
if h<parent.ClientWidth then
T:=(parent.ClientWidth-h) div 2;
SetBounds(T, L, h, w);
end;
end;
procedure TZoomableCtrl.paint;
begin
FClipRect:=ClientRect;
DrawTo(Canvas, clientRect, false);
if (csDesigning in ComponentState) and FdesignShowFrame then
with canvasdo
begin
pen.Color:=clRed;
pen.Style:=psdot;
Brush.Style:=bsClear;
Rectangle(PaintRect);
Rectangle(HeadRect);
Rectangle(FootRect)
end;
end;
function TZoomableCtrl.PaintRect: TRect;
begin
Result:=FClipRect;
inc(Result.Left, pcToRealSize(FMarginPc.mgLeftPc));
dec(Result.Right, pcToRealSize(FMarginPc.mgRightPc));
inc(Result.Top, pcToRealSize(FMarginPc.mgTopPc));
Dec(Result.Bottom, pcToRealSize(FMarginPc.mgBottomPc));
end;
function TZoomableCtrl.pcToRealSize(pcValue: integer): integer;
var
maxValue: integer;
begin
maxValue:=max(FClipRect.Right-FClipRect.Left, FClipRect.Bottom-FClipRect.Top);
result:=maxValue*pcValue div 1000;
end;
procedure TZoomableCtrl.Print;
var
poOld: TprinterOrientation;
begin
with printerdo
try
poOld:=printer.Orientation;
printer.Orientation:=FOrientation;
begin
Doc;
FClipRect:=Canvas.ClipRect;
DrawTo(Canvas, Canvas.ClipRect, true);
enddoc;
finally
printer.Orientation:=poOld;
end;
end;
procedure TZoomableCtrl.saveAsJpeg(FileName: string;
showDlg: boolean);
var
jpg: TJpegImage;
bmp: TBitMap;
dlg: TSaveDialog;
Fname: string;
const
aaa = '&Icirc;&Auml;&frac14;&thorn;&Atilde;&ucirc;&sup3;&AElig;';
begin
if showDlg then
begin
dlg:=TSaveDialog.Create(self);
try
dlg.DefaultExt:='jpg';
dlg.Filter:='JPEG&Iacute;&frac14;&AElig;&not;(*.jpg)|*.jpg';
dlg.FileName:=fileName;
if dlg.Execute then
Fname:=dlg.FileName else
exit;
finally
dlg.Free;
end;
end
else
fname:=fileName;
if fName='' then
exit;
jpg:=TJpegImage.Create;
bmp:=TBitMap.Create;
try
bmp.Width:=Width;
bmp.Height:=Height;
bmp.PixelFormat:=pf24bit;
PaintTo(bmp.Canvas,0,0);
jpg.Assign(bmp);
jpg.SaveToFile(Fname);
finally
jpg.Free;
bmp.Free;
end;
end;
procedure TZoomableCtrl.SetdesignShowFrame(const Value: boolean);
begin
FdesignShowFrame := Value;
Invalidate;
end;
procedure TZoomableCtrl.SetMarginPc(const Value: TMargins);
begin
FMarginPc.Assign(Value);
end;
procedure TZoomableCtrl.SetOrientation(const Value: TprinterOrientation);
begin
if FOrientation<>Value then
begin
FOrientation:= Value;
OrientationChange;
end;
end;
procedure TZoomableCtrl.SetZoom(const Value: integer);
begin
if Fzoom<>value then
begin
if (value<10) or (value>200) then
begin
MessageBox(handle, pchar('&Ccedil;&euml;&Eacute;è&Ouml;&Atilde;&Euml;&otilde;·&Aring;±&Egrave;&Agrave;&yacute;&Ocirc;&Uacute;10%-200%&Ouml;&reg;&frac14;&auml;'),pchar('&Euml;&otilde;·&Aring;±&Egrave;&Agrave;&yacute;'),
MB_OK or MB_ICONERROR);
exit;
end;
Fzoom := Value;
OrientationChange;
end;
end;
procedure TZoomableCtrl.WmErasebkgnd(var msg: TMessage);
begin
msg.Result:=1;
end;
procedure TZoomableCtrl.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
Msg.Result := DLGC_WANTARROWS or DLGC_WANTTAB;
end;
procedure TZoomableCtrl.WmKillFocus(var msg: TMessage);
begin
inherited;
repaint;
end;
procedure TZoomableCtrl.WmSetFocus(var msg: TMessage);
begin
inherited;
repaint;
end;