C # LPT operation print bar code

Source: Internet
Author: User

The bar code is usually printed by command or image, and the image is fastreport.

However, I have not found the method for vs to call it. It is only successfully used in Delphi 7. While

In fact, most bar code printer manufacturers have their own print instruction language,

With this language, you can print directly without a driver, and the operation is also very simple, you only need

The command is sent to the printer.

VS has a comport control, but there is no ready-made LPT port control.

For the port, the LPT speed is faster, so when printing, the customer generally chooses the LPT communication method.

After some consultation on the Internet, the LPT port printing is finally realized, and the printer is Zebra, write

To share with you.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace printdemo
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
Tbbarcode. Focus ();
}

Private void tbbarcode_keydown (Object sender,

Keyeventargs E)
{
Switch (E. keycode)
{
Case keys. Enter:
Printbarcode (tbbarcode. Text. Trim ());
Tbbarcode. Text = "";
Tbbarcode. Focus ();
Break;
Default:
Break;
}
}
Private void printbarcode (string barcode)
{
Barcode = "^ XA ^ fo48, 12 ^ by4 ^ BCN, 152, N, N ^ FD>;" +

Barcode. Trim () + "^ FS ^ ft62, 220 ^ ci0 ^ ABN, 44,28 ^ FD" +

Barcode. Trim () + "^ FS ^ pq1, 0, 1, y ^ xz ";
Printdemo. posprinter PRN = new

Printdemo. posprinter ("LPT1 ");
String strmsg = PRN. printline (barcode );
If (strmsg! = "")
{
MessageBox. Show (strmsg );
}
}
}
}
 

The posprinter class is defined as follows:

Namespace printdemo
{
Class posprinter
{
Const int open_existing = 3;
String prnport = "LPT1 ";
[Dllimport ("kernel32.dll", charset = charset. Auto)]
Private Static extern intptr createfile (string

Lpfilename,
Int dwdesiredaccess,
Int dww.mode,
Int lpsecurityattributes,
Int dwcreationdisposition,
Int dwflagsandattributes,
Int htemplatefile );

Public posprinter ()
{
//
// Todo: add the constructor logic here
//
}

Public posprinter (string prnport)
{
This. prnport = prnport; // printer port
}

Public String printline (string Str)
{

Intptr ihandle = createfile (prnport, 0x40000000,

0, 0, open_existing, 0, 0 );
If (ihandle. toint32 () =-1)
{
Return "LPT1 port open failed ";
}
Else
{

Filestream FS = new filestream (ihandle,

Fileaccess. readwrite );
Streamwriter Sw = new streamwriter (FS,

System. Text. encoding. Default); // write data
Sw. writeline (STR );
Sw. Close ();
FS. Close ();
Return "";
}
}
}
}

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.