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 "";
}
}
}
}