C#中通过API实现的打印类 实例代码
using system;
using system.collections;
using system.text;
using system.runtime.interopservices;
using system.security;
using system.componentmodel;
using system.drawing.printing;
namespace printerapi
{
public class printer
{
private printer()
{
}
///泥人张版本加强版
#region api声明
[structlayout(layoutkind.sequential, charset = charset.auto)]
internal struct structprinterdefaults
{
[marshalas(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 structsize
{
public int32 width;
public int32 height;
}
[structlayout(layoutkind.sequential, charset = charset.auto)]
internal struct structrect
{
public int32 left;
public int32 top;
public int32 right;
public int32 bottom;
}
[structlayout(layoutkind.explicit, charset = charset.unicode)]
internal struct forminfo1
{
[fieldoffset(0), marshalas(unmanagedtype.i4)]
public uint flags;
[fieldoffset(4), marshalas(unmanagedtype.lpwstr)]
public string pname;
[fieldoffset(8)]
public structsize size;
[fieldoffset(16)]
public structrect imageablearea;
};
[structlayout(layoutkind.sequential, charset = charset.ansi)]
internal struct structdevmode
{
[marshalas(unmanagedtype.byvaltstr, sizeconst = 32)]
public string
dmdevicename;
[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;
}
[structlayout(layoutkind.sequential, charset = charset.auto)]
internal struct printer_info_9
{
public intptr pdevmode;
}
[dllimport("winspool.drv", entrypoint = "addformw", setlasterror = true,
charset = charset.unicode, exactspelling = true,
callingconvention = callingconvention.stdcall), suppressunmanagedcodesecurityattribute()]
internal static extern bool addform(
intptr phprinter,
[marshalas(unmanagedtype.i4)] int level,
ref forminfo1 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("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 structdevmode
pdevmode);
[dllimport("gdi32.dll", entrypoint = "deletedc", setlasterror = true,
charset = charset.unicode, exactspelling = false,
callingconvention = callingconvention.stdcall),
suppressunmanagedcodesecurityattribute()]
internal static extern bool deletedc(intptr hdc);
[dllimport("winspool.drv", entrypoint = "setprintera", setlasterror = true,
charset = charset.auto, exactspelling = true,
callingconvention = callingconvention.stdcall), suppressunmanagedcodesecurityattribute()]
internal static extern bool setprinter(
intptr hprinter,
[marshalas(unmanagedtype.i4)] int level,
intptr pprinter,
[marshalas(unmanagedtype.i4)] int command);
[dllimport("winspool.drv", entrypoint = "documentpropertiesa", setlasterror = true,
exactspelling = true, callingconvention = callingconvention.stdcall)]
internal static extern int documentproperties(
intptr hwnd,
intptr hprinter,
[marshalas(unmanagedtype.lpstr)] string pdevicename,
intptr pdevmodeoutput,
intptr pdevmodeinput,
int fmode
);
[dllimport("winspool.drv", entrypoint = "getprintera", setlasterror = true,
exactspelling = true, callingconvention = callingconvention.stdcall)]
internal static extern bool getprinter(
intptr hprinter,
int dwlevel,
intptr pprinter,
int dwbuf,
out int dwneeded
);
[flags]
internal enum sendmessagetimeoutflags : uint
{
smto_normal = 0x0000,
smto_block = 0x0001,
smto_abortifhung = 0x0002,
smto_notimeoutifnothung = 0x0008
}
const int wm_settingchange = 0x001a;
const int hwnd_broadcast = 0xffff;
[dllimport("user32.dll", setlasterror = true, charset = charset.auto)]
internal static extern intptr sendmessagetimeout(
intptr windowhandle,
uint msg,
intptr wparam,
intptr lparam,
sendmessagetimeoutflags flags,
uint timeout,
out intptr result
);
//enumprinters用到的函数和结构体
[dllimport("winspool.drv", charset = charset.auto, setlasterror = true)]
private static extern bool enumprinters(printerenumflags flags, string name, uint level,
intptr pprinterenum, uint cbbuf,
ref uint pcbneeded, ref uint pcreturned);
[structlayout(layoutkind.sequential)]
internal struct printer_info_2
{
public string pservername;
public string pprintername;
public string psharename;
public string pportname;
public string pdrivername;
public string pcomment;
public string plocation;
public intptr pdevmode;
public string psepfile;
public string pprintprocessor;
public string pdatatype;
public string pparameters;
public intptr psecuritydescriptor;
public uint attributes;
public uint priority;
public uint defaultpriority;
public uint starttime;
public uint untiltime;
public uint status;
public uint cjobs;
public uint averageppm;
}
[flagsattribute]
internal enum printerenumflags
{
printer_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_shared = 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 = 0x00100000,
printer_enum_icon6 = 0x00200000,
printer_enum_icon7 = 0x00400000,
printer_enum_icon8 = 0x00800000,
printer_enum_hide = 0x01000000
}
//打印机状态
[flagsattribute]
internal enum printerstatus
{
printer_status_busy = 0x00000200,
printer_status_door_open = 0x00400000,
printer_status_error = 0x00000002,
printer_status_initializing = 0x00008000,
printer_status_io_active = 0x00000100,
printer_status_manual_feed = 0x00000020,
printer_status_no_toner = 0x00040000,
printer_status_not_available = 0x00001000,
printer_status_offline = 0x00000080,
printer_status_out_of_memory = 0x00200000,
printer_status_output_bin_full = 0x00000800,
printer_status_page_punt = 0x00080000,
printer_status_paper_jam = 0x00000008,
printer_status_paper_out = 0x00000010,
printer_status_paper_problem = 0x00000040,
printer_status_paused = 0x00000001,
printer_status_pending_deletion = 0x00000004,
printer_status_printing = 0x00000400,
printer_status_processing = 0x00004000,
printer_status_toner_low = 0x00020000,
printer_status_user_intervention = 0x00100000,
printer_status_waiting = 0x20000000,
printer_status_warming_up = 0x00010000
}
//getdefaultprinter用到的api函数说明
[dllimport("winspool.drv", charset = charset.auto, setlasterror = true)]
internal static extern bool getdefaultprinter(stringbuilder pszbuffer, ref int size);
//setdefaultprinter用到的api函数声明
[dllimport("winspool.drv", charset = charset.auto, setlasterror = true)]
internal static extern bool setdefaultprinter(string name);
//enumformsa用到的函数声明,应该和enumprinters类似
[dllimport("winspool.drv", entrypoint = "enumforms")]
internal static extern int enumformsa(intptr hprinter, int level, ref byte pform, int cbbuf, ref int pcbneeded, ref int pcreturned);
#endregion 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];
}
}
#region 获取当前指定打印机的状态
/// </summary>
/// 获取当前指定打印机的状态
/// </summary>
/// <param name="printername">打印机名称</param>
/// <returns>打印机状态描述</returns>
public static string getprinterstatus(string printername)
{
int intvalue = getprinterstatusint(printername);
string strret = string.empty;
switch (intvalue)
{
case 0:
strret = "准备就绪(ready)";
break;
case 0x00000200:
strret = "忙(busy)";
break;
case 0x00400000:
strret = "门被打开(printer door open)";
break;
case 0x00000002:
strret = "错误(printer error)";
break;
case 0x0008000:
strret = "正在初始化(initializing)";
break;
case 0x00000100:
strret = "正在输入或输出(i/o active)";
break;
case 0x00000020:
strret = "手工送纸(manual feed)";
break;
case 0x00040000:
strret = "无墨粉(no toner)";
break;
case 0x00001000:
strret = "不可用(not available)";
break;
case 0x00000080:
strret = "脱机(off line)";
break;
case 0x00200000:
strret = "内存溢出(out of memory)";
break;
case 0x00000800:
strret = "输出口已满(output bin full)";
break;
case 0x00080000:
strret = "当前页无法打印(page punt)";
break;
case 0x00000008:
strret = "塞纸(paper jam)";
break;
case 0x00000010:
strret = "打印纸用完(paper out)";
break;
case 0x00000040:
strret = "纸张问题(page problem)";
break;
case 0x00000001:
strret = "暂停(paused)";
break;
case 0x00000004:
strret = "正在删除(pending deletion)";
break;
case 0x00000400:
strret = "正在打印(printing)";
break;
case 0x00004000:
strret = "正在处理(processing)";
break;
case 0x00020000:
strret = "墨粉不足(toner low)";
break;
case 0x00100000:
strret = "需要用户干预(user intervention)";
break;
case 0x20000000:
strret = "等待(waiting)";
break;
case 0x00010000:
strret = "正在准备(warming up)";
break;
default:
strret = "未知状态(unknown status)";
break;
}
return strret;
}
#endregion 获取当前指定打印机的状态
#region 删除已经存在的自定义纸张
/**/
/// <summary>
/// 删除已经存在的自定义纸张
/// </summary>
/// <param name="printername">打印机名称</param>
/// <param name="papername">纸张名称</param>
public static void deletecustompapersize(string printername, string papername)
{
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))
{
try
{
deleteform(hprinter, papername);
closeprinter(hprinter);
}
catch
{
}
}
}
#endregion 删除已经存在的自定义纸张
#region 指定的打印机设置以mm为单位的自定义纸张(form)
/**/
/// <summary>
/// 指定的打印机设置以mm为单位的自定义纸张(form)
/// </summary>
/// <param name="printername">打印机名称</param>
/// <param name="papername">form名称</param>
/// <param name="widthinmm">以mm为单位的宽度</param>
/// <param name="heightinmm">以mm为单位的高度</param>
public static void addcustompapersize(string printername, string papername, float widthinmm, float heightinmm)
{
if (platformid.win32nt == environment.osversion.platform)
{
const int printer_access_use = 0x00000008;
const int printer_access_administer = 0x00000004;
const int form_printer = 0x00000002;
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))
{
try
{
//如果form存在删除之
deleteform(hprinter, papername);
//创建并初始化form_info_1
forminfo1 forminfo = new forminfo1();
forminfo.flags = 0;
forminfo.pname = papername;
forminfo.size.width = (int)(widthinmm * 1000.0);
forminfo.size.height = (int)(heightinmm * 1000.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, ref forminfo))
{
stringbuilder strbuilder = new stringbuilder();
strbuilder.appendformat("向打印机 {1} 添加自定义纸张 {0} 失败!错误代号:{2}",
papername, printername, getlasterror());
throw new applicationexception(strbuilder.tostring());
}
//初始化
const int dm_out_buffer = 2;
const int dm_in_buffer = 8;
structdevmode devmode = new structdevmode();
intptr hprinterinfo, hdummy;
printer_info_9 printerinfo;
printerinfo.pdevmode = intptr.zero;
int iprinterinfosize, idummyint;
int idevmodesize = documentproperties(intptr.zero, hprinter, printername, intptr.zero, intptr.zero, 0);
if (idevmodesize < 0)
throw new applicationexception("无法取得devmode结构的大小!");
//分配缓冲
intptr hdevmode = marshal.alloccotaskmem(idevmodesize + 100);
//获取dev_mode指针
int iret = documentproperties(intptr.zero, hprinter, printername, hdevmode, intptr.zero, dm_out_buffer);
if (iret < 0)
throw new applicationexception("无法获得devmode结构!");
//填充dev_mode
devmode = (structdevmode)marshal.ptrtostructure(hdevmode, devmode.gettype());
devmode.dmfields = 0x10000;
//form名称
devmode.dmformname = papername;
marshal.structuretoptr(devmode, hdevmode, true);
iret = documentproperties(intptr.zero, hprinter, printername,
printerinfo.pdevmode, printerinfo.pdevmode, dm_in_buffer | dm_out_buffer);
if (iret < 0)
throw new applicationexception("无法为打印机设定打印方向!");
getprinter(hprinter, 9, intptr.zero, 0, out iprinterinfosize);
if (iprinterinfosize == 0)
throw new applicationexception("调用getprinter方法失败!");
hprinterinfo = marshal.alloccotaskmem(iprinterinfosize + 100);
bool bsuccess = getprinter(hprinter, 9, hprinterinfo, iprinterinfosize, out idummyint);
if (!bsuccess)
throw new applicationexception("调用getprinter方法失败!");
printerinfo = (printer_info_9)marshal.ptrtostructure(hprinterinfo, printerinfo.gettype());
printerinfo.pdevmode = hdevmode;
marshal.structuretoptr(printerinfo, hprinterinfo, true);
bsuccess = setprinter(hprinter, 9, hprinterinfo, 0);
if (!bsuccess)
throw new win32exception(marshal.getlastwin32error(), "调用setprinter方法失败,无法进行打印机设置!");
sendmessagetimeout(
new intptr(hwnd_broadcast),
wm_settingchange,
intptr.zero,
intptr.zero,
printer.sendmessagetimeoutflags.smto_normal,
1000,
out hdummy);
}
finally
{
closeprinter(hprinter);
}
}
else
{
stringbuilder strbuilder = new stringbuilder();
strbuilder.appendformat("无法打开打印机{0}, 错误代号: {1}",
printername, getlasterror());
throw new applicationexception(strbuilder.tostring());
}
}
else
{
structdevmode pdevmode = new structdevmode();
intptr hdc = createdc(null, printername, null, ref pdevmode);
if (hdc != intptr.zero)
{
const long dm_papersize = 0x00000002l;
const long dm_paperlength = 0x00000004l;
const long dm_paperwidth = 0x00000008l;
pdevmode.dmfields = (int)(dm_papersize | dm_paperwidth | dm_paperlength);
pdevmode.dmpapersize = 256;
pdevmode.dmpaperwidth = (short)(widthinmm * 1000.0);
pdevmode.dmpaperlength = (short)(heightinmm * 1000.0);
resetdc(hdc, ref pdevmode);
deletedc(hdc);
}
}
}
#endregion 指定的打印机设置以mm为单位的自定义纸张(form)
#region 获取本地打印机列表
/**/
/// <summary>
/// 获取本地打印机列表
/// 可以通过制定参数获取网络打印机
/// </summary>
/// <returns>打印机列表</returns>
public static system.collections.arraylist getprinterlist()
{
system.collections.arraylist alret = new system.collections.arraylist();
printer_info_2[] info2 = enumprintersbyflag(printerenumflags.printer_enum_local);
for (int i = 0; i < info2.length; i++)
{
alret.add(info2[i].pprintername);
}
return alret;
}
#endregion 获取本地打印机列表
#region 获取本机的默认打印机名称
/**/
/// <summary>
/// 获取本机的默认打印机名称
/// </summary>
/// <returns>默认打印机名称</returns>
public static string getdeaultprintername()
{
stringbuilder dp = new stringbuilder(256);
int size = dp.capacity;
if (getdefaultprinter(dp, ref size))
{
return dp.tostring();
}
else
{
return string.empty;
}
}
#endregion 获取本机的默认打印机名称
#region 设置默认打印机
/**/
/// <summary>
/// 设置默认打印机
/// </summary>
/// <param name="printername">可用的打印机名称</param>
public static void setprintertodefault(string printername)
{
setdefaultprinter(printername);
}
#endregion 设置默认打印机
#region 判断打印机是否在系统可用的打印机列表中
/**/
///// <summary>
///// 判断打印机是否在系统可用的打印机列表中
///// </summary>
///// <param name="printername">打印机名称</param>
///// <returns>是:在;否:不在</returns>
public static bool printerinlist(string printername)
{
bool bolret = false;
system.collections.arraylist alprinters = getprinterlist();
for (int i = 0; i < alprinters.count; i++)
{
if (printername == alprinters[i].tostring())
{
bolret = true;
break;
}
}
alprinters.clear();
alprinters = null;
return bolret;
}
#endregion 判断打印机是否在系统可用的打印机列表中
#region 判断表单是否在指定的打印机所支持的纸张列表中
/**/
///// <summary>
///// 判断表单是否在指定的打印机所支持的纸张列表中,表单就是我们平常所说的纸张
///// </summary>
///// <param name="printername">打印机名称</param>
///// <param name="papername">纸张名称</param>
///// <returns>是:在;否:不在</returns>
public static bool forminprinter(string printername, string papername)
{
bool bolret = false;
system.drawing.printing.printdocument pd = new system.drawing.printing.printdocument();
pd.printersettings.printername = printername;
foreach (system.drawing.printing.papersize ps in pd.printersettings.papersizes)
{
if (ps.papername == papername)
{
bolret = true;
break;
}
}
pd.dispose();
return bolret;
}
#endregion 判断表单是否在指定的打印机所支持的纸张列表中
#region 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
/**/
/// <summary>
/// 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
/// </summary>
/// <param name="printername">打印机名称</param>
/// <param name="formname">表单名称</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static bool formsamesize(string printername, string formname, decimal width, decimal height)
{
bool bolret = false;
system.drawing.printing.printdocument pd = new system.drawing.printing.printdocument();
pd.printersettings.printername = printername;
foreach (system.drawing.printing.papersize ps in pd.printersettings.papersizes)
{
if (ps.papername == formname)
{
decimal decwidth = frominchtocm(system.convert.todecimal(ps.width));
decimal decheight = frominchtocm(system.convert.todecimal(ps.height));
//只要整数位相同即认为是同一纸张,毕竟inch到cm的转换并不能整除
if (math.round(decwidth, 0) == math.round(width, 0) && math.round(decheight, 0) == math.round(height, 0))
bolret = true;
break;
}
}
pd.dispose();
return bolret;
}
#endregion 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
#region 英寸到厘米的转换
/**/
/// <summary>
/// 英寸到厘米的转换
/// /* = = = = = = = = = = = = = = = = *\
/// | 换算一下计量单位,将其换算成厘米 |
/// | 厘米 像素 英寸 |
/// | 1 38 0.395 |
/// | 0.026 1 0.01 |
/// | 2.54 96 1 |
/// \* = = = = = = = = = = = = = = = = */
/// </summary>
/// <param name="inch">英寸数</param>
/// <returns>厘米数,两位小数</returns>
///
public static decimal frominchtocm(decimal inch)
{
return math.round((system.convert.todecimal((inch / 100)) * system.convert.todecimal(2.5400)), 2);
}
#endregion 英寸到厘米的转换
}
}