如何把一个字符串直接送打印机打印?

YHW 2003-10-16 11:48:59
调用那个类?请告知.谢谢
...全文
176 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2003-10-29
  • 打赏
  • 举报
回复
下面的代码可以通过调用WIN32 API让你直接输出到打印机,因而可以达到最快的速度进行打印,而不是等待Windows的打印系统。此外,示例代码还说明了如何向打印机发送PCL代码。

// PrintDirect.cs
// 本文参考了Microsoft Support 文档号:Q298141
// 本代码假定你在file://192.168.1.101/hpl存在共享打印机
// 本代码示例了如何向打印机发送Hewlett Packard PCL5代码直接在页面中央打印出一个矩形。


using System;
using System.Text;
using System.Runtime.InteropServices;

[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}

public class PrintDirect
{
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long OpenPrinter(string pPrinterName,ref IntPtr phPrinter,
int pDefault);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo);

[ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long StartPagePrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long WritePrinter(IntPtr hPrinter,string data,
int buf,ref int pcWritten);

[ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long EndPagePrinter(IntPtr hPrinter);

[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long EndDocPrinter(IntPtr hPrinter);

[ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
public static extern long ClosePrinter(IntPtr hPrinter);
}

public class App
{
public static void Main ()
{
System.IntPtr lhPrinter=new System.IntPtr();

DOCINFO di = new DOCINFO();
int pcWritten=0;
string st1;

// text to print with a form feed character
st1="This is an example of printing directly to a printer\f";
di.pDocName="my test document";
di.pDataType="RAW";

// the \x1b means an ascii escape character
st1="\x1b*c600a6b0P\f";
//lhPrinter contains the handle for the printer opened
//If lhPrinter is 0 then an error has occured
PrintDirect.OpenPrinter("\\\\192.168.1.101\\hpl",ref lhPrinter,0);
PrintDirect.StartDocPrinter(lhPrinter,1,ref di);
PrintDirect.StartPagePrinter(lhPrinter);
try
{
// Moves the cursor 900 dots (3 inches at 300 dpi) in from the left margin, and
// 600 dots (2 inches at 300 dpi) down from the top margin.
st1="\x1b*p900x600Y";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);

// Using the print model commands for rectangle dimensions, "600a" specifies a rectangle
// with a horizontal size or width of 600 dots, and "6b" specifies a vertical
// size or height of 6 dots. The 0P selects the solid black rectangular area fill.
st1="\x1b*c600a6b0P";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);

// Specifies a rectangle with width of 6 dots, height of 600 dots, and a
// fill pattern of solid black.
st1="\x1b*c6a600b0P";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);
// Moves the current cursor position to 900 dots, from the left margin and
// 1200 dots down from the top margin.
st1="\x1b*p900x1200Y";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);
// Specifies a rectangle with a width of 606 dots, a height of 6 dots and a

// fill pattern of solid black.
st1="\x1b*c606a6b0P";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);

// Moves the current cursor position to 1500 dots from the left margin and
// 600 dots down from the top margin.
st1="\x1b*p1500x600Y";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);

// Specifies a rectangle with a width of 6 dots, a height of 600 dots and a
// fill pattern of solid black.
st1="\x1b*c6a600b0P";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);
// Send a form feed character to the printer
st1="\f";
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

PrintDirect.EndPagePrinter(lhPrinter);
PrintDirect.EndDocPrinter(lhPrinter);
PrintDirect.ClosePrinter(lhPrinter);
}
}



http://www.c-sharpcorner.com/Code/2002/Oct/PrintDirect.asp
liqianhong 2003-10-29
  • 打赏
  • 举报
回复
FileStream fs=new FileStream("LPT1",FileMode.Write);
fs.Write("fdsfdsf");
fs.Close();
行不行,我也没试过,不过你可以试一下
YHW 2003-10-29
  • 打赏
  • 举报
回复
难道没人知道?
YHW 2003-10-17
  • 打赏
  • 举报
回复
1楼的方法要和printdocument配合才能用,太麻烦了?有没有简单的?
楼上的,具体点啊.
weifenluo 2003-10-16
  • 打赏
  • 举报
回复
打开一个名字为“LPT1”的文件往这个文件写应该就行了
CSTerry 2003-10-16
  • 打赏
  • 举报
回复
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("SampleText",
new Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125);
}

110,531

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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