首页  编辑  

用API直接打印

Tags: /超级猛料/Print.打印/   Date Created:

用API直接打印

uses CommDlg;

{$IFNDEF WIN32}

const MAX_PATH = 144;

{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);

var

Pd : TPrintDlg;

DocInfo: TDocInfo;

begin

FillChar(Pd, sizeof(Pd), #0);

Pd.lStructSize := sizeof(Pd);

Pd.hWndOwner := Form1.Handle;

Pd.Flags := PD_RETURNDC;

if PrintDlg(pd) then begin

FillChar(DocInfo, sizeof(DocInfo), #0);

DocInfo.cbSize := SizeOf(DocInfo);

GetMem(DocInfo.lpszDocName, 32);

GetMem(DocInfo.lpszOutput, MAX_PATH);

lStrCpy(DocInfo.lpszDocName, 'My Document');

{Add this line to print to a file }

lStrCpy(DocInfo.lpszOutput, 'C:\Download\Test.doc');

StartDoc(Pd.hDc, DocInfo);

StartPage(Pd.hDc);

TextOut(Pd.hDc, 100, 100, 'Page 1', 6);

EndPage(Pd.hDc);

StartPage(Pd.hDc);

TextOut(Pd.hDc, 100, 100, 'Page 2', 6);

EndPage(Pd.hDc);

EndDoc(Pd.hDc);

FreeMem(DocInfo.lpszDocName, 32);

FreeMem(DocInfo.lpszOutput, MAX_PATH);

end;

end;