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

上传人:b****8 文档编号:11478841 上传时间:2023-03-01 格式:DOCX 页数:11 大小:16.53KB
下载 相关 举报
使用C#更改打印机设置.docx_第1页
第1页 / 共11页
使用C#更改打印机设置.docx_第2页
第2页 / 共11页
使用C#更改打印机设置.docx_第3页
第3页 / 共11页
使用C#更改打印机设置.docx_第4页
第4页 / 共11页
使用C#更改打印机设置.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

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

《使用C#更改打印机设置.docx》由会员分享,可在线阅读,更多相关《使用C#更改打印机设置.docx(11页珍藏版)》请在冰豆网上搜索。

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

使用C#更改打印机设置

使用C#更改打印机设置

以下是全球范围内更改打印机设置的C#代码。

publicclassPrinterSettings

{

#region"PrivateVariables"

privateIntPtrhPrinter=newSystem.IntPtr();

privatePRINTER_DEFAULTSPrinterValues=newPRINTER_DEFAULTS();

privatePRINTER_INFO_2pinfo=newPRINTER_INFO_2();

privateDEVMODEdm;

privateIntPtrptrDM;

privateIntPtrptrPrinterInfo;

privateintsizeOfDevMode=0;

privateintlastError;

privateintnBytesNeeded;

privatelongnRet;

privateintintError;

privateSystem.Int32nJunk;

privateIntPtryDevModeData;

#endregion

#region"WinAPIDef"

[DllImport("kernel32.dll",EntryPoint="GetLastError",SetLastError=false,

ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

privatestaticexternInt32GetLastError();

[DllImport("winspool.Drv",EntryPoint="ClosePrinter",SetLastError=true,

ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

privatestaticexternboolClosePrinter(IntPtrhPrinter);

[DllImport("winspool.Drv",EntryPoint="DocumentPropertiesA",SetLastError=true,

ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

privatestaticexternintDocumentProperties(IntPtrhwnd,IntPtrhPrinter,

[MarshalAs(UnmanagedType.LPStr)]stringpDeviceNameg,

IntPtrpDevModeOutput,refIntPtrpDevModeInput,intfMode);

[DllImport("winspool.Drv",EntryPoint="GetPrinterA",SetLastError=true,

CharSet=CharSet.Ansi,ExactSpelling=true,

CallingConvention=CallingConvention.StdCall)]

privatestaticexternboolGetPrinter(IntPtrhPrinter,Int32dwLevel,

IntPtrpPrinter,Int32dwBuf,outInt32dwNeeded);

/*[DllImport("winspool.Drv",EntryPoint="OpenPrinterA",

SetLastError=true,CharSet=CharSet.Ansi,

ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

staticexternboolOpenPrinter([MarshalAs(UnmanagedType.LPStr)]stringszPrinter,

outIntPtrhPrinter,refPRINTER_DEFAULTSpd)

[DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternlongOpenPrinter(stringpPrinterName,

refIntPtrphPrinter,intpDefault);*/

/*[DllImport("winspool.Drv",EntryPoint="OpenPrinterA",

SetLastError=true,CharSet=CharSet.Ansi,

ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

staticexternboolOpenPrinter([MarshalAs(UnmanagedType.LPStr)]stringszPrinter,

outIntPtrhPrinter,refPRINTER_DEFAULTSpd);

*/

[DllImport("winspool.Drv",EntryPoint="OpenPrinterA",

SetLastError=true,CharSet=CharSet.Ansi,

ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

privatestaticexternbool

OpenPrinter([MarshalAs(UnmanagedType.LPStr)]stringszPrinter,

outIntPtrhPrinter,refPRINTER_DEFAULTSpd);

[DllImport("winspool.drv",CharSet=CharSet.Ansi,SetLastError=true)]

privatestaticexternboolSetPrinter(IntPtrhPrinter,intLevel,IntPtr

pPrinter,intCommand);

/*[DllImport("winspool.drv",CharSet=CharSet.Ansi,SetLastError=true)]

privatestaticexternboolSetPrinter(IntPtrhPrinter,intLevel,IntPtr

pPrinter,intCommand);*/

//WrapperforWin32messageformatter.

/*[DllImport("kernel32.dll",CharSet=System.Runtime.InteropServices.CharSet.Auto)]

privateunsafestaticexternintFormatMessage(intdwFlags,

refIntPtrpMessageSource,

intdwMessageID,

intdwLanguageID,

refstringlpBuffer,

intnSize,

IntPtr*pArguments);*/

#endregion

#region"Datastructure"

[StructLayout(LayoutKind.Sequential)]

publicstructPRINTER_DEFAULTS

{

publicintpDatatype;

publicintpDevMode;

publicintDesiredAccess;

}

/*[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]

publicstructPRINTER_DEFAULTS

{

publicintpDataType;

publicIntPtrpDevMode;

publicACCESS_MASKDesiredAccess;

}*/

[StructLayout(LayoutKind.Sequential)]

privatestructPRINTER_INFO_2

{

[MarshalAs(UnmanagedType.LPStr)]publicstringpServerName;

[MarshalAs(UnmanagedType.LPStr)]publicstringpPrinterName;

[MarshalAs(UnmanagedType.LPStr)]publicstringpShareName;

[MarshalAs(UnmanagedType.LPStr)]publicstringpPortName;

[MarshalAs(UnmanagedType.LPStr)]publicstringpDriverName;

[MarshalAs(UnmanagedType.LPStr)]publicstringpComment;

[MarshalAs(UnmanagedType.LPStr)]publicstringpLocation;

publicIntPtrpDevMode;

[MarshalAs(UnmanagedType.LPStr)]publicstringpSepFile;

[MarshalAs(UnmanagedType.LPStr)]publicstringpPrintProcessor;

[MarshalAs(UnmanagedType.LPStr)]publicstringpDatatype;

[MarshalAs(UnmanagedType.LPStr)]publicstringpParameters;

publicIntPtrpSecurityDescriptor;

publicInt32Attributes;

publicInt32Priority;

publicInt32DefaultPriority;

publicInt32StartTime;

publicInt32UntilTime;

publicInt32Status;

publicInt32cJobs;

publicInt32AveragePPM;

}

privateconstshortCCDEVICENAME=32;

privateconstshortCCFORMNAME=32;

[StructLayout(LayoutKind.Sequential)]

publicstructDEVMODE

{

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=CCDEVICENAME)]

publicstringdmDeviceName;

publicshortdmSpecVersion;

publicshortdmDriverVersion;

publicshortdmSize;

publicshortdmDriverExtra;

publicintdmFields;

publicshortdmOrientation;

publicshortdmPaperSize;

publicshortdmPaperLength;

publicshortdmPaperWidth;

publicshortdmScale;

publicshortdmCopies;

publicshortdmDefaultSource;

publicshortdmPrintQuality;

publicshortdmColor;

publicshortdmDuplex;

publicshortdmYResolution;

publicshortdmTTOption;

publicshortdmCollate;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=CCFORMNAME)]

publicstringdmFormName;

publicshortdmUnusedPadding;

publicshortdmBitsPerPel;

publicintdmPelsWidth;

publicintdmPelsHeight;

publicintdmDisplayFlags;

publicintdmDisplayFrequency;

}

#endregion

#region"Constants"

privateconstintDM_DUPLEX=0x1000;

privateconstintDM_IN_BUFFER=8;

privateconstintDM_OUT_BUFFER=2;

privateconstintPRINTER_ACCESS_ADMINISTER=0x4;

privateconstintPRINTER_ACCESS_USE=0x8;

privateconstintSTANDARD_RIGHTS_REQUIRED=0xF0000;

privateconstintPRINTER_ALL_ACCESS=

(STANDARD_RIGHTS_REQUIRED|PRINTER_ACCESS_ADMINISTER

|PRINTER_ACCESS_USE);

#endregion

#region"Functiontochangeprintersettings"

publicboolChangePrinterSetting(stringPrinterName,PrinterDataPS)

{

if(((int)PS.Duplex<1)||((int)PS.Duplex>3))

{

thrownewArgumentOutOfRangeException("nDuplexSetting",

"nDuplexSettingisincorrect.");

}

else

{

dm=this.GetPrinterSettings(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;

/*updatedriverdependentpartoftheDEVMODE

1=DocumentProperties(IntPtr.Zero,hPrinter,sPrinterName,yDevModeData

refpinfo.pDevMode,(DM_IN_BUFFER|DM_OUT_BUFFER));*/

Marshal.StructureToPtr(pinfo,ptrPrinterInfo,true);

lastError=Marshal.GetLastWin32Error();

nRet=Convert.ToInt16(SetPrinter(hPrinter,2,ptrPrinterInfo,0));

if(nRet==0)

{

//Unabletosetsharedprintersettings.

lastError=Marshal.GetLastWin32Error();

//stringmyErrMsg=GetErrorMessage(lastError);

thrownewWin32Exception(Marshal.GetLastWin32Error());

}

if(hPrinter!

=IntPtr.Zero)

ClosePrinter(hPrinter);

returnConvert.ToBoolean(nRet);

}

}

privateDEVMODEGetPrinterSettings(stringPrinterName)

{

PrinterDataPData=newPrinterData();

DEVMODEdm;

constintPRINTER_ACCESS_ADMINISTER=0x4;

constintPRINTER_ACCESS_USE=0x8;

constintPRINTER_ALL_ACCESS=(STANDARD_RIGHTS_REQUIRED|

PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE);

PrinterValues.pDatatype=0;

PrinterValues.pDevMode=0;

PrinterValues.DesiredAccess=PRINTER_ALL_ACCESS;

nRet=Convert.ToInt32(OpenPrinter(PrinterName,

outhPrinter,refPrinterValues));

if(nRet==0)

{

lastError=Marshal.GetLastWin32Error();

thrownewWin32Exception(Marshal.GetLastWin32Error());

}

GetPrinter(hPrinter,2,IntPtr.Zero,0,outnBytesNeeded);

if(nBytesNeeded<=0)

{

thrownewSystem.Exception("Unabletoallocatememory");

}

else

{

//AllocateenoughspaceforPRINTER_INFO_2...

{ptrPrinterInfo=Marshal.AllocCoTaskMem(nBytesNeeded)};

ptrPrinterInfo=Marshal.AllocHGlobal(nBytesNeeded);

//ThesecondGetPrinterfillsinallthecurrentsettings,soallyou

//needtodoismodifywhatyou'reinterestedin...

nRet=Convert.ToInt32(GetPrinter(hPrinter,2,

ptrPrinterInfo,nBytesNeeded,outnJunk));

if(nRet==0)

{

lastError=Marshal.GetLastWin32Error();

thrownewWin32Exception(Marshal.GetLastWin32Error());

}

pinfo=(PRINTER_INFO_2)Marshal.PtrToStructure(ptrPrinterInfo,

typeof(PRINTER_INFO_2));

IntPtrTemp=newIntPtr();

if(pinfo.pDevMode==IntPtr.Zero)

{

//IfGetPrinterdidn'tfillintheDEVMODE,trytogetitbycalling

//DocumentProperties...

IntPtrptrZero=IntPtr.Zero;

//getthesizeofthedevmodestructure

sizeOfDevMode=DocumentProperties(IntPtr.Zero,hPrinter,

PrinterName,ptrZero,refptrZero,0);

ptrDM=Marshal.AllocCoTaskMem(sizeOfDevMode);

inti;

i=DocumentProperties(IntPtr.Zero,hPrinter,PrinterName,ptrDM,

refptrZero,DM_OUT_BUFFER);

if((i<0)||(ptrDM==IntPtr.Zero))

{

//CannotgettheDEVMODEstructure.

thrownewSystem.Exception("CannotgetDEVMODEdata");

}

pinfo.pDevMode=ptrDM;

}

intError=DocumentProperties(IntPtr.Zero,hPrinter,

PrinterName,IntPtr.Zero,refTemp,0);

//IntPtryDevModeData=Marshal.AllocCoTaskMem(i1);

yDevModeData=Marshal.AllocHGlobal(intError);

intError=DocumentProperties(IntPtr.Zero,hPrinter,

PrinterName,yDevModeData,refTemp,2);

dm=(DEVMODE)Marshal.PtrToStructure(yDevModeData,typeof(DEVMODE));

//nRet=DocumentProperties(IntPtr.Zero,hPrinter,sPrinterName,yDevModeData

//,refyDevModeData,(DM_IN_BUFFER|DM_OUT_BUFFER)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 自考

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

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