| | |
| | | using System.Drawing; |
| | | using System.Drawing.Printing; |
| | | using System.Linq; |
| | | using System.Runtime.InteropServices; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using System.Windows.Forms; |
| | | using ZXing; |
| | | using ZXing.Common; |
| | | |
| | | //using System.Printing; |
| | | |
| | | namespace Bro.M141.Process |
| | | { |
| | |
| | | ttheigh = 160; |
| | | message = str; |
| | | |
| | | this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)"; |
| | | this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)"; |
| | | this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh); |
| | | |
| | | this.printDocument1.PrintController = new System.Drawing.Printing.StandardPrintController(); |
| | |
| | | return; |
| | | } |
| | | PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); |
| | | this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240 (300 dpi)"; |
| | | this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)"; |
| | | this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh); |
| | | this.printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument_Print); |
| | | printPreviewDialog1.Document = printDocument1; |
| | | DialogResult res = printPreviewDialog1.ShowDialog(); |
| | | printPreviewDialog1.Document.Dispose(); |
| | | } |
| | | |
| | | #region |
| | | [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] |
| | | private static extern bool OpenPrinter(string pPrinterName, out IntPtr hPrinter, IntPtr pDefault); |
| | | |
| | | |
| | | [DllImport("winspool.drv", SetLastError = true)] |
| | | private static extern bool ClosePrinter(IntPtr hPrinter); |
| | | |
| | | |
| | | [DllImport("winspool.drv", SetLastError = true)] |
| | | private static extern bool GetPrinter(IntPtr hPrinter, |
| | | int dwLevel, IntPtr pPrinter, int cbBuf, out int pcbNeeded); |
| | | |
| | | |
| | | [DllImport("winspool.drv", CharSet = CharSet.Auto)] |
| | | public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf, |
| | | out int pcbNeeded, out int pcReturned); |
| | | |
| | | |
| | | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| | | public struct PRINTER_INFO_2 |
| | | { |
| | | public string pServerName; |
| | | public string pPrinterName; |
| | | public string pShareName; |
| | | public string pPortName; |
| | | public string pDriverName; |
| | | public string pComment; |
| | | public string pLocation; |
| | | public IntPtr pDevMode; |
| | | public string pSepFile; |
| | | public string pPrintProcessor; |
| | | public string pDatatype; |
| | | public string pParameters; |
| | | public IntPtr pSecurityDescriptor; |
| | | public uint Attributes; |
| | | public uint Priority; |
| | | public uint DefaultPriority; |
| | | public uint StartTime; |
| | | public uint UntilTime; |
| | | public uint Status; |
| | | public uint cJobs; |
| | | public uint AveragePPM; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 默认打印机的名称 |
| | | /// </summary> |
| | | //private static string printerName = new LocalPrintServer().DefaultPrintQueue.Name; |
| | | private static string printerName = "Honeywell PX240S (300 dpi)"; |
| | | |
| | | /// <summary> |
| | | /// 获取打印机的状态编码(方式1) |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static int GetPrinterStatusCodeInt() |
| | | { |
| | | int intRet = -1; |
| | | IntPtr hPrinter; |
| | | |
| | | |
| | | if (OpenPrinter(printerName, out hPrinter, IntPtr.Zero)) |
| | | { |
| | | 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(); |
| | | |
| | | |
| | | Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2)); |
| | | |
| | | |
| | | intRet = System.Convert.ToInt32(Info2.Status); |
| | | } |
| | | Marshal.FreeHGlobal(pAddr); |
| | | } |
| | | ClosePrinter(hPrinter); |
| | | } |
| | | |
| | | |
| | | return intRet; |
| | | } |
| | | /// <summary> |
| | | /// 检查打印机是否可用(方式1) |
| | | /// </summary> |
| | | /// <param name="intStatusCodeValue"></param> |
| | | /// <returns></returns> |
| | | public static bool CheckIsEnable(int intStatusCodeValue) |
| | | { |
| | | bool isEnable = false; |
| | | if (UIPrinter.GetTaskNumber() == 0) |
| | | { |
| | | return true; |
| | | } |
| | | if (intStatusCodeValue == 0x0008000 || intStatusCodeValue == 0x00000400 || intStatusCodeValue == 0x00004000) |
| | | { |
| | | return true; |
| | | } |
| | | return isEnable; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 获取打印机的状态信息(方式1) |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static string GetPrinterStatusMessage(int intStatusCodeValue) |
| | | { |
| | | string strRet = string.Empty; |
| | | switch (intStatusCodeValue) |
| | | { |
| | | case 0: |
| | | strRet = "准备就绪(Ready)"; |
| | | break; |
| | | case 0x00000200: |
| | | strRet = "忙(Busy)"; |
| | | break; |
| | | case 0x00400000: |
| | | strRet = "被打开(Printer Door Open)"; |
| | | break; |
| | | case 0x00000002: |
| | | strRet = "错误(Printer Error)"; |
| | | break; |
| | | case 0x0008000: |
| | | strRet = "初始化(Initializing)"; |
| | | break; |
| | | case 0x00000100: |
| | | strRet = "正在输入,输出(I/O Active)"; |
| | | break; |
| | | case 0x00000020: |
| | | strRet = "手工送纸(Manual Feed)"; |
| | | break; |
| | | case 0x00040000: |
| | | strRet = "无墨粉(No Toner)"; |
| | | break; |
| | | case 0x00001000: |
| | | strRet = "不可用(Not Available)"; |
| | | break; |
| | | case 0x00000080: |
| | | strRet = "脱机(Off Line)"; |
| | | break; |
| | | case 0x00200000: |
| | | strRet = "内存溢出(Out of Memory)"; |
| | | break; |
| | | case 0x00000800: |
| | | strRet = "输出口已满(Output Bin Full)"; |
| | | break; |
| | | case 0x00080000: |
| | | strRet = "当前页无法打印(Page Punt)"; |
| | | break; |
| | | case 0x00000008: |
| | | strRet = "塞纸(Paper Jam)"; |
| | | break; |
| | | case 0x00000010: |
| | | strRet = "打印纸用完(Paper Out)"; |
| | | break; |
| | | case 0x00000040: |
| | | strRet = "纸张问题(Page Problem)"; |
| | | break; |
| | | case 0x00000001: |
| | | strRet = "暂停(Paused)"; |
| | | break; |
| | | case 0x00000004: |
| | | strRet = "正在删除(Pending Deletion)"; |
| | | break; |
| | | case 0x00000400: |
| | | strRet = "正在打印(Printing)"; |
| | | break; |
| | | case 0x00004000: |
| | | strRet = "正在处理(Processing)"; |
| | | break; |
| | | case 0x00020000: |
| | | strRet = "墨粉不足(Toner Low)"; |
| | | break; |
| | | case 0x00100000: |
| | | strRet = "需要用户干预(User Intervention)"; |
| | | break; |
| | | case 0x20000000: |
| | | strRet = "等待(Waiting)"; |
| | | break; |
| | | case 0x00010000: |
| | | strRet = "热机中(Warming Up)"; |
| | | break; |
| | | case 1048706: |
| | | strRet = "色带已用尽"; |
| | | break; |
| | | default: |
| | | strRet = "未知状态(Unknown Status)"; |
| | | break; |
| | | } |
| | | return strRet; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取打印机正在打印的任务数(方式1) |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static int GetTaskNumber() |
| | | { |
| | | IntPtr handle; |
| | | int FirstJob = 0; |
| | | int NumJobs = 127; |
| | | int pcbNeeded; |
| | | int pcReturned = -1; |
| | | OpenPrinter(printerName, out handle, IntPtr.Zero); |
| | | // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127) |
| | | EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned); |
| | | |
| | | // allocate unmanaged memory |
| | | IntPtr pData = Marshal.AllocHGlobal(pcbNeeded); |
| | | |
| | | // get structs |
| | | EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned); |
| | | |
| | | return pcReturned; |
| | | } |
| | | #endregion |
| | | |
| | | |
| | | private void button3_Click(object sender, EventArgs e) |
| | | { |
| | | int taskNum = GetTaskNumber(); |
| | | int statuscode = GetPrinterStatusCodeInt(); |
| | | string status = GetPrinterStatusMessage(statuscode); |
| | | //只有在打印机异常时再次下发打印任务后,才能获取到打印机状态。这是因为打印机驱动程序通常只在处理打印请求时更新其状态信息。如果打印机没有接收到新的打印任务,可能无法立即反映出真实状态。 |
| | | textBox2.Text = "taskNum:" + taskNum+ ",statuscode:"+ statuscode+",status:"+status; |
| | | } |
| | | } |
| | | } |