WritePrinter 向印表機輸出資料

來源:互聯網
上載者:User
OpenPrinter, WritePrinter,直接寫印表機連接埠,只要知道印表機採用的語言就可以。////////////////WritePrinter   向印表機輸出資料
例:
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.

 

 

#4樓 得分:0回複於:2003-02-14 14:42:45

The   WritePrinter   function   informs   the   print   spooler   that   data   should   be   written   to   the   specified   printer.  

BOOL   WritePrinter(

        HANDLE   hPrinter, //   handle   to   printer   object  
        LPVOID   pBuf, //   pointer   to   array   that   contains   printer   data  
        DWORD   cbBuf, //   size,   in   bytes,   of   array  
        LPDWORD   pcWritten   //   addr.   of   variable   with   count   of   bytes   written  
      );
 

Parameters

hPrinter

Identifies   the   printer.  

pBuf

Points   to   an   array   of   bytes   that   contains   the   data   that   should   be   written   to   the   printer.  

cbBuf

Specifies   the   size,   in   bytes,   of   the   array.  

pcWritten

Points   to   a   value   that   specifies   the   number   of   bytes   of   data   that   were   written   to   the   printer.  

 

Return   Values

If   the   function   succeeds,   the   return   value   is   nonzero.
If   the   function   fails,   the   return   value   is   zero.   To   get   extended   error   information,   call   GetLastError

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.