// Code: // using system; using system. collections. generic; using system. text; using system. runtime. interopservices; namespace lptcontrols {public class lptcontrols {[structlayout (layoutkind. sequential)] private struct overlapped {int internal; int internalhigh; int offset; int offsethigh; int hevent;} [dllimport ("kernel32.dll")] Private Static extern int createfile (string lpfilename, uint dwdesiredacces S, int dwsharemode, int lpsecurityattributes, int dwcreationdisposition, int second, int htemplatefile); [dllimport ("kernel32.dll")] Private Static extern bool writefile (INT hfile, byte [] lpbuffer, int nnumberofbytestowriter, out int handle, out overlapped lpoverlapped); [dllimport ("kernel32.dll")] Private Static extern bool closehandle (INT hobject); Private int ihandl E; // open the LPT Port Public bool open () {ihandle = createfile ("LPT1", 0x40000000, 0, 0, 3, 0, 0); If (ihandle! =-1) {return true;} else {return false ;}// print the function. The parameter is the printer command or other text! Public bool write (string mystring) {If (ihandle! = 1) {int I; overlapped X; byte [] mybyte = system. text. encoding. default. getbytes (mystring); Return writefile (ihandle, mybyte, mybyte. length, out I, out X);} else {Throw new exception ("port not opened ~! ") ;}}// Close the print Port Public bool close () {return closehandle (ihandle );}}} //************************************** **************************************** * ****** // usage method private void button#click (Object sender, eventargs e) {lptcontrols. lptcontrols LPT = new lptcontrols. lptcontrols (); string mycommanglines = system. io. file. readalltext ("print.txt"); // The command LPT of the bar code machine is written in print.txt. open (); LPT. write (mycommanglines); LPT. close ();}
A printer control class is very useful. The Windows API is used to control the LPT port. The control method for bar code printer is as follows: Write the printer command to a file and then use it.