RDLC报表(七)


        有关LocalReport、DeviceInfo和PrintDocument的内容已经介绍得差不多了,稍后会给出一个继承自System.Drawing.Printing.PrintDocument的组件EMFStreamPrintDocument。但是现在,来看一下如何进行自定义纸张票据打印时的页面设置。页面设置窗体如下图所示:

o_pagesetting.gif

        如何添加、删除自定义大小的纸张、枚举系统的打印机?以前在博客园的一篇随笔中参加过讨论,见http://wormday.cnblogs.com/archive/2005/12/22/302635.aspx。当然还是使用Win32 API,以下是我封装的一个关于打印机控制的类[以前用VB实现过比这个类还要多的关于打印机控制的功能,但是在C#中感到还是挺困难的,所以这次只给出了够用的功能:获取当前指定打印机的状态、删除已经存在的自定义纸张、指定的打印机设置以mm为单位的自定义纸张(Form)、获取本地打印机列表、获取本机的默认打印机名称、设置默认打印机、判断打印机是否在系统可用的打印机列表中、判断表单是否在指定的打印机所支持的纸张列表中、判断指定纸张的宽度和高度和在文本框中指定的宽度和高度是否匹配、英尺到厘米的转换]:

None.gif using  System;
None.gif
using  System.Text;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Security;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing.Printing;
None.gif
None.gif
namespace  RDLCReport
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
InBlock.gif    
public class Printer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Printer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
API声明#region API声明
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Auto)]
InBlock.gif        
internal struct structPrinterDefaults
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)]
InBlock.gif            
public String pDatatype;
InBlock.gif            
public IntPtr pDevMode;
InBlock.gif            [MarshalAs(UnmanagedType.I4)]
InBlock.gif            
public int DesiredAccess;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "OpenPrinter", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall),
InBlock.gif        SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)]
InBlock.gif            
string printerName,
InBlock.gif            
out IntPtr phPrinter,
InBlock.gif            
ref structPrinterDefaults pd);
InBlock.gif
InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = false,
InBlock.gif             CallingConvention 
= CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern bool ClosePrinter(IntPtr phPrinter);
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Auto)]
InBlock.gif        
internal struct structSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public Int32 width;
InBlock.gif            
public Int32 height;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Auto)]
InBlock.gif        
internal struct structRect
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public Int32 left;
InBlock.gif            
public Int32 top;
InBlock.gif            
public Int32 right;
InBlock.gif            
public Int32 bottom;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Explicit, CharSet 
= CharSet.Unicode)]
InBlock.gif        
internal struct FormInfo1
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            [FieldOffset(
0), MarshalAs(UnmanagedType.I4)]
InBlock.gif            
public uint Flags;
InBlock.gif            [FieldOffset(
4), MarshalAs(UnmanagedType.LPWStr)]
InBlock.gif            
public String pName;
InBlock.gif            [FieldOffset(
8)]
InBlock.gif            
public structSize Size;
InBlock.gif            [FieldOffset(
16)]
InBlock.gif            
public structRect ImageableArea;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Ansi)]
InBlock.gif        
internal struct structDevMode
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
= 32)]
InBlock.gif            
public String
InBlock.gifdmDeviceName;
InBlock.gif            [MarshalAs(UnmanagedType.U2)]
InBlock.gif            
public short dmSpecVersion;
InBlock.gif            [MarshalAs(UnmanagedType.U2)]
InBlock.gif            
public short dmDriverVersion;
InBlock.gif            [MarshalAs(UnmanagedType.U2)]
InBlock.gif            
public short dmSize;
InBlock.gif            [MarshalAs(UnmanagedType.U2)]
InBlock.gif            
public short dmDriverExtra;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmFields;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmOrientation;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmPaperSize;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmPaperLength;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmPaperWidth;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmScale;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmCopies;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmDefaultSource;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmPrintQuality;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmColor;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmDuplex;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmYResolution;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmTTOption;
InBlock.gif            [MarshalAs(UnmanagedType.I2)]
InBlock.gif            
public short dmCollate;
InBlock.gif            [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
= 32)]
InBlock.gif            
public String dmFormName;
InBlock.gif            [MarshalAs(UnmanagedType.U2)]
InBlock.gif            
public short dmLogPixels;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmBitsPerPel;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmPelsWidth;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmPelsHeight;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmNup;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmDisplayFrequency;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmICMMethod;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmICMIntent;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmMediaType;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmDitherType;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmReserved1;
InBlock.gif            [MarshalAs(UnmanagedType.U4)]
InBlock.gif            
public int dmReserved2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Auto)]
InBlock.gif        
internal struct PRINTER_INFO_9
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public IntPtr pDevMode;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "AddFormW", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = true,
InBlock.gif             CallingConvention 
= CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern bool AddForm(
InBlock.gif         IntPtr phPrinter,
InBlock.gif            [MarshalAs(UnmanagedType.I4)] 
int level,
InBlock.gif         
ref FormInfo1 form);
InBlock.gif
InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "DeleteForm", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall),
InBlock.gif        SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern bool DeleteForm(
InBlock.gif         IntPtr phPrinter,
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)] 
string pName);
InBlock.gif
InBlock.gif        [DllImport(
"kernel32.dll", EntryPoint = "GetLastError", SetLastError = false,
InBlock.gif             ExactSpelling 
= true, CallingConvention = CallingConvention.StdCall),
InBlock.gif        SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern Int32 GetLastError();
InBlock.gif
InBlock.gif        [DllImport(
"GDI32.dll", EntryPoint = "CreateDC", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = false,
InBlock.gif             CallingConvention 
= CallingConvention.StdCall),
InBlock.gif        SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern IntPtr CreateDC([MarshalAs(UnmanagedType.LPTStr)]
InBlock.gif            
string pDrive,
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)] 
string pName,
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)] 
string pOutput,
InBlock.gif            
ref structDevMode pDevMode);
InBlock.gif
InBlock.gif        [DllImport(
"GDI32.dll", EntryPoint = "ResetDC", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = false,
InBlock.gif             CallingConvention 
= CallingConvention.StdCall),
InBlock.gif        SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern IntPtr ResetDC(
InBlock.gif         IntPtr hDC,
InBlock.gif         
ref structDevMode
InBlock.gif            pDevMode);
InBlock.gif
InBlock.gif        [DllImport(
"GDI32.dll", EntryPoint = "DeleteDC", SetLastError = true,
InBlock.gif             CharSet 
= CharSet.Unicode, ExactSpelling = false,
InBlock.gif             CallingConvention 
= CallingConvention.StdCall),
InBlock.gif        SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern bool DeleteDC(IntPtr hDC);
InBlock.gif
InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "SetPrinterA", SetLastError = true,
InBlock.gif            CharSet 
= CharSet.Auto, ExactSpelling = true,
InBlock.gif            CallingConvention 
= CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
InBlock.gif        
internal static extern bool SetPrinter(
InBlock.gif           IntPtr hPrinter,
InBlock.gif           [MarshalAs(UnmanagedType.I4)] 
int level,
InBlock.gif           IntPtr pPrinter,
InBlock.gif           [MarshalAs(UnmanagedType.I4)] 
int command);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         LONG DocumentProperties(
InBlock.gif           HWND hWnd,               // handle to parent window 
InBlock.gif           HANDLE hPrinter,         // handle to printer object
InBlock.gif           LPTSTR pDeviceName,      // device name
InBlock.gif           PDEVMODE pDevModeOutput, // modified device mode
InBlock.gif           PDEVMODE pDevModeInput,  // original device mode
InBlock.gif           DWORD fMode              // mode options
InBlock.gif           );
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "DocumentPropertiesA", SetLastError = true,
InBlock.gif        ExactSpelling 
= true, CallingConvention = CallingConvention.StdCall)]
InBlock.gif        
internal static extern int DocumentProperties(
InBlock.gif           IntPtr hwnd,
InBlock.gif           IntPtr hPrinter,
InBlock.gif           [MarshalAs(UnmanagedType.LPStr)] 
string pDeviceName,
InBlock.gif           IntPtr pDevModeOutput,
InBlock.gif           IntPtr pDevModeInput,
InBlock.gif           
int fMode
InBlock.gif           );
InBlock.gif
InBlock.gif        [DllImport(
"winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true,
InBlock.gif        ExactSpelling 
= true, CallingConvention = CallingConvention.StdCall)]
InBlock.gif        
internal static extern bool GetPrinter(
InBlock.gif           IntPtr hPrinter,
InBlock.gif           
int dwLevel,
InBlock.gif           IntPtr pPrinter,
InBlock.gif           
int dwBuf,
InBlock.gif           
out int dwNeeded
InBlock.gif           );
InBlock.gif
InBlock.gif        [Flags]
InBlock.gif        
internal enum SendMessageTimeoutFlags : uint
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SMTO_NORMAL 
= 0x0000,
InBlock.gif            SMTO_BLOCK 
= 0x0001,
InBlock.gif            SMTO_ABORTIFHUNG 
= 0x0002,
InBlock.gif            SMTO_NOTIMEOUTIFNOTHUNG 
= 0x0008
ExpandedSubBlockEnd.gif        }

InBlock.gif        
const int WM_SETTINGCHANGE = 0x001A;
InBlock.gif        
const int HWND_BROADCAST = 0xffff;
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
InBlock.gif        
internal static extern IntPtr SendMessageTimeout(
InBlock.gif           IntPtr windowHandle,
InBlock.gif           
uint Msg,
InBlock.gif           IntPtr wParam,
InBlock.gif           IntPtr lParam,
InBlock.gif           SendMessageTimeoutFlags flags,
InBlock.gif           
uint timeout,
InBlock.gif           
out IntPtr result
InBlock.gif           );
InBlock.gif
InBlock.gif        
//EnumPrinters用到的函数和机构体
InBlock.gif
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
InBlock.gif        
private static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level,
InBlock.gif            IntPtr pPrinterEnum, 
uint cbBuf,
InBlock.gif        
ref uint pcbNeeded, ref uint pcReturned);
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
internal struct PRINTER_INFO_2
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public string pServerName;
InBlock.gif            
public string pPrinterName;
InBlock.gif            
public string pShareName;
InBlock.gif            
public string pPortName;
InBlock.gif            
public string pDriverName;
InBlock.gif            
public string pComment;
InBlock.gif            
public string pLocation;
InBlock.gif            
public IntPtr pDevMode;
InBlock.gif            
public string pSepFile;
InBlock.gif            
public string pPrintProcessor;
InBlock.gif            
public string pDatatype;
InBlock.gif            
public string pParameters;
InBlock.gif            
public IntPtr pSecurityDescriptor;
InBlock.gif            
public uint Attributes;
InBlock.gif            
public uint Priority;
InBlock.gif            
public uint DefaultPriority;
InBlock.gif            
public uint StartTime;
InBlock.gif            
public uint UntilTime;
InBlock.gif            
public uint Status;
InBlock.gif            
public uint cJobs;
InBlock.gif            
public uint AveragePPM;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [FlagsAttribute]
InBlock.gif        
internal enum PrinterEnumFlags
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PRINTER_ENUM_DEFAULT 
= 0x00000001,
InBlock.gif            PRINTER_ENUM_LOCAL 
= 0x00000002,
InBlock.gif            PRINTER_ENUM_CONNECTIONS 
= 0x00000004,
InBlock.gif            PRINTER_ENUM_FAVORITE 
= 0x00000004,
InBlock.gif            PRINTER_ENUM_NAME 
= 0x00000008,
InBlock.gif            PRINTER_ENUM_REMOTE 
= 0x00000010,
InBlock.gif            PRINTER_ENUM_SHARED 
= 0x00000020,
InBlock.gif            PRINTER_ENUM_NETWORK 
= 0x00000040,
InBlock.gif            PRINTER_ENUM_EXPAND 
= 0x00004000,
InBlock.gif            PRINTER_ENUM_CONTAINER 
= 0x00008000,
InBlock.gif            PRINTER_ENUM_ICONMASK 
= 0x00ff0000,
InBlock.gif            PRINTER_ENUM_ICON1 
= 0x00010000,
InBlock.gif            PRINTER_ENUM_ICON2 
= 0x00020000,
InBlock.gif            PRINTER_ENUM_ICON3 
= 0x00040000,
InBlock.gif            PRINTER_ENUM_ICON4 
= 0x00080000,
InBlock.gif            PRINTER_ENUM_ICON5 
= 0x00100000,
InBlock.gif            PRINTER_ENUM_ICON6 
= 0x00200000,
InBlock.gif            PRINTER_ENUM_ICON7 
= 0x00400000,
InBlock.gif            PRINTER_ENUM_ICON8 
= 0x00800000,
InBlock.gif            PRINTER_ENUM_HIDE 
= 0x01000000
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//打印机状态
InBlock.gif
        [FlagsAttribute]
InBlock.gif        
internal enum PrinterStatus
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PRINTER_STATUS_BUSY                 
=       0x00000200,
InBlock.gif            PRINTER_STATUS_DOOR_OPEN            
=       0x00400000,
InBlock.gif            PRINTER_STATUS_ERROR                
=       0x00000002,
InBlock.gif            PRINTER_STATUS_INITIALIZING         
=       0x00008000,
InBlock.gif            PRINTER_STATUS_IO_ACTIVE            
=       0x00000100,
InBlock.gif            PRINTER_STATUS_MANUAL_FEED          
=       0x00000020,
InBlock.gif            PRINTER_STATUS_NO_TONER             
=       0x00040000,
InBlock.gif            PRINTER_STATUS_NOT_AVAILABLE        
=       0x00001000,
InBlock.gif            PRINTER_STATUS_OFFLINE              
=       0x00000080,
InBlock.gif            PRINTER_STATUS_OUT_OF_MEMORY        
=       0x00200000,
InBlock.gif            PRINTER_STATUS_OUTPUT_BIN_FULL      
=       0x00000800,
InBlock.gif            PRINTER_STATUS_PAGE_PUNT            
=       0x00080000,
InBlock.gif            PRINTER_STATUS_PAPER_JAM            
=       0x00000008,
InBlock.gif            PRINTER_STATUS_PAPER_OUT            
=       0x00000010,
InBlock.gif            PRINTER_STATUS_PAPER_PROBLEM        
=       0x00000040,
InBlock.gif            PRINTER_STATUS_PAUSED               
=       0x00000001,
InBlock.gif            PRINTER_STATUS_PENDING_DELETION     
=       0x00000004,
InBlock.gif            PRINTER_STATUS_PRINTING             
=       0x00000400,
InBlock.gif            PRINTER_STATUS_PROCESSING           
=       0x00004000,
InBlock.gif            PRINTER_STATUS_TONER_LOW            
=       0x00020000,
InBlock.gif            PRINTER_STATUS_USER_INTERVENTION    
=       0x00100000,
InBlock.gif            PRINTER_STATUS_WAITING              
=       0x20000000,
InBlock.gif            PRINTER_STATUS_WARMING_UP           
=       0x00010000
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
//GetDefaultPrinter用到的API函数说明
InBlock.gif
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
InBlock.gif        
internal static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);
InBlock.gif
InBlock.gif        
//SetDefaultPrinter用到的API函数声明
InBlock.gif
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
InBlock.gif        
internal static extern bool SetDefaultPrinter(string Name);
InBlock.gif
InBlock.gif        
//EnumFormsA用到的函数声明,应该和EnumPrinters类似
InBlock.gif
        [DllImport("winspool.drv", EntryPoint = "EnumForms")]
InBlock.gif        
internal static extern int EnumFormsA(IntPtr hPrinter, int Level, ref byte pForm, int cbBuf, ref int pcbNeeded, ref int pcReturned);  
InBlock.gif
InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
InBlock.gif        
internal static int GetPrinterStatusInt(string PrinterName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int intRet = 0;
InBlock.gif            IntPtr hPrinter;
InBlock.gif            structPrinterDefaults defaults 
= new structPrinterDefaults();
InBlock.gif
InBlock.gif            
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int cbNeeded = 0;
InBlock.gif                
bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0out cbNeeded);
InBlock.gif                
if (cbNeeded > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IntPtr pAddr 
= Marshal.AllocHGlobal((int)cbNeeded);
InBlock.gif                    bolRet 
= GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
InBlock.gif                    
if (bolRet)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        PRINTER_INFO_2 Info2 
= new PRINTER_INFO_2();
InBlock.gif                        
InBlock.gif                        Info2 
= (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
InBlock.gif                        
InBlock.gif                        intRet 
= System.Convert.ToInt32(Info2.Status);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    Marshal.FreeHGlobal(pAddr);
ExpandedSubBlockEnd.gif                }

InBlock.gif                ClosePrinter(hPrinter);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return intRet;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
uint cbNeeded = 0;
InBlock.gif            
uint cReturned = 0;
InBlock.gif            
bool ret = EnumPrinters( PrinterEnumFlags.PRINTER_ENUM_LOCAL, null2, IntPtr.Zero, 0ref cbNeeded, ref cReturned);
InBlock.gif
InBlock.gif            IntPtr pAddr 
= Marshal.AllocHGlobal((int)cbNeeded);
InBlock.gif            ret 
= EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);
InBlock.gif
InBlock.gif            
if (ret)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PRINTER_INFO_2[] Info2 
= new PRINTER_INFO_2[cReturned];
InBlock.gif
InBlock.gif                
int offset = pAddr.ToInt32();
InBlock.gif
InBlock.gif                
for (int i = 0; i < cReturned; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Info2[i].pServerName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pPrinterName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pShareName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pPortName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pDriverName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pComment 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pLocation 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pDevMode 
= Marshal.ReadIntPtr(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pSepFile 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pPrintProcessor 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pDatatype 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pParameters 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].pSecurityDescriptor 
= Marshal.ReadIntPtr(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].Attributes 
= (uint )Marshal.ReadIntPtr(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].Priority 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].DefaultPriority 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].StartTime 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].UntilTime 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].Status 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].cJobs 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif                    Info2[i].AveragePPM 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
InBlock.gif                    offset 
+= 4;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Marshal.FreeHGlobal(pAddr);
InBlock.gif
InBlock.gif                
return Info2;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new PRINTER_INFO_2[0];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取当前指定打印机的状态
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="PrinterName">打印机名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>打印机状态描述</returns>

InBlock.gif        public static string GetPrinterStatus(string PrinterName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int intValue = GetPrinterStatusInt(PrinterName);
InBlock.gif            
string strRet = string.Empty;
InBlock.gif            
switch (intValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case 0:
InBlock.gif                    strRet 
= "准备就绪(Ready)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000200:
InBlock.gif                    strRet 
= "忙(Busy)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00400000:
InBlock.gif                    strRet 
= "门被打开(Printer Door Open)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000002:
InBlock.gif                    strRet 
= "错误(Printer Error)";
InBlock.gif                    
break;
InBlock.gif                
case 0x0008000:
InBlock.gif                    strRet 
= "正在初始化(Initializing)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000100:
InBlock.gif                    strRet 
= "正在输入或输出(I/O Active)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000020:
InBlock.gif                    strRet 
= "手工送纸(Manual Feed)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00040000:
InBlock.gif                    strRet 
= "无墨粉(No Toner)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00001000:
InBlock.gif                    strRet 
= "不可用(Not Available)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000080:
InBlock.gif                    strRet 
= "脱机(Off Line)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00200000:
InBlock.gif                    strRet 
= "内存溢出(Out of Memory)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000800:
InBlock.gif                    strRet 
= "输出口已满(Output Bin Full)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00080000:
InBlock.gif                    strRet 
= "当前页无法打印(Page Punt)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000008:
InBlock.gif                    strRet 
= "塞纸(Paper Jam)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000010:
InBlock.gif                    strRet 
= "打印纸用完(Paper Out)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000040:
InBlock.gif                    strRet 
= "纸张问题(Page Problem)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000001:
InBlock.gif                    strRet 
= "暂停(Paused)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000004:
InBlock.gif                    strRet 
= "正在删除(Pending Deletion)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00000400:
InBlock.gif                    strRet 
= "正在打印(Printing)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00004000:
InBlock.gif                    strRet 
= "正在处理(Processing)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00020000:
InBlock.gif                    strRet 
= "墨粉不足(Toner Low)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00100000:
InBlock.gif                    strRet 
= "需要用户干预(User Intervention)";
InBlock.gif                    
break;
InBlock.gif                
case 0x20000000:
InBlock.gif                    strRet 
= "等待(Waiting)";
InBlock.gif                    
break;
InBlock.gif                
case 0x00010000:
InBlock.gif                    strRet 
= "正在准备(Warming Up)";
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    strRet 
= "未知状态(Unknown Status)";
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strRet;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除已经存在的自定义纸张
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="PrinterName">打印机名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="PaperName">纸张名称</param>

InBlock.gif        public static void DeleteCustomPaperSize(string PrinterName, string PaperName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
const int PRINTER_ACCESS_USE = 0x00000008;
InBlock.gif            
const int PRINTER_ACCESS_ADMINISTER = 0x00000004;
InBlock.gif
InBlock.gif            structPrinterDefaults defaults 
= new structPrinterDefaults();
InBlock.gif            defaults.pDatatype 
= null;
InBlock.gif            defaults.pDevMode 
= IntPtr.Zero;
InBlock.gif            defaults.DesiredAccess 
= PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;
InBlock.gif
InBlock.gif            IntPtr hPrinter 
= IntPtr.Zero;
InBlock.gif
InBlock.gif            
//打开打印机
InBlock.gif
            if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DeleteForm(hPrinter, PaperName);
InBlock.gif                    ClosePrinter(hPrinter);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Pub.WinForm.Msg.Warning(
"删除自定义纸张时发生错误!");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 指定的打印机设置以mm为单位的自定义纸张(Form)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="PrinterName">打印机名称</param>
InBlock.gif        
/// <param name="PaperName">Form名称</param>
InBlock.gif        
/// <param name="WidthInMm">以mm为单位的宽度</param>
ExpandedSubBlockEnd.gif        
/// <param name="HeightInMm">以mm为单位的高度</param>

InBlock.gif        public static void AddCustomPaperSize(string PrinterName, string PaperName, float WidthInMm, float HeightInMm)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (PlatformID.Win32NT == Environment.OSVersion.Platform)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
const int PRINTER_ACCESS_USE = 0x00000008;
InBlock.gif                
const int PRINTER_ACCESS_ADMINISTER = 0x00000004;
InBlock.gif                
const int FORM_PRINTER = 0x00000002;
InBlock.gif
InBlock.gif                structPrinterDefaults defaults 
= new structPrinterDefaults();
InBlock.gif                defaults.pDatatype 
= null;
InBlock.gif                defaults.pDevMode 
= IntPtr.Zero;
InBlock.gif                defaults.DesiredAccess 
= PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;
InBlock.gif
InBlock.gif                IntPtr hPrinter 
= IntPtr.Zero;
InBlock.gif
InBlock.gif                
//打开打印机
InBlock.gif
                if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//如果Form存在删除之
InBlock.gif
                        DeleteForm(hPrinter, PaperName);
InBlock.gif                        
//创建并初始化FORM_INFO_1
InBlock.gif
                        FormInfo1 formInfo = new FormInfo1();
InBlock.gif                        formInfo.Flags 
= 0;
InBlock.gif                        formInfo.pName 
= PaperName;
InBlock.gif                        formInfo.Size.width 
= (int)(WidthInMm * 1000.0);
InBlock.gif                        formInfo.Size.height 
= (int)(HeightInMm * 1000.0);
InBlock.gif                        formInfo.ImageableArea.left 
= 0;
InBlock.gif                        formInfo.ImageableArea.right 
= formInfo.Size.width;
InBlock.gif                        formInfo.ImageableArea.top 
= 0;
InBlock.gif                        formInfo.ImageableArea.bottom 
= formInfo.Size.height;
InBlock.gif                        
if (!AddForm(hPrinter, 1ref formInfo))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            StringBuilder strBuilder 
= new StringBuilder();
InBlock.gif                            strBuilder.AppendFormat(
"向打印机 {1} 添加自定义纸张 {0} 失败!错误代号:{2}",
InBlock.gif                                PaperName, PrinterName, GetLastError());
InBlock.gif                            
throw new ApplicationException(strBuilder.ToString());
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
//初始化
InBlock.gif
                        const int DM_OUT_BUFFER = 2;
InBlock.gif                        
const int DM_IN_BUFFER = 8;
InBlock.gif                        structDevMode devMode 
= new structDevMode();
InBlock.gif                        IntPtr hPrinterInfo, hDummy;
InBlock.gif                        PRINTER_INFO_9 printerInfo;
InBlock.gif                        printerInfo.pDevMode 
= IntPtr.Zero;
InBlock.gif                        
int iPrinterInfoSize, iDummyInt;
InBlock.gif
InBlock.gif
InBlock.gif                        
int iDevModeSize = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, IntPtr.Zero, IntPtr.Zero, 0);
InBlock.gif
InBlock.gif                        
if (iDevModeSize < 0)
InBlock.gif                            
throw new ApplicationException("无法取得DEVMODE结构的大小!");
InBlock.gif
InBlock.gif                        
//分配缓冲
InBlock.gif
                        IntPtr hDevMode = Marshal.AllocCoTaskMem(iDevModeSize + 100);
InBlock.gif
InBlock.gif                        
//获取DEV_MODE指针
InBlock.gif
                        int iRet = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, hDevMode, IntPtr.Zero, DM_OUT_BUFFER);
InBlock.gif
InBlock.gif                        
if (iRet < 0)
InBlock.gif                            
throw new ApplicationException("无法获得DEVMODE结构!");
InBlock.gif
InBlock.gif                        
//填充DEV_MODE
InBlock.gif
                        devMode = (structDevMode)Marshal.PtrToStructure(hDevMode, devMode.GetType());
InBlock.gif
InBlock.gif
InBlock.gif                        devMode.dmFields 
= 0x10000;
InBlock.gif
InBlock.gif                        
//FORM名称
InBlock.gif
                        devMode.dmFormName = PaperName;
InBlock.gif
InBlock.gif                        Marshal.StructureToPtr(devMode, hDevMode, 
true);
InBlock.gif
InBlock.gif                        iRet 
= DocumentProperties(IntPtr.Zero, hPrinter, PrinterName,
InBlock.gif                                 printerInfo.pDevMode, printerInfo.pDevMode, DM_IN_BUFFER 
| DM_OUT_BUFFER);
InBlock.gif
InBlock.gif                        
if (iRet < 0)
InBlock.gif                            
throw new ApplicationException("无法为打印机设定打印方向!");
InBlock.gif
InBlock.gif                        GetPrinter(hPrinter, 
9, IntPtr.Zero, 0out iPrinterInfoSize);
InBlock.gif                        
if (iPrinterInfoSize == 0)
InBlock.gif                            
throw new ApplicationException("调用GetPrinter方法失败!");
InBlock.gif
InBlock.gif                        hPrinterInfo 
= Marshal.AllocCoTaskMem(iPrinterInfoSize + 100);
InBlock.gif
InBlock.gif                        
bool bSuccess = GetPrinter(hPrinter, 9, hPrinterInfo, iPrinterInfoSize, out iDummyInt);
InBlock.gif
InBlock.gif                        
if (!bSuccess)
InBlock.gif                            
throw new ApplicationException("调用GetPrinter方法失败!");
InBlock.gif
InBlock.gif                        printerInfo 
= (PRINTER_INFO_9)Marshal.PtrToStructure(hPrinterInfo, printerInfo.GetType());
InBlock.gif                        printerInfo.pDevMode 
= hDevMode;
InBlock.gif
InBlock.gif                        Marshal.StructureToPtr(printerInfo, hPrinterInfo, 
true);
InBlock.gif
InBlock.gif                        bSuccess 
= SetPrinter(hPrinter, 9, hPrinterInfo, 0);
InBlock.gif
InBlock.gif                        
if (!bSuccess)
InBlock.gif                            
throw new Win32Exception(Marshal.GetLastWin32Error(), "调用SetPrinter方法失败,无法进行打印机设置!");
InBlock.gif
InBlock.gif                        SendMessageTimeout(
InBlock.gif                           
new IntPtr(HWND_BROADCAST),
InBlock.gif                           WM_SETTINGCHANGE,
InBlock.gif                           IntPtr.Zero,
InBlock.gif                           IntPtr.Zero,
InBlock.gif                           Printer.SendMessageTimeoutFlags.SMTO_NORMAL,
InBlock.gif                           
1000,
InBlock.gif                           
out hDummy);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ClosePrinter(hPrinter);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    StringBuilder strBuilder 
= new StringBuilder();
InBlock.gif                    strBuilder.AppendFormat(
"无法打开打印机{0}, 错误代号: {1}",
InBlock.gif                        PrinterName, GetLastError());
InBlock.gif                    
throw new ApplicationException(strBuilder.ToString());
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                structDevMode pDevMode 
= new structDevMode();
InBlock.gif                IntPtr hDC 
= CreateDC(null, PrinterName, nullref pDevMode);
InBlock.gif                
if (hDC != IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
const long DM_PAPERSIZE = 0x00000002L;
InBlock.gif                    
const long DM_PAPERLENGTH = 0x00000004L;
InBlock.gif                    
const long DM_PAPERWIDTH = 0x00000008L;
InBlock.gif                    pDevMode.dmFields 
= (int)(DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH);
InBlock.gif                    pDevMode.dmPaperSize 
= 256;
InBlock.gif                    pDevMode.dmPaperWidth 
= (short)(WidthInMm * 1000.0);
InBlock.gif                    pDevMode.dmPaperLength 
= (short)(HeightInMm * 1000.0);
InBlock.gif                    ResetDC(hDC, 
ref pDevMode);
InBlock.gif                    DeleteDC(hDC);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取本地打印机列表
InBlock.gif        
/// 可以通过制定参数获取网络打印机
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>打印机列表</returns>

InBlock.gif        public static System.Collections.ArrayList GetPrinterList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Collections.ArrayList alRet 
= new System.Collections.ArrayList();
InBlock.gif            PRINTER_INFO_2[] Info2 
= EnumPrintersByFlag(PrinterEnumFlags.PRINTER_ENUM_LOCAL);
InBlock.gif            
for (int i = 0; i < Info2.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                alRet.Add(Info2[i].pPrinterName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return alRet;
ExpandedSubBlockEnd.gif        }

InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取本机的默认打印机名称
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>默认打印机名称</returns>

InBlock.gif        public static string GetDeaultPrinterName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder dp 
= new StringBuilder(256);
InBlock.gif            
int size = dp.Capacity;
InBlock.gif            
if (GetDefaultPrinter(dp, ref size))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return dp.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rc = GetLastError();
InBlock.gif                Pub.WinForm.Msg.Warning(
"获取默认打印机失败!错误代号:" + rc.ToString());
InBlock.gif                
return string.Empty;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置默认打印机
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="PrinterName">可用的打印机名称</param>

InBlock.gif        public static void SetPrinterToDefault(string PrinterName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SetDefaultPrinter(PrinterName);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////// <summary>
InBlock.gif        
///// 判断打印机是否在系统可用的打印机列表中
InBlock.gif        
///// </summary>
InBlock.gif        
///// <param name="PrinterName">打印机名称</param>
ExpandedSubBlockEnd.gif        
///// <returns>是:在;否:不在</returns>

InBlock.gif        public static bool PrinterInList(string PrinterName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bolRet = false;
InBlock.gif
InBlock.gif            System.Collections.ArrayList alPrinters 
= GetPrinterList();
InBlock.gif
InBlock.gif            
for (int i = 0; i < alPrinters.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (PrinterName == alPrinters[i].ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bolRet 
= true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            alPrinters.Clear();
InBlock.gif            alPrinters 
= null;
InBlock.gif
InBlock.gif            
return bolRet;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////// <summary>
InBlock.gif        
///// 判断表单是否在指定的打印机所支持的纸张列表中
InBlock.gif        
///// </summary>
InBlock.gif        
///// <param name="PrinterName">打印机名称</param>
InBlock.gif        
///// <param name="PaperName">纸张名称</param>
ExpandedSubBlockEnd.gif        
///// <returns>是:在;否:不在</returns>

InBlock.gif        public static bool FormInPrinter(string PrinterName, string PaperName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bolRet = false;
InBlock.gif
InBlock.gif            System.Drawing.Printing.PrintDocument pd 
= new System.Drawing.Printing.PrintDocument();
InBlock.gif
InBlock.gif            pd.PrinterSettings.PrinterName 
= PrinterName;
InBlock.gif
InBlock.gif            
foreach (System.Drawing.Printing.PaperSize ps in pd.PrinterSettings.PaperSizes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ps.PaperName == PaperName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bolRet 
= true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            pd.Dispose();
InBlock.gif
InBlock.gif            
return bolRet;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断指定纸张的宽度和高度和在文本框中指定的宽度和高度是否匹配
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="PrinterName">打印机名称</param>
InBlock.gif        
/// <param name="FormName">表单名称</param>
InBlock.gif        
/// <param name="Width">宽度</param>
InBlock.gif        
/// <param name="Height">高度</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool FormSameSize(string PrinterName, string FormName, decimal Width, decimal Height)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bolRet = false;
InBlock.gif
InBlock.gif            System.Drawing.Printing.PrintDocument pd 
= new System.Drawing.Printing.PrintDocument();
InBlock.gif
InBlock.gif            pd.PrinterSettings.PrinterName 
= PrinterName;
InBlock.gif
InBlock.gif            
foreach (System.Drawing.Printing.PaperSize ps in pd.PrinterSettings.PaperSizes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ps.PaperName == FormName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
decimal decWidth = FromInchToCM(System.Convert.ToDecimal(ps.Width));
InBlock.gif                    
decimal decHeight = FromInchToCM(System.Convert.ToDecimal(ps.Height));
InBlock.gif                    
//只要整数位相同即认为是同一纸张,毕竟inch到cm的转换并不能整除
InBlock.gif
                    if (Pub.MathEx.Round(decWidth, 0== Pub.MathEx.Round(Width, 0&& Pub.MathEx.Round(decHeight, 0== Pub.MathEx.Round(Height, 0))
InBlock.gif                        bolRet 
= true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            pd.Dispose();
InBlock.gif
InBlock.gif            
return bolRet;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 英尺到厘米的转换
InBlock.gif        
/// 米国人用的是英尺,中国人用的是厘米
InBlock.gif        
/// 1 inch = 2.5400 cm
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="inch">英尺数</param>
ExpandedSubBlockEnd.gif        
/// <returns>厘米数,两位小数</returns>

InBlock.gif        public static decimal FromInchToCM(decimal inch)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Math.Round((System.Convert.ToDecimal((inch / 100)) * System.Convert.ToDecimal(2.5400)), 2);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

        页面设置窗体由报表浏览器窗体ReportViewer调用,使用以下结构的XML文件存储针对每个报表的页面设置:

None.gif <? xml version="1.0" standalone="yes" ?>  
None.gif
< ReportSettings >  
None.gif    
< 采购订单 >  
None.gif        
< ReportName > 采购订单 </ ReportName >  
None.gif        
< PrinterName > EPSON LQ-1600KIII </ PrinterName >  
None.gif        
< PaperName > Test </ PaperName >  
None.gif        
< PageWidth > 8.00 </ PageWidth >  
None.gif        
< PageHeight > 8.00 </ PageHeight >  
None.gif        
< MarginTop > 0.2 </ MarginTop >  
None.gif        
< MarginBottom > 0.2 </ MarginBottom >  
None.gif        
< MarginLeft > 0.2 </ MarginLeft >  
None.gif        
< MarginRight > 0.2 </ MarginRight >  
None.gif        
< Orientation > 横向 </ Orientation >  
None.gif    
</ 采购订单 >  
None.gif
</ ReportSettings >

        当然,这种格式的XML配置文件是用DataSet来读写更新的,因为这种方法比较简单。

        页面设置窗体上有一个关于打印方向的设置,在(六)中有一个问题忘记阐述了,就是关于打印方向的。因为DeviceInfo结构中并没有打印方向的设置,所以在生成DeviceInfo结构的字符串时,应该根据打印方向来设置DeviceInfo结构的页高和页宽,见(五)中给出的EMFDeviceInfo类的属性DeviceInfoString。放在这里补充说一下。

        完整的页面设置窗体的代码下载
        
        相关随笔:
            RDLC报表(一) 
            RDLC报表(二) 
            RDLC报表(三) 
            RDLC报表(四) 
            RDLC报表(五) 
            RDLC报表(六)  

        14.gif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值