




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#自定义纸张问题经常在软件中需要打印各种各样尺寸的报表,在W2K系统中只能使用API来实现。 1、需要创建一种纸张 2、选定创建的纸张后设置纸张的尺寸实现代码如下:using System;using System.Text;using System.Runtime;using System.Security;using System.Runtime.InteropServices;using System.Windows.Forms;namespace DBSoft.Report.ReportServicepublic class DBPaperSizeStructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)internal struct structPrinterDefaultsMarshalAs(UnmanagedType.LPTStr) public String pDatatype;public IntPtr pDevMode;MarshalAs(UnmanagedType.I4) public int DesiredAccess;DllImport(winspool.Drv, EntryPoint=OpenPrinter, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false,CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern bool OpenPrinter(MarshalAs(UnmanagedType.LPTStr)string printerName,out IntPtr phPrinter,ref structPrinterDefaults pd);DllImport(winspool.Drv, EntryPoint=ClosePrinter, SetLastError=true,CharSet=CharSet.Unicode, ExactSpelling=false,CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern bool ClosePrinter(IntPtr phPrinter);StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)internal struct structSizepublic Int32 width;public Int32 height;StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)internal struct structRectpublic Int32 left;public Int32 top;public Int32 right;public Int32 bottom;StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)internal struct structFormInfo1MarshalAs(UnmanagedType.I4) public int Flags;MarshalAs(UnmanagedType.LPTStr) public String pName;public structSize Size;public structRect ImageableArea;StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)internal struct structDevModeMarshalAs(UnmanagedType.ByValTStr, SizeConst=32) public StringdmDeviceName;MarshalAs(UnmanagedType.U2) public short dmSpecVersion;MarshalAs(UnmanagedType.U2) public short dmDriverVersion;MarshalAs(UnmanagedType.U2) public short dmSize;MarshalAs(UnmanagedType.U2) public short dmDriverExtra;MarshalAs(UnmanagedType.U4) public int dmFields;MarshalAs(UnmanagedType.I2) public short dmOrientation;MarshalAs(UnmanagedType.I2) public short dmPaperSize;MarshalAs(UnmanagedType.I2) public short dmPaperLength;MarshalAs(UnmanagedType.I2) public short dmPaperWidth;MarshalAs(UnmanagedType.I2) public short dmScale;MarshalAs(UnmanagedType.I2) public short dmCopies;MarshalAs(UnmanagedType.I2) public short dmDefaultSource;MarshalAs(UnmanagedType.I2) public short dmPrintQuality;MarshalAs(UnmanagedType.I2) public short dmColor;MarshalAs(UnmanagedType.I2) public short dmDuplex;MarshalAs(UnmanagedType.I2) public short dmYResolution;MarshalAs(UnmanagedType.I2) public short dmTTOption;MarshalAs(UnmanagedType.I2) public short dmCollate;MarshalAs(UnmanagedType.ByValTStr, SizeConst=32) public String dmFormName;MarshalAs(UnmanagedType.U2) public short dmLogPixels;MarshalAs(UnmanagedType.U4) public int dmBitsPerPel;MarshalAs(UnmanagedType.U4) public int dmPelsWidth;MarshalAs(UnmanagedType.U4) public int dmPelsHeight;MarshalAs(UnmanagedType.U4) public int dmNup;MarshalAs(UnmanagedType.U4) public int dmDisplayFrequency;MarshalAs(UnmanagedType.U4) public int dmICMMethod;MarshalAs(UnmanagedType.U4) public int dmICMIntent;MarshalAs(UnmanagedType.U4) public int dmMediaType;MarshalAs(UnmanagedType.U4) public int dmDitherType;MarshalAs(UnmanagedType.U4) public int dmReserved1;MarshalAs(UnmanagedType.U4) public int dmReserved2;DllImport(winspool.Drv, EntryPoint=AddForm, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false,CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern bool AddForm(IntPtr phPrinter,MarshalAs(UnmanagedType.I4) int level, ref structFormInfo1 form);DllImport(winspool.Drv, EntryPoint=DeleteForm, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false,CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern bool DeleteForm(IntPtr phPrinter,MarshalAs(UnmanagedType.LPTStr) string pName);DllImport(winspool.Drv, EntryPoint=SetForm, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false,CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern bool SetForm(IntPtr phPrinter,MarshalAs(UnmanagedType.LPTStr) string pName,MarshalAs(UnmanagedType.I4) int level, ref structFormInfo1 form);DllImport(kernel32.dll, EntryPoint=GetLastError, SetLastError=false,ExactSpelling=true, CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern Int32 GetLastError();DllImport(GDI32.dll, EntryPoint=CreateDC, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false, CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern IntPtr CreateDC(MarshalAs(UnmanagedType.LPTStr)string pDrive,MarshalAs(UnmanagedType.LPTStr) string pName,MarshalAs(UnmanagedType.LPTStr) string pOutput,ref structDevMode pDevMode);DllImport(GDI32.dll, EntryPoint=ResetDC, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false, CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern IntPtr ResetDC(IntPtr hDC, ref structDevModepDevMode);DllImport(GDI32.dll, EntryPoint=DeleteDC, SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false,CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()internal static extern bool DeleteDC(IntPtr hDC);public System.Drawing.Printing.PaperSize GetPrintForm(string printerName,string paperName)System.Drawing.Printing.PaperSize paper=null;System.Drawing.Printing.PrinterSettings printer=new System.Drawing.Printing.PrinterSettings();printer.PrinterName=printerName;foreach(System.Drawing.Printing.PaperSize ps in printer.PaperSizes)if(ps.PaperName.ToLower()=paperName.ToLower()paper=ps;break;return paper;public void SetPrintForm(string printerName,string paperName,float width,float height)if (PlatformID.Win32NT = Environment.OSVersion.Platform)const int PRINTER_ACCESS_USE = 0x00000008;const int PRINTER_ACCESS_ADMINISTER = 0x00000004;structPrinterDefaults defaults = new structPrinterDefaults();defaults.pDatatype = null;defaults.pDevMode = IntPtr.Zero;defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;IntPtr hPrinter = IntPtr.Zero;if (OpenPrinter(printerName, out hPrinter, ref defaults)trystructFormInfo1 formInfo = new structFormInfo1();formInfo.Flags = 0;formInfo.pName = paperName;formInfo.Size.width = (int)(width * 100.0);formInfo.Size.height = (int)(height * 100.0);formInfo.ImageableArea.left = 0;formInfo.ImageableArea.right = formInfo.Size.width;formInfo.ImageableArea.top = 0;formInfo.ImageableArea.bottom = formInfo.Size.height;bool rslt=false;if(GetPrintForm(printerName,paperName)!=null)rslt=SetForm(hPrinter,paperName,1,ref formInfo);elsethis.AddCustomPaperSize(printerName,paperName,width,height);rslt=true;if(!rslt)StringBuilder strBuilder = new StringBuilder();strBuilder.AppendFormat(添加纸张0时发生错误!, 系统错误号: 1, paperName, GetLastError();/throw new ApplicationException(strBuilder.ToString();MessageBox.Show(strBuilder.ToString();finallyClosePrinter(hPrinter);public void AddCustomPaperSize(string printerName, string paperName, float width, float height)if (PlatformID.Win32NT = Environment.OSVersion.Platform)const int PRINTER_ACCESS_USE = 0x00000008;const int PRINTER_ACCESS_ADMINISTER = 0x00000004;structPrinterDefaults defaults = new structPrinterDefaults();defaults.pDatatype = null;defaults.pDevMode = IntPtr.Zero;defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;IntPtr hPrinter = IntPtr.Zero;if (OpenPrinter(printerName, out hPrinter, ref defaults)tryDeleteForm(hPrinter, paperName);structFormInfo1 formInfo = new structFormInfo1();formInfo.Flags = 0;formInfo.pName = paperName;formInfo.Size.width = (int)(width * 100.0);formInfo.Size.height = (int)(height * 100.0);formInfo.ImageableArea.left = 0;formInfo.ImageableArea.right = formInfo.Size.width;formInfo.ImageableArea.top = 0;formInfo.ImageableArea.bottom = formInfo.Size.height;if (!AddForm(hPrinter, 1,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国食品蜡项目商业计划书
- 中国膦酸盐项目投资计划书
- 中国哈巴俄苷项目商业计划书
- 中国负离子纤维项目商业计划书
- 2025年明光市明古新材料有限公司介绍企业发展分析报告
- 中国金属空气电池项目商业计划书
- 忻州市中医院透析患者饮食指导考核
- 通辽市人民医院全科医学科年度综合能力考核
- 忻州市中医院门静脉系统超声考核
- 中国高纯铟项目创业计划书
- 2024年广东省中考满分作文《当好自己故事的主角》2
- 人教版一年级上册数学期中试卷(共5套-可直接打印)
- DB37-T 5001-2021 住宅工程外窗水密性现场检测技术规程
- CVD 碳化硅涂层产品技术要求
- 2024版以房抵债协议范本
- 心衰患者出入量管理研究进展
- 马克思主义制度经济理论知到智慧树章节测试课后答案2024年秋上海财经大学
- 安全部经理竞聘汇报
- 《智能建造技术与装备》 课件 第二章 BIM技术与应用
- 2024年中国香辣酥市场调查研究报告
- 绵阳市高中2022级(2025届)高三第一次诊断性考试(一诊)生物试卷(含标准答案)
评论
0/150
提交评论