用printer怎样实现超市的小票打印?

wcqty 2004-07-28 09:14:47
其实就是马上把我要打印的东西打出来!

在窗体上可以用Form1.Canvas.FrameRect

而printer.Canvas 没有这个方法!
要打印其中的内容就必须要 用printer.EndDoc

我现在想实现输入一条就打印一条,到最后结帐的时候才调用printer.EndDoc结束打印,怎么实现
...全文
164 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangheaaa 2004-07-28
  • 打赏
  • 举报
回复
你可以自己控制打印什么时候结束,
也可以每打印一条就调用printer.EndDoc结束打印,
当打印下一条时,在重新开始一次也可以。

如果想更灵活的控制打印,可以参考使用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;
//////////////////////////////////////////////////////
uses winspool;

procedure PrintFile(const sFileName: string);
const
BufSize = 16384;
type
TDoc_Info_1 = record
pDocName: pChar;
pOutputFile: pChar;
pDataType: pChar;
end;
var
Count, BytesWritten: integer;
hPrinter: THandle;
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDeviceMode: THandle;
DocInfo: TDoc_Info_1;
f: file;
Buffer: Pointer;
begin
Printer.PrinterIndex := -1;
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
if not WinSpool.OpenPrinter(@Device, hPrinter, nil) then exit;
DocInfo.pDocName := 'MyDocument';
DocInfo.pOutputFile := nil;
DocInfo.pDatatype := 'RAW';
if StartDocPrinter(hPrinter, 1, @DocInfo) = 0 then
begin
WinSpool.ClosePrinter(hPrinter);
exit;
end;
if not StartPagePrinter(hPrinter) then
begin
EndDocPrinter(hPrinter);
WinSpool.ClosePrinter(hPrinter);
exit;
end;
System.Assign(f, sFileName);
try
Reset(f, 1);
GetMem(Buffer, BufSize);
while not eof(f) do
begin
Blockread(f, Buffer^, BufSize, Count);
if Count > 0 then
begin
if not WritePrinter(hPrinter, Buffer, Count, BytesWritten) then
begin
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
WinSpool.ClosePrinter(hPrinter);
FreeMem(Buffer, BufSize);
exit;
end;
end;
end;
FreeMem(Buffer, BufSize);
EndDocPrinter(hPrinter);
WinSpool.ClosePrinter(hPrinter);
finally
System.Closefile( f );
end;
end;

*************************************
procedure WriteRawStringToPrinter(PrinterName: String; S: String);
var
Handle: THandle;
N: DWORD;
DocInfo1: TDocInfo1;
begin
if not OpenPrinter(PChar(PrinterName), Handle, nil) then
begin
ShowMessage('error ' + IntToStr(GetLastError));
Exit;
end;
with DocInfo do
begin
pDocName := PChar('test doc');
pOutputFile := nil;
pDataType := 'RAW';
end;
StartDocPrinter(Handle, 1, ocInfo);
StartPagePrinter(Handle);
WritePrinter(Handle, PChar(S), Length(S), N);
EndPagePrinter(Handle);
EndDocPrinter(Handle);
ClosePrinter(Handle);
end;

The PrinterName parameter must be the name of the printer as it is
installed. For example, if the name of the printer
is "HP LaserJet 5MP" then that is what you should pass.
</quote>
/////////////////////////////////////////////////////

Use CreateFile to get a handle to LPT1

LPTHandle := CreateFile( 'LPT1',GENERIC_WRITE,
0, PSecurityAttributes(nil),
OPEN_EXISTING, FILE_FLAG_OVERLAPPED,
0);


Then use WriteFile to send a string of characters or use

While not
TransmitCommChar( LPTHandle, CharToSend ) do
Application.ProcessMessages;

It sends one raw character at a time to the parallel port. It waits for the recent character to get processed and then immediately sends a new one. I got it printing stuff
pretty fast.

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧