ImageVerifierCode 换一换
格式:DOCX , 页数:11 ,大小:16.53KB ,
资源ID:11478841      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11478841.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(使用C#更改打印机设置.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

使用C#更改打印机设置.docx

1、使用C#更改打印机设置使用C更改打印机设置以下是全球范围内更改打印机设置的C代码。public class PrinterSettings #region Private Variablesprivate IntPtr hPrinter = new System.IntPtr() ;private PRINTER_DEFAULTS PrinterValues = new PRINTER_DEFAULTS();private PRINTER_INFO_2 pinfo = new PRINTER_INFO_2();private DEVMODE dm;private IntPtr ptrDM;pr

2、ivate IntPtr ptrPrinterInfo;private int sizeOfDevMode = 0;private int lastError;private int nBytesNeeded;private long nRet; private int intError;private System.Int32 nJunk;private IntPtr yDevModeData;#endregion#region Win API DefDllImport(kernel32.dll, EntryPoint=GetLastError, SetLastError=false,Exa

3、ctSpelling=true, CallingConvention=CallingConvention.StdCall)private static extern Int32 GetLastError();DllImport(winspool.Drv, EntryPoint=ClosePrinter, SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)private static extern bool ClosePrinter(IntPtr hPrinter);DllImpo

4、rt(winspool.Drv, EntryPoint=DocumentPropertiesA, SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)private static extern int DocumentProperties (IntPtr hwnd, IntPtr hPrinter, MarshalAs(UnmanagedType.LPStr) string pDeviceNameg, IntPtr pDevModeOutput, ref IntPtr pDevMo

5、deInput, int fMode);DllImport(winspool.Drv, EntryPoint=GetPrinterA, SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)private static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);/*DllIm

6、port(winspool.Drv, EntryPoint=OpenPrinterA, SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)static extern bool OpenPrinter(MarshalAs(UnmanagedType.LPStr) string szPrinter, out IntPtr hPrinter, ref PRINTER_DEFAULTS pd) DllImport( winspool.drv,C

7、harSet=CharSet.Unicode,ExactSpelling=false,CallingConvention=CallingConvention.StdCall )public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);*/*DllImport(winspool.Drv, EntryPoint=OpenPrinterA, SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, Call

8、ingConvention=CallingConvention.StdCall)static extern bool OpenPrinter(MarshalAs(UnmanagedType.LPStr) string szPrinter, out IntPtr hPrinter, ref PRINTER_DEFAULTS pd);*/ DllImport(winspool.Drv, EntryPoint=OpenPrinterA, SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=Cal

9、lingConvention.StdCall)private static extern bool OpenPrinter(MarshalAs(UnmanagedType.LPStr) string szPrinter, out IntPtr hPrinter, ref PRINTER_DEFAULTS pd);DllImport(winspool.drv, CharSet=CharSet.Ansi, SetLastError=true)private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrint

10、er, int Command);/*DllImport(winspool.drv, CharSet=CharSet.Ansi, SetLastError=true)private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int Command);*/ Wrapper for Win32 message formatter./*DllImport(kernel32.dll, CharSet=System.Runtime.InteropServices.CharSet.Auto)priv

11、ate unsafe static extern int FormatMessage( int dwFlags,ref IntPtr pMessageSource,int dwMessageID,int dwLanguageID,ref string lpBuffer,int nSize,IntPtr* pArguments);*/#endregion#region Data structureStructLayout(LayoutKind.Sequential)public struct PRINTER_DEFAULTSpublic int pDatatype;public int pDev

12、Mode;public int DesiredAccess;/*StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)public struct PRINTER_DEFAULTSpublic int pDataType;public IntPtr pDevMode;public ACCESS_MASK DesiredAccess;*/StructLayout(LayoutKind.Sequential)private struct PRINTER_INFO_2MarshalAs(UnmanagedType.LPStr) public

13、string pServerName; MarshalAs(UnmanagedType.LPStr) public string pPrinterName; MarshalAs(UnmanagedType.LPStr) public string pShareName; MarshalAs(UnmanagedType.LPStr) public string pPortName; MarshalAs(UnmanagedType.LPStr) public string pDriverName; MarshalAs(UnmanagedType.LPStr) public string pComm

14、ent; MarshalAs(UnmanagedType.LPStr) public string pLocation; public IntPtr pDevMode; MarshalAs(UnmanagedType.LPStr) public string pSepFile; MarshalAs(UnmanagedType.LPStr) public string pPrintProcessor; MarshalAs(UnmanagedType.LPStr) public string pDatatype; MarshalAs(UnmanagedType.LPStr) public stri

15、ng pParameters; public IntPtr pSecurityDescriptor; public Int32 Attributes; public Int32 Priority; public Int32 DefaultPriority; public Int32 StartTime; public Int32 UntilTime; public Int32 Status; public Int32 cJobs; public Int32 AveragePPM; private const short CCDEVICENAME = 32;private const short

16、 CCFORMNAME = 32;StructLayout(LayoutKind.Sequential) public struct DEVMODE MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCDEVICENAME) public string dmDeviceName;public short dmSpecVersion;public short dmDriverVersion;public short dmSize;public short dmDriverExtra;public int dmFields;public short d

17、mOrientation;public short dmPaperSize;public short dmPaperLength;public short dmPaperWidth;public short dmScale;public short dmCopies;public short dmDefaultSource;public short dmPrintQuality;public short dmColor;public short dmDuplex;public short dmYResolution;public short dmTTOption;public short dm

18、Collate;MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCFORMNAME) public string dmFormName; public short dmUnusedPadding;public short dmBitsPerPel;public int dmPelsWidth;public int dmPelsHeight;public int dmDisplayFlags;public int dmDisplayFrequency;#endregion#region Constantsprivate const int DM_D

19、UPLEX = 0x1000;private const int DM_IN_BUFFER = 8;private const int DM_OUT_BUFFER = 2;private const int PRINTER_ACCESS_ADMINISTER = 0x4;private const int PRINTER_ACCESS_USE = 0x8;private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;private const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | P

20、RINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);#endregion#region Function to change printer settings public bool ChangePrinterSetting(string PrinterName,PrinterData PS)if (int)PS.Duplex 3) )throw new ArgumentOutOfRangeException(nDuplexSetting, nDuplexSetting is incorrect.);elsedm = this.GetPrinterSe

21、ttings( PrinterName);dm.dmDefaultSource =(short)PS.source; dm.dmOrientation = (short)PS.Orientation; dm.dmPaperSize =(short)PS.Size;dm.dmDuplex = (short)PS.Duplex; Marshal.StructureToPtr( dm,yDevModeData,true); pinfo.pDevMode = yDevModeData;pinfo.pSecurityDescriptor = IntPtr.Zero;/*update driver dep

22、endent part of the DEVMODE1 = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, yDevModeData, ref pinfo.pDevMode, (DM_IN_BUFFER | DM_OUT_BUFFER);*/Marshal.StructureToPtr(pinfo,ptrPrinterInfo,true); lastError = Marshal.GetLastWin32Error();nRet = Convert.ToInt16(SetPrinter(hPrinter, 2, ptrPrinte

23、rInfo, 0);if (nRet = 0)/Unable to set shared printer settings.lastError = Marshal.GetLastWin32Error();/string myErrMsg = GetErrorMessage(lastError);throw new Win32Exception(Marshal.GetLastWin32Error(); if (hPrinter != IntPtr.Zero) ClosePrinter(hPrinter);return Convert.ToBoolean(nRet);private DEVMODE

24、 GetPrinterSettings(string PrinterName)PrinterData PData = new PrinterData(); DEVMODE dm;const int PRINTER_ACCESS_ADMINISTER = 0x4;const int PRINTER_ACCESS_USE = 0x8;const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);PrinterValues.pDatatype =0;

25、PrinterValues.pDevMode = 0 ;PrinterValues.DesiredAccess = PRINTER_ALL_ACCESS;nRet = Convert.ToInt32(OpenPrinter(PrinterName, out hPrinter, ref PrinterValues);if (nRet = 0)lastError = Marshal.GetLastWin32Error();throw new Win32Exception(Marshal.GetLastWin32Error(); GetPrinter(hPrinter, 2, IntPtr.Zero

26、, 0, out nBytesNeeded);if (nBytesNeeded = 0)throw new System.Exception(Unable to allocate memory);else/ Allocate enough space for PRINTER_INFO_2. ptrPrinterIn fo = Marshal.AllocCoTaskMem(nBytesNeeded);ptrPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded);/ The second GetPrinter fills in all the curren

27、t settings, so all you / need to do is modify what youre interested in.nRet = Convert.ToInt32(GetPrinter(hPrinter, 2, ptrPrinterInfo, nBytesNeeded, out nJunk);if (nRet = 0)lastError = Marshal.GetLastWin32Error();throw new Win32Exception(Marshal.GetLastWin32Error(); pinfo = (PRINTER_INFO_2)Marshal.Pt

28、rToStructure(ptrPrinterInfo, typeof(PRINTER_INFO_2);IntPtr Temp = new IntPtr();if (pinfo.pDevMode = IntPtr.Zero)/ If GetPrinter didnt fill in the DEVMODE, try to get it by calling/ DocumentProperties.IntPtr ptrZero = IntPtr.Zero;/get the size of the devmode structuresizeOfDevMode = DocumentPropertie

29、s(IntPtr.Zero, hPrinter, PrinterName, ptrZero, ref ptrZero, 0);ptrDM = Marshal.AllocCoTaskMem(sizeOfDevMode);int i ;i = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, ptrDM, ref ptrZero, DM_OUT_BUFFER);if (i 0) | (ptrDM = IntPtr.Zero)/Cannot get the DEVMODE structure.throw new System.Excepti

30、on(Cannot get DEVMODE data); pinfo.pDevMode = ptrDM;intError = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, IntPtr.Zero , ref Temp , 0);/IntPtr yDevModeData = Marshal.AllocCoTaskMem(i1);yDevModeData= Marshal.AllocHGlobal(intError);intError = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, yDevModeData , ref Temp , 2);dm = (DEVMODE)Marshal.PtrToStructure(yDevModeData, typeof(DEVMODE);/nRet = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, yDevModeData/ , ref yDevModeData, (DM_IN_BUFFER | DM_OUT_BUFFER)

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1