打印机如何将自定义纸型设置为默认纸型

andy7643 2005-12-21 12:42:21
我在打印机中自定义纸型名称是PRINT

我在程序中如下所写

Dim pz As System.Drawing.Printing.PaperSize
Dim pd As New System.Drawing.Printing.PrintDocument
For Each pz In pd.PrinterSettings.PaperSizes
If pz.PaperName = "PRINT" Then
pd.DefaultPageSettings.PaperSize = pz
Exit For
End If
Next


能够执行到pd.DefaultPageSettings.PaperSize = pz这行代码
但是,打印时的默认纸型不是PRINT

请教一下,这是怎么回事?
应该怎么解决这个问题?
...全文
874 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
andy7643 2006-01-23
  • 打赏
  • 举报
回复
有没有vb.net 的代码阿
andy7643 2005-12-26
  • 打赏
  • 举报
回复
谁帮我解决了阿?
感激不尽!!!!!!!!!!
  • 打赏
  • 举报
回复
这个问题问的人太多了。
从网友的回答中给你一下自定义纸张的方法

http://community.csdn.net/Expert/topic/4318/4318195.xml?temp=.8120691


前几天我问你的一个连续纸打印的问题,现在已经解决了,方法虽然不太好,但是简单有效。我把它记在我的网络日志里了。

http://spaces.msn.com/members/vdonkey/

本来想去网上发一发,可是多数相关贴子已结贴。所以我想到发给你才更可能会帮到其它人。如果有人向你求助相关问题,可以让他参考一下。

andy7643 2005-12-22
  • 打赏
  • 举报
回复
最后的打印语句是cviewer.printreport()//cviewer是crystalReportViewer控件
andy7643 2005-12-21
  • 打赏
  • 举报
回复
我用的是水晶报表打印

如何将PrintDocument和水晶报表关连起来
yaowang820907 2005-12-21
  • 打赏
  • 举报
回复
里面用到了一些 结构体 和 枚举
你可以通过查询win32 api手册 获得
yaowang820907 2005-12-21
  • 打赏
  • 举报
回复
使用printdcoument只能改变程序运行时的打印机状态
当printdocument对象释放的时候 打印机自动恢复默认设置

应该调用windows api实现

public static bool ChangePrinterSetting(ref PrinterData PS, bool bl)
{
if ((((int)PS.duplex < 1) || ((int)PS.duplex > 3)) && bl )
{
throw new ArgumentOutOfRangeException("nDuplexSetting","nDuplexSetting is incorrect.");
}
else
{

string PrinterName = GetDefaultPrinterName();
dm = GetPrinterSettings(PrinterName);
if(bl)
{
/*dm.dmFields |= (int)DmFields.DM_DUPLEX | (int)DmFields.DM_ORIENTATION
| (int)DmFields.DM_DEFAULTSOURCE | (int)DmFields.DM_PAPERSIZE
| (int)DmFields.DM_PAPERLENGTH | (int)DmFields.DM_PAPERWIDTH;

*/
dm.dmFields = PS.pmFields;
dm.dmDuplex = (short)PS.duplex;
dm.dmOrientation = (short)PS.orientation;
dm.dmDefaultSource =(short)PS.source;
dm.dmPaperSize =(short)PS.size;
dm.dmPaperLength = (short)PS.pLength;
dm.dmPaperWidth = (short)PS.pWidth;
}
else
{
PS.pmFields = dm.dmFields;
PS.duplex = dm.dmDuplex;
PS.orientation = dm.dmOrientation;
PS.source = dm.dmDefaultSource;
PS.size = dm.dmPaperSize;
PS.pLength = dm.dmPaperLength;
PS.pWidth = dm.dmPaperWidth;
}
Marshal.StructureToPtr( dm,yDevModeData,true);
pinfo.pDevMode = yDevModeData;
pinfo.pSecurityDescriptor = IntPtr.Zero;
/*update driver dependent part of the DEVMODE
1 = 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, ptrPrinterInfo, 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 static DEVMODE 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;
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, 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 current settings, so all you // need to do is modify what you're 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.PtrToStructure(ptrPrinterInfo, typeof(PRINTER_INFO_2));
IntPtr Temp = new IntPtr();

if (pinfo.pDevMode == IntPtr.Zero)
{
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
IntPtr ptrZero = IntPtr.Zero;
//get the size of the devmode structure
sizeOfDevMode = DocumentProperties(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.Exception("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));
if ((nRet == 0) || (hPrinter == IntPtr.Zero))
{
lastError = Marshal.GetLastWin32Error();
//string myErrMsg = GetErrorMessage(lastError);
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return dm;
}
}
上述代码可以实现 纸张大小 打印方向 以及 进纸口的设置

希望对你有帮助

有什么问题发邮件问我 yaowang820907@163.com
xjtandqt 2005-12-21
  • 打赏
  • 举报
回复
我的代码:

Dim ps As New PageSettings
dim pDocument as new PrintDocument
Dim myPaperSize As System.Drawing.Printing.PaperSize

If ps.PrinterSettings.InstalledPrinters.Count > 0 Then '如过系统存在打印机
If ps.PrinterSettings.IsValid = True Then '当前打印机是否有效
For Each myPaperSize In ps.PrinterSettings.PaperSizes '检查该当前打印机是否支持A3的纸张
If myPaperSize.Kind = PaperKind.A3 Then
Exit For
End If
next
If myPaperSize.Kind <> PaperKind.A3 Then '如果当前打印机不支持A3纸张,那么自定义A3大小的纸张
myPaperSize = New System.Drawing.Printing.PaperSize("A3", 1169, 1654)
End If
ps.PaperSize = myPaperSize '设置为指定的纸张
ps.Landscape = False
pDocument.DefaultPageSettings = ps
pDocument.PrinterSettings.PrinterName = ps.PrinterSettings.PrinterName
End If
End If

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧