希望这里有人可以帮助我,因为我正用头撞墙试图让它发挥作用。基本上我需要使用winspool.Drv win32 API 将原始文本/PCL 命令发送到打印机进行打印。这可以正常工作,但是,您在打印机对话框/首选项中选择的任何打印机选项都不会做任何事情。
最终,我想从 PrinterSettings 对话框中获取 DEVMODE,并将其用于原始打印,但我什至无法手动设置它。
我曾尝试在 WritePrinter 之前或在单独的打开/关闭打印机之前更改 DEVMODE,我尝试仅使用 DocumentProperties 调用,并使用带有 PRINTER_INFO_8 结构的 GetPrinter/SetPrinter,没有任何问题。DEVMODE 结构似乎填写正确,我更改它,它似乎更改正确,但无论如何打印机总是以相同的方式打印。
这是我一直在使用的代码:
方法1:
public static bool SetPrinterSettings(PrinterSettings settings, IntPtr hPrinter)
{
IntPtr hDevMode; // a handle to our current DEVMODE
IntPtr pDevMode; // a pointer to our current DEVMODE
String sPrinterName; // normalized printer name
DEVMODE dm;
// Setup
sPrinterName = settings.PrinterName.Normalize();
// Obtain the current DEVMODE from the PrinterSettings
hDevMode = settings.GetHdevmode(settings.DefaultPageSettings);
// Obtain a lock on the handle and get an actual pointer so Windows won't move
// it around while we're futzing with it
pDevMode = GlobalLock(hDevMode);
// test
dm = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE));
// Our DEVMODE is 188 bytes, but dmSize returns 220, does this matter?
// This code doesnt seem to work
//dm.dmSize = (short)Marshal.SizeOf(dm); // Set size to our implementation of DevMode
//int isize = GlobalSize(pDevMode).ToInt32() - (int)dm.dmSize; // Set the print drivers extra size
//dm.dmDriverExtra = Convert.ToInt16(isize);
// Change things
dm.dmFields = DM_FIELD_TYPE.DM_ORIENTATION;
dm.dmOrientation = 2;
// Load the structure back into the buffer
Marshal.StructureToPtr(dm, pDevMode, true);
//Tell the printer about the new property
int ret = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, pDevMode, pDevMode, (DM_IN_BUFFER | DM_OUT_BUFFER));
if (ret <= 0)
{
return false;
}
// We're done futzing
GlobalUnlock(hDevMode);
// Tell our printer settings to use the one we just overwrote
//settings.SetHdevmode(hDevMode);
// It's copied to our printer settings, so we can free the OS-level one
GlobalFree(hDevMode);
//test
dm = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE));
// ^^ this contains correct orientation
return true;
}
方法2:
public static bool SetLandscapeMode(PrinterSettings settings, IntPtr hPrinter)
{
// Setup
String sPrinterName = settings.PrinterName.Normalize();
// get current printer settings
int memNeeded = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, IntPtr.Zero, IntPtr.Zero, 0);
IntPtr pFullDevMode = Marshal.AllocHGlobal(memNeeded);
DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, pFullDevMode, IntPtr.Zero, DM_OUT_BUFFER);
DEVMODE dm = (DEVMODE)Marshal.PtrToStructure(pFullDevMode, typeof(DEVMODE));
// change the settings
dm.dmFields = DM_FIELD_TYPE.DM_ORIENTATION;
dm.dmOrientation = 2;
Marshal.StructureToPtr(dm, pFullDevMode, true);
PRINTER_INFO_8 PI8 = new PRINTER_INFO_8();
PI8.pDevMode = pFullDevMode;
IntPtr pPI8 = Marshal.AllocHGlobal(Marshal.SizeOf(PI8));
Marshal.StructureToPtr(PI8, pPI8, true);
// save the printer settings
SetPrinter(hPrinter, 9, pPI8, 0);
Marshal.FreeHGlobal(pPI8);
return true;
}
测试打印的代码:
private void testPrint_Click(object sender, EventArgs e)
{
IntPtr hPrinter;
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
hPrinter = RawPrint.OpenRAWPrinter(printDialog1.PrinterSettings.PrinterName);
if (hPrinter == IntPtr.Zero)
{
MessageBox.Show("Error opening printer");
return;
}
if (!RawPrint.StartRAWDocument(hPrinter, "Test document"))
{
MessageBox.Show("Error starting raw document");
RawPrint.CloseRAWPrinter(hPrinter);
return;
}
// Start a page.
if (RawPrint.StartRawPage(hPrinter))
{
// Set the printers settings
//if (!RawPrint.SetPrinterSettings(printDialog1.PrinterSettings, hPrinter))
if(!RawPrint.SetLandscapeMode(printDialog1.PrinterSettings, hPrinter))
{
MessageBox.Show("Error setting printer settings");
RawPrint.CloseRAWPrinter(hPrinter);
return;
}
String test = "This is \r\n test print data";
byte[] buf = Encoding.Default.GetBytes(test);
GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
RawPrint.writeRAW(hPrinter, gch.AddrOfPinnedObject(), buf.Length);
gch.Free();
RawPrint.EndRawPage(hPrinter);
}
RawPrint.EndRAWDocument(hPrinter);
RawPrint.CloseRAWPrinter(hPrinter);
}
else
{
MessageBox.Show("Error starting raw page");
}
}
唯一奇怪的是 Marshal.SizeOf(typeof(DEVMODE)) 或 Marshal.SizeOf(dm) 等于 188,而返回到结构中的 dmSize 是 220。我尝试修改这个 dmSize 以匹配我们的结构,如注释中所示输出代码,但这似乎出错或不起作用,不确定这是否重要。
除此之外,在 DevModes 中返回的信息似乎都是正确且有效的。我可以在打印对话框中设置选项,并在 PrinterSettings 开发模式中查看它们的更改。在这些函数结束时,将 pDevMode 编组回结构具有更改的设置。
然而,这一切似乎对印刷品没有任何影响。我有什么遗漏吗?
编辑:测试函数中的 RawPrint 调用只是围绕 spooler api 调用的包装器。该页面打印良好,它总是以纵向模式打印,并且没有任何选项有效。