码迷,mamicode.com
首页 > Windows程序 > 详细

C#使用window API 控制打印纸张大小(转载)

时间:2016-01-22 02:43:16      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

windows一个特点就是设备无关性,这样就给程序控制打印机提供了很好的方法。

首先引用“泥人张”写的打印API类。

技术分享using System;
技术分享using System.Collections;
技术分享using System.Text;
技术分享using System.Runtime.InteropServices;
技术分享using System.Security;
技术分享using System.ComponentModel;
技术分享using System.Drawing.Printing;
技术分享
技术分享namespace PrintAPI
技术分享{
技术分享    public class Printer
技术分享    {
技术分享        private Printer()
技术分享        {
技术分享
技术分享        }
技术分享        ///泥人张版本加强版
技术分享        API声明   API声明
技术分享
技术分享        internal static int GetPrinterStatusInt(string PrinterName)
技术分享        {
技术分享            int intRet = 0;
技术分享            IntPtr hPrinter;
技术分享            structPrinterDefaults defaults = new structPrinterDefaults();
技术分享
技术分享            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();
技术分享
技术分享                        Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
技术分享
技术分享                        intRet = System.Convert.ToInt32(Info2.Status);
技术分享                    }
技术分享                    Marshal.FreeHGlobal(pAddr);
技术分享                }
技术分享                ClosePrinter(hPrinter);
技术分享            }
技术分享
技术分享            return intRet;
技术分享        }
技术分享
技术分享        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);
技术分享
技术分享            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
技术分享            ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);
技术分享
技术分享            if (ret)
技术分享            {
技术分享                PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];
技术分享
技术分享                int offset = pAddr.ToInt32();
技术分享
技术分享                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;
技术分享
技术分享                }
技术分享
技术分享                Marshal.FreeHGlobal(pAddr);
技术分享
技术分享                return Info2;
技术分享
技术分享            }
技术分享            else
技术分享            {
技术分享                return new PRINTER_INFO_2[0];
技术分享            }
技术分享        }
技术分享
技术分享        获取当前指定打印机的状态 获取当前指定打印机的状态
技术分享        删除已经存在的自定义纸张 删除已经存在的自定义纸张
技术分享        指定的打印机设置以mm为单位的自定义纸张(Form) 指定的打印机设置以mm为单位的自定义纸张(Form)
技术分享        
技术分享        获取本地打印机列表 获取本地打印机列表
技术分享
技术分享        获取本机的默认打印机名称 获取本机的默认打印机名称
技术分享
技术分享        设置默认打印机 设置默认打印机
技术分享
技术分享        判断打印机是否在系统可用的打印机列表中 判断打印机是否在系统可用的打印机列表中
技术分享
技术分享        判断表单是否在指定的打印机所支持的纸张列表中 判断表单是否在指定的打印机所支持的纸张列表中
技术分享
技术分享        判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
技术分享
技术分享        英寸到厘米的转换 英寸到厘米的转换
技术分享    }
技术分享
技术分享}

 然后在程序里调用写好的API即可。

技术分享using System;
技术分享using System.Collections.Generic;
技术分享using System.ComponentModel;
技术分享using System.Data;
技术分享using System.Drawing;
技术分享using System.Text;
技术分享using System.Windows.Forms;
技术分享using System.Runtime.InteropServices;
技术分享using System.IO;
技术分享
技术分享namespace PrintAPI
技术分享...

 使用到的Printer.ini配置文件

技术分享[Printer]
技术分享InvoicePrinter= pdfFactory Pro
技术分享ReceiptPrinter = pdfFactory Pro
技术分享
技术分享[BillSize]
技术分享InvoiceWidth = 706
技术分享InvoiceHeight = 515
技术分享
技术分享ReceiptWidth = 706
技术分享ReceiptHeight = 515

是不是很简单呢?

C#使用window API 控制打印纸张大小(转载)

标签:

原文地址:http://www.cnblogs.com/jiangyuxuan/p/5149944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!