windows一个特点就是设备无关性,这样就给程序控制打印机提供了很好的方法。
首先引用“泥人张”写的打印API类。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing.Printing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace PrintAPI
{
public class Printer
{
private Printer()
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
///泥人张版本加强版
API声明 API声明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
internal static int GetPrinterStatusInt(string PrinterName)
{
int intRet = 0;
IntPtr hPrinter;
structPrinterDefaults defaults = new structPrinterDefaults();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
{
int cbNeeded = 0;
bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
if (cbNeeded > 0)
{
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
if (bolRet)
{
PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
intRet = System.Convert.ToInt32(Info2.Status);
}
Marshal.FreeHGlobal(pAddr);
}
ClosePrinter(hPrinter);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return intRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
{
uint cbNeeded = 0;
uint cReturned = 0;
bool ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (ret)
{
PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int offset = pAddr.ToInt32();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < cReturned; i++)
{
Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].Attributes = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.FreeHGlobal(pAddr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Info2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
else
{
return new PRINTER_INFO_2[0];
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
获取当前指定打印机的状态 获取当前指定打印机的状态
删除已经存在的自定义纸张 删除已经存在的自定义纸张
指定的打印机设置以mm为单位的自定义纸张(Form) 指定的打印机设置以mm为单位的自定义纸张(Form)
获取本地打印机列表 获取本地打印机列表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
获取本机的默认打印机名称 获取本机的默认打印机名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
设置默认打印机 设置默认打印机
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
判断打印机是否在系统可用的打印机列表中 判断打印机是否在系统可用的打印机列表中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
判断表单是否在指定的打印机所支持的纸张列表中 判断表单是否在指定的打印机所支持的纸张列表中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
英寸到厘米的转换 英寸到厘米的转换
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
然后在程序里调用写好的API即可。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace PrintAPI
...
使用到的Printer.ini配置文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[Printer]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
InvoicePrinter= pdfFactory Pro
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ReceiptPrinter = pdfFactory Pro
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[BillSize]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
InvoiceWidth = 706
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
InvoiceHeight = 515
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ReceiptWidth = 706
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ReceiptHeight = 515
是不是很简单呢?