




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 法律科技(LawTech)专员考试试卷及答案
- 2025年金溪县遴选教师考试笔试试题【答案】
- 2025年海水淡化及水处理设备项目建议书
- 2025年山西省住房和城乡建设厅下属事业单位招聘考试笔试试题【答案】
- 2025年宁波市奉化区交通控股集团有限公司招聘考试笔试试题【答案】
- 2025年吉林省长白山公安局招聘警务辅助人员考试试题【答案】
- 2025年南宁市第十三中学招聘初中顶岗教师考试笔试试题【答案】
- 2025年乐山市沙湾区妇幼保健院招聘专技人员考试试题【答案】
- 2025年乙酸甲酯项目合作计划书
- 大学生家具厂实习报告范文
- 小学一至六年级《体育与健康》健康课教案
- 2025中国临床肿瘤学会CSCO非小细胞肺癌诊疗指南要点解读课件
- 2025年全国工业锅炉G1证理论考试笔试试题(400题)含答案
- 泛微实施面试题及答案
- 试验检测管理制度
- 管道施工应急预案
- 贸易安全培训
- 2025年1月国家开放大学法学本科《中国法律史》期末纸质考试试题及答案
- 初中地理跨学科主题学习设计与实施
- 2021卫生监督法律法规知识竞赛题库及答案
- 惩罚游戏学校班会公司早会小游戏晨会年会团建课堂娱乐互动340
评论
0/150
提交评论