熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

直接把結果輸出到打印機

2022-06-13   來源: .NET編程 

  下面的代碼可以通過調用WIN API讓你直接輸出到打印機因而可以達到最快的速度進行打印而不是等待Windows的打印系統此外示例代碼還說明了如何向打印機發送PCL代碼
  
  // PrintDirectcs
  // 本文參考了Microsoft Support 文檔號Q
  // 本代碼假定你在file:///hpl存在共享打印機
  // 本代碼示例了如何向打印機發送Hewlett Packard PCL代碼直接在頁面中央打印出一個矩形
  
  
  using System;
  using SystemText;
  using SystemRuntimeInteropServices;
  
  [StructLayout( LayoutKindSequential)]
  public struct DOCINFO
  {
   [MarshalAs(UnmanagedTypeLPWStr)]public string pDocName;
   [MarshalAs(UnmanagedTypeLPWStr)]public string pOutputFile;
   [MarshalAs(UnmanagedTypeLPWStr)]public string pDataType;
  }
  
  public class PrintDirect
  {
   [ DllImport( winspooldrvCharSet=CharSetUnicodeExactSpelling=false
   CallingConvention=CallingConventionStdCall )]
   public static extern long OpenPrinter(string pPrinterNameref IntPtr phPrinter
   int pDefault);
   [ DllImport( winspooldrvCharSet=CharSetUnicodeExactSpelling=false
   CallingConvention=CallingConventionStdCall )]
   public static extern long StartDocPrinter(IntPtr hPrinter int Level
   ref DOCINFO pDocInfo);
  
   [ DllImport(winspooldrvCharSet=CharSetUnicodeExactSpelling=true
   CallingConvention=CallingConventionStdCall)]
   public static extern long StartPagePrinter(IntPtr hPrinter);
   [ DllImport( winspooldrvCharSet=CharSetAnsiExactSpelling=true
   CallingConvention=CallingConventionStdCall)]
   public static extern long WritePrinter(IntPtr hPrinterstring data
   int bufref int pcWritten);
  
   [ DllImport( winspooldrv CharSet=CharSetUnicodeExactSpelling=true
   CallingConvention=CallingConventionStdCall)]
   public static extern long EndPagePrinter(IntPtr hPrinter);
  
   [ DllImport( winspooldrvCharSet=CharSetUnicodeExactSpelling=true
   CallingConvention=CallingConventionStdCall)]
   public static extern long EndDocPrinter(IntPtr hPrinter);
  
   [ DllImport(winspooldrvCharSet=CharSetUnicodeExactSpelling=true
   CallingConvention=CallingConventionStdCall )]
   public static extern long ClosePrinter(IntPtr hPrinter);
  }
  
  public class App
  {
   public static void Main ()
   {
   SystemIntPtr lhPrinter=new SystemIntPtr();
  
   DOCINFO di = new DOCINFO();
   int pcWritten=;
   string st;
  
   // text to print with a form feed character
   st=This is an example of printing directly to a printer\f;
   dipDocName=my test document;
   dipDataType=RAW;
  
   // the \xb means an ascii escape character
   st=\xb*cabP\f;
   //lhPrinter contains the handle for the printer opened
   //If lhPrinter is then an error has occured
   PrintDirectOpenPrinter(\\\\\\hplref lhPrinter);
   PrintDirectStartDocPrinter(lhPrinterref di);
   PrintDirectStartPagePrinter(lhPrinter);
   try
   {
   // Moves the cursor dots ( inches at dpi) in from the left margin and
   // dots ( inches at dpi) down from the top margin
   st=\xb*pxY;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
  
   // Using the print model commands for rectangle dimensions a specifies a rectangle
   // with a horizontal size or width of dots and b specifies a vertical
   // size or height of dots The P selects the solid black rectangular area fill
   st=\xb*cabP;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
  
   // Specifies a rectangle with width of dots height of dots and a
   // fill pattern of solid black
   st=\xb*cabP;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
   // Moves the current cursor position to dots from the left margin and
   // dots down from the top margin
   st=\xb*pxY;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
   // Specifies a rectangle with a width of dots a height of dots and a
  
   // fill pattern of solid black
   st=\xb*cabP;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
  
   // Moves the current cursor position to dots from the left margin and
   // dots down from the top margin
   st=\xb*pxY;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
  
   // Specifies a rectangle with a width of dots a height of dots and a
   // fill pattern of solid black
   st=\xb*cabP;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
   // Send a form feed character to the printer
   st=\f;
   PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);
   }
   catch (Exception e)
   {
   ConsoleWriteLine(eMessage);
   }
  
   PrintDirectEndPagePrinter(lhPrinter);
   PrintDirectEndDocPrinter(lhPrinter);
   PrintDirectClosePrinter(lhPrinter);
   }
  }


From:http://tw.wingwit.com/Article/program/net/201311/12794.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.