First, the reading of barcodes
If you use the keyboard port-based sweeping tool, you will know that it is basically no need to program and process as the number key is pressed on the keyboard. But if you use other interfaces, you may have to write a communication code for the device. The following is a simple 25-pin serial bar code reader communication code. Option Explicitdim SDATA AS STRINGPRIVATE SUB FORM_LOAD () with mscomm1 .commport = 3 'is set to COM3, and you can provide a COMBox to choose from. .Petopen = true 'Open Communication Port End Withend Subprivate Sub Mscomm1_oncomm ()
DIM ENDPOS AS INTEGERSELECT CASE MSCOS ASETELECT CASE MSCOMM1.COMMEVENTCASE COMEVRECEVE 'When there is a data transfer, SDATA = SDATA & TRIM (MSCOMM1.INPUT)' retrieves the carriage return, usually the card is ended back to a carriage return as a endpos = INSTR (1, SDATA, CHR (13)) if endpos = 0 Then 'If not end, continue to work hard Else' to read a group. lblbarcode.caption = SDATA 'Display a set of barcode with lstbarcode.additem MID (SDATA, 1, ENDPOS - 1)' Add a set of barcods to the list End withsdata = "" = End IFEND SELECTEND SUB
PRIVATE SUB CMDEND_CLICK () MSComm1.Portopen = false 'Close Port end
Second, the production of barcodes is not difficult to program the program on VB. The following is also easy to understand about the code generated by barcodes, just use a Barcode control with an Office to easily print out 11 different standard barcodes, enough to meet our requirements. I remembered a book in a book on my book with Turbo C to write a barcode print program article, a long story, I didn't know when I saw N days, I didn't have any satisfaction, and now I am so happy :). After the words, I have to return to the topic. And see the code generated by the barcode and the instructions. The source code is mainly composed of two forms (FRMMAIN main forms and FRMOPTION barcode settings forms) and two modules (Modgetscreen.bas, Sysdlg32.bas). Taking into account the space, only some of the critical code is listed here. Create a standard project to add a barcode component named (Microsoft Access Barcode Control9) and add a bar code control to the window and rename the window to frmmain, as shown. Due to more controls, this is inconvenient here, please see the source code. Module ModgetScreen.bas code as follows: Option Explicit Declaration Bitblt, getDesktopWindow, getWindowdc, releasedc These API functions public reguser as boolean
Sub getobjimage1 (Obj As Object, Owner AS Picturebox, Picture1 As Picturebox) 'HDCDIM HWNDDESK AS LONGDIM HDCDESK AS Long' Area Expression Variable DIM X AS Longdim Y AS Longdim W AS Longdim H AS Long
x = Obj.Left Screen.TwipsPerPixelXy = Obj.Top Screen.TwipsPerPixelYw = Obj.Width Screen.TwipsPerPixelXh = Obj.Height Screen.TwipsPerPixelYhDCDesk = OwnerForm.hdc 'taken image Call BitBlt (Picture1.hdc, 0, 0, w, h , HDCDESK, X, Y, VBSRCPY) CALL ReleaseDC (HWNDDESK, HDCDESK) End Sub
The main form frmmain.frm part of the code is as follows: private subdprint_click () Generates a barcode image DIM R As long, i as integer, t as string, cfile as string 'temporary variable T = Barcodefor I = 0 To Val (Times) - 1
Barcode1.Value = Barcode iDOEvents Picture1.refresh
Getobjimage1 Barcode1, Conel, Picture1
If reguser = false the 'If not registered Add Mask tag Picture1.PaintPicture Picture2.Picture, 300, 300nd IF
If Dir (SavePath, Vbdirectory = "" "" "" "," ""
Cfile = SavePath & Barcode1.Value & ".bmp"
SavePicture Picture1.Image, cfile 'Save the barcode as an image file to print nextbarcode = t
End Sub
Barcode setting form frMoption.frm code as follows: Option Explicit 'Barcode Setup Module
Private sub cbobig_click () barcode1.style = CBOBIG.LISTINDEX 'Change Standard End Sub
PRIVATE SUB CBODIRECTION_CLICK () Barcode1.direction = CBODIRECTION.LISTINDEX 'Change direction END SUB
PRIVATE SUB CBOLINE_CLICK () Barcode1.LineWeight = CBOLINE.LISTINDEX 'Change Line Wide End Sub
PRIVATE SUB CBOSMALL_Click () barcode1.substyle = cbosmall.listindex 'change styles End Sub
PRIVATE SUB CHECK1_CLICK () Barcode1.showdata = check1.value 'Whether to display data END SUB
Private Sub Cmdchange_Click () 'Sets long, wide size barwidth = barcode1.heightbarHeight = Barcode1.widthcmdrefresh_clickend Sub
Private Sub cmdOK_Click () 'transmitted to the main interface barcode configuration With frmMain.BarCode1.LineWeight = BarCode1.LineWeight.Style = BarCode1.Style.SubStyle = BarCode1.SubStyle.Direction = BarCode1.Direction.Width = BarCode1.Width.Height = BarCode1.Height.ShowData = BarCode1.ShowDataMe.HideEnd WithWith frmMain.Picture1.Width = .BarCode1.Width.Picture1.Height = .BarCode1.Height.Conel.Width = .BarCode1.Width.Conel.Height = .BarCode1.HeightEnd WithEnd Subprivate Sub Cmdrefresh_Click () Barcode1.Width = Barwidthbarcode1.Height = Barheightens Sub
Private Sub Form_Load () loadbarinfobarwidth = barode1.widthbarheight = barcode1.heightens Sub
Sub LoadBarInfo () 'initialization options LoadBigClass cboBigLoadSmallClass cboSmallLoadLineSize cboLineLoadDirection cboDirectionEnd SubSub LoadBigClass (cbo As ComboBox)' standard barcode With cbo.AddItem "UPC-A" .AddItem "UPC-E" .AddItem "EAN-13" .AddItem "EAN- 8 ".additem" Case Code ".additem" Codabar (NW-T) ". AddItem" Code-39 ".additem" CODE-128 ".additem" US PostNet ".addItem" US Postal Fim ".additem" JP POST ".ListIndex = 2End WithEnd SubSub LoadSmallClass (cbo As ComboBox) 'style barcode With cbo.AddItem" Standard ".AddItem" 2-Digit Supplement ".AddItem" 5-Digit Supplement ".AddItem" POS Case Code ".ListIndex = 0End WITHEND SUB Many people want to add barcode function to strengthen work efficiency, especially sales management, and library management, but because bar code technology is difficult to master, and who is more In the past, various reasons have made many people. This article introduces a simple and practical barcode solution, hoping to help you improve the functionality of the software system. ?
Two methods of printing barcodes in the VB application
Barcode as a machine-recognizable graphic, it can quickly and accurately identify some kind of product or commodity, which works very important in many database applications, such as supermarket cash register, station ticketing and other occasions. When the barcode on some item is properly interpreted by the barcode scanner, the unique identification string of the item will be obtained. We can easily know some of its other properties and processed by retrieving the database. Although many free and free barcode print controls can be found on the Internet, these controls have a biggest shortcomings in addition to inconvenience: their printouts cannot have a print page with our programs, such as In one process, let's output some content to the system printer, then call the control barcode print method, the last print result is two pages! If we want to handle a ticket now, the above will not only print the barcode, but also have information such as terminal and fare, then the control is not available. For programmers, it is possible still to understand the principle of barcode printing, this paper proposes two printing methods and peers. First, directly utilize printers with barcode printing function
There are many printers to print barcodes directly, but in VB, our Lprint statement we are familiar with the DOS era can no longer be used, the print operation is fully taken over by Windows SPOOL system, and the output is in "page", all printouts Both are converted to the print driver by Windows to the graphic. To make the printer, you must send the corresponding ESC sequence directly to it, so we must find a way to avoid the Windows spool system, that is, in the reproduction program cannot use the Printer object and the Printers collection to process printout, in VB In order to send the ESC instruction to the printer, there are at least three methods. The first two methods are to call the Windows API function: escape () and spoolfile (), the third is the easiest way: Open the printer port for binary access, we Mainly considering this method.
Even in the Windows era, "LPT1:" and "PRN" are still available, let's make a test: Open a DOS window, enter the Copy Con lt1: Enter, then enter some characters, then press F6 keys, the printer starts working, it will print out the characters you entered! The following code demonstrates direct instructions and characters directly to the printer:
Private submmand1_click ()
DIM STROUT AS STRING
Strout = "This is the string that is sent directly to the printer port"
'Open the printer port, "LPT1:" may need to change according to your printer settings
Open "LPT1:" for binary access write "for binary access Write as # 1
'Send to the printer, pay attention to the last parameter of the statement must be variables
PUT # 1, Strout
'Close the printer port
Close # 1
End Sub
The instructions of various printer print barcodes may vary, such as assigning the above variable strout to:
Strout = CHR (28) & "P" & CHR (5) & chr (2) & chr (3) & chr (3) & chr (6) & "012345"
Barcode of the Code39 format of content "012345" will be printed on the Ar2400 printer. Specific print control instructions, please refer to the printer manual.
Disadvantages of this method: First, the printer that relies on the printer itself, and the printer with barcode printing is usually expensive than ordinary printers, which will make the construction application system is not economical; the second is that all printouts must be handled, For example, print positioning is very wasting. Second, use the drawing mode to output to ordinary printers
The coding rules of the barcode are nothing more than a wide narrow difference between the lines and the inter-line gaps. As long as we understand the coding rules of the barcode, it can be accepted on a normal printer in a normal printer in a normal printer as long as we understand the coding rules of the barcode. Below we use the most common Code39 code to discuss.
The coding rule of the Code39 code is:
1, each five lines represent a character;
2, thick line representation 1, thin line means 0;
3, the gap width of the lines 1, narrow representation 0;
4, the five lines plus the four gaps between them are nine binary codes, and this nine-digit must have a three-bit 1, so it is called 39 yards;
5, the first end of the barcode, one * identification start and end
In our program, the commonly used characters are encoded. When interpreting, take the line thick, and then take a narrow gap, such as:
The characters in the above picture can be interpreted as 001101000, character 3 interpretation is 110000100
Below is the child process we give:
Note: Output barcodes corresponding to the string strbarcode to the default printer
Private sub printbarcode
Byval strbarcode as string, _
Optional byval Intxpos as integer = 0, _
Optional byval intYpos as integer = 0, _
Optional Byval Intprintheight as integer = 10, _
Optional Byval BolprintText As Boolean = True_
)
Note: Parameter Description:
Note: Strbarcode - Barcode string to be printed
Note: INTXPOS, INTYPOS - Print the upper left corner coordinate of the barcode (default (0,0), coordinate scale: mm)
Note: INTHEIGHT - print height (default is one centimeter, coordinate scale: mm)
Note: BOLPRINTTEXT - Whether to print artificial identification characters (default TRUE)
Note: "0" - "9", "A-Z", "-", "%", "$" and "*" bar code encoding format, total 40 characters
Static strbartable (39) AS STRING
Note: Initializing barcode encoding format table
Strbartable (0) = "001100100" Note: 0
Strbartable (1) = "100010100" Note: 1
Strbartable (2) = "010010100" Notes: 2
Strbartable (3) = "110000100" Note: 3
Strbartable (4) = "001010100" Note: 4
Strbartable (5) = "101000100" Note: 5Strbartable (6) = "011000100" Note: 6
Strbartable (7) = "000110100" Note: 7
Strbartable (8) = "100100100" Note: 8
Strbartable (9) = "010100100" Note: 9
Strbartable (10) = "100010010" Note: a
Strbartable (11) = "010010010" Note: B
Strbartable (12) = "110000010" Note: c
Strbartable (13) = "001010010" Note: d
Strbartable (14) = "101000010" Notes: e
Strbartable (15) = "011000010" Note: f
Strbartable (16) = "000110010" Note: g
Strbartable (17) = "100100010" Note: h
Strbartable (18) = "010100010" Note: i
Strbartable (19) = "001100010" Note: j
Strbartable (20) = "100010001" Note: k
Strbartable (21) = "010010001" Note: L
Strbartable (22) = "110000001" Note: m
Strbartable (23) = "001010001" Note: n
Strbartable (24) = "101000001" Note: O
Strbartable (25) = "011000001" Note: P
Strbartable (26) = "000110001" Note: Q
Strbartable (27) = "100100001" Note: r
Strbartable (28) = "010100001" Note: s
Strbartable (29) = "001100001" Note: T
Strbartable (30) = "100011000" Note: U
Strbartable (31) = "010011000" Note: V
Strbartable (32) = "110001000" Note: w
Strbartable (33) = "001011000" Note: x
Strbartable (34) = "101001000" Note: Y
Strbartable (35) = "011001000" Note: zstrbartable (36) = "000111000" Note: -
Strbartable (37) = "100101000" Notes:%
Strbartable (38) = "010101000" Notes: $
Strbartable (39) = "001101000" Note: *
If strbarcode = "" "The EXIT SUB Note: Do not print an empty string
Note: Save the printer scalemode
DIM INTOLDSCALEMODE As Scalemodeconstants
INTOLDSCALEMODE = Printer.Scalemode
Note: Save the printer DrawWidth
DIM INTOLDDRAWIDTH AS INTEGER
INTOLDDRAWWIDTH = Printer.drawWidth
Note: Save the printer font
DIM FNTOLDFONT AS stdfont
Set fntoldfont = printer.font
Printer.scalemode = VBTWIPS Note: Set the coordinate scale set for printing (TWIP = 1)
Printer.drawWidth = 1 Note: The line width is 1
Printer.FontName = "Song" Note: Print the font and size of characters below the barcode
Printer.fontsize = 10
DIM STRBC AS STRING Note: Barcode string to print
Strbc = ucase (strbarcode)
Note: The X coordinate expressed in millimeters is expressed as
DIM X as integer
x = Printer.scaleX (INTXPOS, VBMillimeters, VBTWIPS)
Note: The Y coordinate conversion indicated by millimeters is expressed as
DIM Y AS Integer
Y = printer.scaley (intYPOS, VBMillimeters, VBTWIPS)
Note: The height of the mime is converted to the either
DIM INTHEIGHT AS INTEGER
INTHEIGHT = Printer.Scaley (intprintheight, vbmillimeters, vbtwips)
Note: Whether to print artificial identification characters below the barcode
If BolprintText = True Then
Note: Barcode printing height to subtract the following characters display height
INTHEIGHT = INTHEIGHT - Printer.TextHeight (strb)
END IF
Const IntWidthcu As INTEGER = 30 Note: Thick and wide gap width
Const IntWidthxi as Integer = 10 Note: Thin wire and narrow gap width
DIM INTINDEX AS INTEGER Note: Currently processed string index
DIM I AS INTEGER, J AS INTEGER, K AS INTEGER Note: Cyclic Control Variables
Note: Add start character
IF LEFT (strbc, 1) <> "*" THEN
Strbc = "*" & strbcendiff
Note: Add end characters
IF Right (strbc, 1) <> "*" THEN
Strbc = strbc & "*"
END IF
Note: Loop processing the barcode characters you want to display
For i = 1 to Len (strbc)
Note: Determine the index of the current characters in strbartable
SELECT CASE MID (STRBC, I, 1)
Case "*"
INTINDEX = 39
Case "$"
INTINDEX = 38
Case "%"
INTINDEX = 37
Case "-"
INTINDEX = 36
Case "0" to "9"
INTINDEX = CINT (MID (STRBC, I, 1))
Case "a" to "z"
INTINDEX = ASC (MID (Strbc, I, 1)) - ASC ("A") 10
Case Else
MsgBox "The barcode string to be printed contains invalid characters! Current version only supports character comments: 0 Note: - Note: 9 Note:, Note: A Note: - Note: z Comment:, Note: - Note:, Note: % Notes:, Note: $ Note: And Note: * Note: "
End SELECT
Note: Whether to print artificial identification characters below the barcode
If BolprintText = True Then
Printer.currentx = x
Printer.currenty = Y intheight
Printer.print MID (Strbc, I, 1)
END IF
For j = 1 to 5
Note: Draw thin line
IF MID (Strbartable (IntIndex), J, 1) = "0" THEN
Fork = 0 to intWidthxi - 1
Printer.Line (X K, Y) -Step (0, INTHEIGHT)
Next K
X = x intWidthxi
Note: Draw a wide line
Else
Fork = 0 to intWidthcu - 1
Printer.Line (X K, Y) -Step (0, INTHEIGHT)
Next K
X = x intWidthcu
END IF
Note: A narrow gap between each character barcode
IF j = 5 THEN
X = x intWidthxi * 3
EXIT for
END IF
Note: Narrow gap
IF MID (Strbartable (intIndex), J 5, 1) = "0" THEN
X = x intWidthxi * 3
Note: Wide gap
Else
X = x intWidthcu * 2
END IF
NEXT J
Next i
Note: Restore the printer scalemode
Printer.scalemode = Intoldscalemode
Note: Recovery Printer DrawWidthprinter.drawWidth = IntoldDrawWidth
Note: Restore the printer font
Set printer.font = fntoldfont
End Sub's ideal case is to make it a control, providing a print method in the control, this method is implemented
The process is roughly the same, but cannot use VB's Printer objects directly in the control, otherwise VB will process your printout in the control as a separate page, but should pass Printer.hdc to it, by calling those need Specifies the HDC's Windows API function to implement the print output of the container on one page, such as we can define this control method:
Note: The Printit method outputs the corresponding barcode to the default printer
Public Sub Printit (Byval Printdc As Long, _
Optional byval Intxpos as integer = 0, _
Optional byval intYpos as integer = 0, _
Optional byval INTPRINTHEIGHT AS INTEGER = 10)
Since the Printer object cannot be used, the line and output text cannot use the line and print method of the Printer object, at least the following three Windows API functions in our program:
'Mobile brush position
Private Declare Function MoveToex Lib "GDI32" (Byval X As Long, Byval Y As Long, LPPOINT AS POINTAPI) AS Long
'Draw a line from the current position of the brush to (x, y)
Private Declare Function Lineto Lib "GDI32" (Byval X as long, Byval Y as long) As long
'Output a string at (x, y)
Private Declare Function TextOut Lib "GDI32" Alias "Textouta" (Byval HDC As Long, Byval X As Long, Byval Y As Long, BYVAL NCOUNT AS Long) AS Long
The parameters required by 'MoveToex () function
Private Type PointApi
XP as long
YP as long
End Type
DIM PAPI as PointApi
Painted line operation is (original Printer.Line function):
MoveToex PrintDC, X K, Y, PAPI
LINETO PrintDC, X K, Y INTHEIGHT 1
Print characters (original Printer.print function):
Textout PrintDC, X, Y INTHEIGHT, MID (Strbc, i 1, 1), 1