Author: Meng will come from: [it] would be a wonderful world Meng Date: 2003 at 10:49:57 on on June 15
//printdirect.cs
// This article refers to the Microsoft Support document number: Q298141
// This code assumes that you have shared printers in File: //192.168.1.101/HPL
// This code example how to send the Hewlett Packard PCL5 code to the printer directly printed a rectangle in the center of the page.
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 = calingconvention.stdcall)]
Public Static Extern long openprinter (String Pprintername, Ref INTPTR Phprinter,
INT PDEFAULT);
[DLLIMPORT ("Winspool.drv", Charset = Charset.unicode, Exactspelling = FALSE,
Callingconvention = calingconvention.stdcall)]
Public Static Extern long StartDocprinter (INTPTR HPRINTER, INT Level,
Ref docinfo pdocinfo;
[DLLIMPORT ("Winspool.drv", Charset = Charset.unicode, Exactspelling = True,
Callingconvention = calingconvention.stdcall)]
Public Static Extern long StartPageprinter (INTPTR HPRINTER);
[DLLIMPORT ("Winspool.drv", Charset = Charset.ansi, Exactspelling = True,
Callingconvention = calingconvention.stdcall)]
Public Static Extern Long Writeprinter (INTPTR HPRINTER, STRING DATA,
INT buf, ref int pcwritten;
[DLLIMPORT ("Winspool.drv", Charset = Charset.unicode, Exactspelling = True,
Callingconvention = calingconvention.stdcall)]
Public Static Extern long (INTPTR HPRINTER);
[DLLIMPORT ("Winspool.drv", Charset = charset.unicode, exactspelling = true, callingconvention = callingconvention.stdcall)]
Public Static Extern long (INTPTR HPRINTER);
[DLLIMPORT ("Winspool.drv", Charset = Charset.unicode, Exactspelling = True,
Callingconvention = calingconvention.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 aprinter / f";
Di.pdocname = "my test document";
Di.pdattype = "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 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 DOM 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);
}
}