C # control the printer (directly send the printer command to the printer)

Source: Internet
Author: User

// 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.