C # Send printer commands directly to printer ++++

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;

Namespace Barcodelibrary
{
public class Zebragesigner
{
[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 dwdesiredaccess, int dwsharemode, int lpsecurityattributes, I NT dwCreationDisposition, int dwflagsandattributes, int htemplatefile);
[DllImport ("kernel32.dll")]
private static extern bool WriteFile (int hfile, byte[] lpbuffer, int nnumberofbytestowriter, out int lpnumberofbyteswriten , out OVERLAPPED lpoverlapped);
[DllImport ("kernel32.dll")]
private static extern bool CloseHandle (int hobject);
[DllImport ("Fnthex32.dll")]
public static extern int Getfonthex (string barcodetext,string fontname,int orient,int height,int width,int isbold,int IsI Talic,stringbuilder returnbarcodecmd);
private int ihandle;
Open LPT port
public bool Open ()
{
Ihandle = CreateFile ("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (ihandle! =-1)
{
return true;
}
Else
{
return false;
}
}
Print functions, parameters for printer commands 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 is not open!") ");
}
}
Close the print port
public bool Close ()
{
return CloseHandle (Ihandle);
}
}
}

*************************************

vate void Button1_Click (object sender, EventArgs e)
{
Zebragesigner ZB = new Zebragesigner ();
String mycommanglines = System.IO.File.ReadAllText ("Print.txt");//print.txt wrote the Order of the barcode machine.
Zb. Open ();
Zb. Write (Mycommanglines);
Zb. Close ();
}

C # Send printer commands directly to printer ++++

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.