1、using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Diagnostics;using System.Drawing.Printing;namespace testprinter2public par
2、tial class Form1 : Form #region 预定义类型FlagsAttributepublic enum PrinterEnumFlagsPRINTER_ENUM_DEFAULT = 0x00000001,PRINTER_ENUM_LOCAL = 0x00000002,PRINTER_ENUM_CONNECTIONS = 0x00000004,PRINTER_ENUM_FAVORITE = 0x00000004,PRINTER_ENUM_NAME = 0x00000008,PRINTER_ENUM_REMOTE = 0x00000010,PRINTER_ENUM_SHARE
3、D = 0x00000020,PRINTER_ENUM_NETWORK = 0x00000040,PRINTER_ENUM_EXPAND = 0x00004000,PRINTER_ENUM_CONTAINER = 0x00008000,PRINTER_ENUM_ICONMASK = 0x00ff0000,PRINTER_ENUM_ICON1 = 0x00010000,PRINTER_ENUM_ICON2 = 0x00020000,PRINTER_ENUM_ICON3 = 0x00040000,PRINTER_ENUM_ICON4 = 0x00080000,PRINTER_ENUM_ICON5
4、= 0x00100000,PRINTER_ENUM_ICON6 = 0x00200000,PRINTER_ENUM_ICON7 = 0x00400000,PRINTER_ENUM_ICON8 = 0x00800000,PRINTER_ENUM_HIDE = 0x01000000StructLayout(LayoutKind.Sequential)public struct PRINTER_INFO_2MarshalAs(UnmanagedType.LPTStr)public string pServerName;MarshalAs(UnmanagedType.LPTStr)public str
5、ing pPrinterName;MarshalAs(UnmanagedType.LPTStr)public string pShareName;MarshalAs(UnmanagedType.LPTStr)public string pPortName;MarshalAs(UnmanagedType.LPTStr)public string pDriverName;MarshalAs(UnmanagedType.LPTStr)public string pComment;MarshalAs(UnmanagedType.LPTStr)public string pLocation;public
6、 IntPtr pDevMode;MarshalAs(UnmanagedType.LPTStr)public string pSepFile;MarshalAs(UnmanagedType.LPTStr)public string pPrintProcessor;MarshalAs(UnmanagedType.LPTStr)public string pDatatype;MarshalAs(UnmanagedType.LPTStr)public string pParameters;public IntPtr pSecurityDescriptor;public uint Attributes
7、;public uint Priority;public uint DefaultPriority;public uint StartTime;public uint UntilTime;public uint Status;public uint cJobs;public uint AveragePPM;#endregion#region 引用 WindowsAPI/引用API声明DllImport(“winspool.drv“, CharSet = CharSet.Auto, SetLastError = true)return: MarshalAs(UnmanagedType.Bool)
8、public static extern bool EnumPrinters(PrinterEnumFlags Flags,string Name,uint Level,IntPtr pPrinterEnum,uint cbBuf,ref uint pcbNeeded,ref uint pcReturned);#endregion/ / 遍历打印机/ / / DllImport(“User32.dll“)public static extern int MessageBox(int h, string m, string c, int type);public Form1()Initializ
9、eComponent();private void button1_Click(object sender, EventArgs e)PRINTER_INFO_2 Info2 = null;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
10、 = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);if (ret)Info2 = new PRINTER_INFO_2cReturned;int offset = pAddr.ToInt32();for (int i = 0; i 2: 3: T a;4: 如果对 Marshal.SizeOf 传入 MyStruct或者 MyStruct这样的类型,则抛出ArgumentException,因为 Marshal.SizeOf 完全
11、不支持泛型。这个是历史遗留问题,从本质上来讲实例化的模板类型(MyStruct )应该是支持的,据说当时主要是没有时间加上对模板的支持。同样的,sizeof 也不支持模板类型,而且连 MyStruct这样子的类型也不支持。C#编译器会对 sizeof(MyStruct)报错:error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type (SizeOf.Program.MyStruct)#region 和#endregion 是什么? 2012-11-10 16:
12、30:57| 分类: 默认分类 | 标签: |字号大中小 订阅 用来注释中间代码的作用 而且在其他地方用到中间的类和方法 都会有你标注的注释 本身不参与编译 还可以缩进代码 方便阅览折叠代码#region 使您得以在使用 Visual Studio 代码编辑器的大纲显示功能时指定可展开或折叠的代码块。 #region name 其中: name 希望给予将出现在 Visual Studio 代码编辑器中的区域的名称。 备注 必须用 #endregion 指令终止 #region 块。 #region 块不能与 #if 块重叠。但是,可以将 #region 块嵌套在 #if 块内,并且可以将 #if 块嵌套在 #region 块内。 示例 / preprocessor_region.cs #region MyClass definition public class MyClass public static void Main()