private void button2_Click(object sender, System.EventArgs e)
{
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
long j=PrintDirect.OpenPrinter("\\\\192.168.1.4\\Star",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 e1)
{
MessageBox.Show("打印机器");
} PrintDirect.EndPagePrinter(lhPrinter);
PrintDirect.EndDocPrinter(lhPrinter);
PrintDirect.ClosePrinter(lhPrinter); }
btn2的代码是有用的,
可什么反映也没有