home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / EAN_Barcod215118512009.psc / clsEANBarCodes.cls < prev    next >
Text File  |  2009-05-02  |  51KB  |  1,293 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsEANBarCodes"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Option Explicit
  15.  
  16. ' *************************************************************************
  17. ' PROJECT:  EAN/UPC Barcode Module
  18. ' AUTHOR:   Milton Neal, Perth Western Australia. miltonneal@arach.net.au.
  19. ' DATE:     1st May 2009
  20. ' VERSION:  1.00
  21. ' UPDATES:
  22. ' *************************************************************************
  23.  
  24. Public Enum eSymbology
  25.     EAN13 = 1
  26.     EAN13_2 = 2
  27.     EAN13_5 = 3
  28.     EAN8 = 4
  29.     EAN8_2 = 5
  30.     EAN8_5 = 6
  31.     UPCA = 7
  32.     UPCA_2 = 8
  33.     UPCA_5 = 9
  34.     UPCE = 10
  35.     UPCE_2 = 11
  36.     UPCE_5 = 12
  37. End Enum
  38.  
  39. 'Dim HRbartext As String
  40.  
  41. Dim BC_ValidCharacters As String * 10
  42. Dim EANUPC_LHO(9) As String
  43. Dim EANUPC_LHE(9) As String
  44. Dim EANUPC_RH(9) As String
  45. Dim EANUPC_Parity(9) As String
  46. Dim UPCe_Parity(9) As String
  47. Dim Plus2_Parity(3) As String
  48. Dim Plus5_Parity(9) As String
  49.  
  50. Private cHDC As Long                'Handle to the output device
  51. Private cSymbology As eSymbology    'Barcode symbology to use
  52. Private cBarX As Single             'Width of the bar in printer pixels
  53. Private cBarMultiplier As Integer   'Bar width multiplier
  54. Private cBarColor As OLE_COLOR      'Color to draw the barcode
  55. Private cBarCodeData As String      'Barcode data
  56. Private cBarRotation As Integer     'Direction to print/display barcode
  57. Private cBarCodeTop As Long         'Top start position in pixels
  58. Private cBarCodeLeft As Long        'Left start position in pixel
  59. Private cBarCodeHeight As Long      'Height of the bars (not including text)in pixels
  60. Private cBarTextGap As Long         'Gap between the barcode and the HR text in pixels
  61.  
  62. Private cHumanReadable As Boolean           'Human readable or barcode only
  63. Private cBarTextFont As UDT_BarTextFont     'Holds the font information
  64. Private cAutoFontSize As Boolean            'Auto adjest the HR test font size to fit
  65.  
  66. Dim BarStartX As Long           'X point to start barcode
  67. Dim BarStartY As Long           'Y point to start barcode
  68. Dim NarrowBar As Single         'Narrow bar
  69. Dim WideBar As Single           'Wide bar
  70. Dim GuardBarHeight As Long      'Height of the gaurd bars in pixels
  71. Dim BarCodeWidth As Long        'Width of barcode in pixels
  72. Dim SupplementWidth As Long     'Width of the supplemental barcode in pixels
  73. Dim SupplementGap As Long       'Gap between the barcode and supplemental in pixels
  74. Dim NumberOfBars As Integer     'Number of bars between the guard bars
  75. Dim BarBit As String * 1        'Holds the "width" value of a bar
  76.  
  77. Dim UPCAOffset As Integer       'Offeset for drawing UPCA HR text
  78. Dim UPCEOffset As Integer       'Offset for drawing UPCE HR check digit
  79. Dim TextGap As Integer          'Size of the gap between the guard bars for the HR text(pixels)
  80. Dim TextStartX As Long          'X point for text
  81. Dim TextStartY As Long          'Y point for text
  82. Dim HRTextHeight As Long        'HR text height in pixels
  83. Dim HRTextWidth As Long         'HR text width in pixels
  84. Dim LeftDigitWidth As Long      'Width of the left character in pixels
  85. Dim RightDigitWidth As Long     'Width of the right character in pixels (for UPC only)
  86. Dim HRLeftDigit As String       'Character left of start guard bar
  87. Dim HRRightDigit As String      'Character right of end guard bar
  88. Dim HRLeftText As String        'Left side HR text
  89. Dim HRRightText As String       'Right side HR text
  90. Dim HRSupplementText As String  'HR text for the supplemental
  91. Dim SupplementData As String    'Holds the encoded data for the supplental barcode
  92. Dim EncodedData As String       'Holds the encoded data for creating barcode
  93.  
  94. 'Private Const BARSPERCHAR As Integer = 7
  95.  
  96. Private cErrorNumber As Long
  97. Private cErrDescription As String
  98. Const B_ErrNoData As Long = 6940
  99. Const S_ErrNoData As String = "No barcode data specified."
  100. Const B_ErrSymbology As Long = 6950
  101. Const S_ErrSymbology As String = "Invalid or no symbology selected."
  102. Const B_ErrDraw As Long = 6960
  103. Const S_ErrDraw As String = "Error generating barcode."
  104. Const B_ErrInvalidChar As Long = 6970
  105. Const S_ErrInvalidChar As String = "Invalid character in the barcode."
  106. Const B_ErrInvalidLength As Long = 6980
  107. Const S_ErrInvalidLength As String = "Invalid code length for symbology."
  108. Const B_ErrInvalidControl As Long = 6990
  109. Const S_ErrInvalidControl As String = "Invalid control."
  110.  
  111. '===============================================
  112. 'PROPERTIES
  113. '===============================================
  114.  
  115. Public Property Let BarcodeOutput(Dest As Object)
  116.     On Error GoTo err
  117.     cHDC = Dest.hdc
  118.     On Error GoTo 0
  119.     Exit Property
  120. err:
  121.     cErrorNumber = B_ErrInvalidControl
  122.     cErrDescription = S_ErrInvalidControl
  123.     err.Raise cErrorNumber, "clsEANBarCode.BarcodeOutput", cErrDescription
  124. End Property
  125.  
  126. Public Property Get Symbology() As eSymbology
  127.     Symbology = cSymbology
  128. End Property
  129. Public Property Let Symbology(bSym As eSymbology)
  130.     cSymbology = bSym
  131. End Property
  132.  
  133. Public Property Get BarXFactor() As Single
  134.     BarXFactor = cBarX
  135. End Property
  136. Public Property Let BarXFactor(xbWidth As Single)
  137.     cBarX = xbWidth
  138. End Property
  139.  
  140. Public Property Get BarMultiplier() As Integer
  141.     BarMultiplier = cBarMultiplier
  142. End Property
  143. Public Property Let BarMultiplier(bMulti As Integer)
  144.     If bMulti > 10 Then bMulti = 10
  145.     If bMulti < 1 Then bMulti = 1
  146.     cBarMultiplier = bMulti
  147. End Property
  148.  
  149. Public Property Get BarRotation() As Integer
  150.     BarRotation = cBarRotation
  151. End Property
  152. Public Property Let BarRotation(bRotation As Integer)
  153.     If bRotation <> 0 And bRotation <> 90 _
  154.     And bRotation <> 180 And bRotation <> 270 Then
  155.         cBarRotation = 0
  156.     Else: cBarRotation = bRotation
  157.     End If
  158. End Property
  159.  
  160. Public Property Get BarCodeData() As String
  161.     BarCodeData = cBarCodeData
  162. End Property
  163.  
  164. Public Property Let BarCodeData(bcData As String)
  165.     Dim iCntr As Integer
  166.     Dim sDataChar As String
  167.     Dim iAscValue As Integer
  168.     
  169.     bcData = Trim(bcData)
  170.     
  171.     For iCntr = 1 To Len(bcData)
  172.         sDataChar = Mid(bcData, iCntr, 1)
  173.         If InStr(1, BC_ValidCharacters, sDataChar) = 0 Then
  174.             cErrorNumber = B_ErrInvalidChar
  175.             cErrDescription = S_ErrInvalidChar
  176.             err.Raise cErrorNumber, "clsEANBarCode.BarCodeData", cErrDescription
  177.             Exit Property
  178.         End If
  179.     Next iCntr
  180.     Select Case cSymbology
  181.         Case EAN13
  182.             If Len(bcData) <> 12 Then GoTo Raise_Error
  183.             EncodeEAN13 bcData
  184.             
  185.         Case EAN13_2
  186.             If Len(bcData) <> 14 Then GoTo Raise_Error
  187.             EncodeEAN13 Mid(bcData, 1, 12)
  188.             EncodePlus2 Right(bcData, 2)
  189.             
  190.         Case EAN13_5
  191.             If Len(bcData) <> 17 Then GoTo Raise_Error
  192.             EncodeEAN13 Mid(bcData, 1, 12)
  193.             EncodePlus5 Right(bcData, 5)
  194.             
  195.         Case EAN8
  196.             If Len(bcData) <> 7 Then GoTo Raise_Error
  197.             EncodeEAN8 bcData
  198.             
  199.         Case EAN8_2
  200.             If Len(bcData) <> 9 Then GoTo Raise_Error
  201.             EncodeEAN8 Mid(bcData, 1, 7)
  202.             EncodePlus2 Right(bcData, 2)
  203.             
  204.         Case EAN8_5
  205.             If Len(bcData) <> 12 Then GoTo Raise_Error
  206.             EncodeEAN8 Mid(bcData, 1, 7)
  207.             EncodePlus5 Right(bcData, 5)
  208.             
  209.         Case UPCA
  210.             If Len(bcData) <> 11 Then GoTo Raise_Error
  211.             EncodeUPCA bcData
  212.             
  213.         Case UPCA_2
  214.             If Len(bcData) <> 13 Then GoTo Raise_Error
  215.             EncodeUPCA Mid(bcData, 1, 11)
  216.             EncodePlus2 Right(bcData, 2)
  217.             
  218.         Case UPCA_5
  219.             If Len(bcData) <> 16 Then GoTo Raise_Error
  220.             EncodeUPCA Mid(bcData, 1, 11)
  221.             EncodePlus5 Right(bcData, 5)
  222.             
  223.         Case UPCE
  224.             If Len(bcData) <> 6 Then GoTo Raise_Error
  225.             EncodeUPCE bcData
  226.             
  227.         Case UPCE_2
  228.             If Len(bcData) <> 8 Then GoTo Raise_Error
  229.             EncodeUPCE Mid(bcData, 1, 6)
  230.             EncodePlus2 Right(bcData, 2)
  231.             
  232.         Case UPCE_5
  233.             If Len(bcData) <> 11 Then GoTo Raise_Error
  234.             EncodeUPCE Mid(bcData, 1, 6)
  235.             EncodePlus5 Right(bcData, 5)
  236.     End Select
  237.     cBarCodeData = bcData
  238.     Exit Property
  239.     
  240. Raise_Error:
  241.     cErrorNumber = B_ErrInvalidLength
  242.     cErrDescription = S_ErrInvalidLength
  243.     err.Raise cErrorNumber, "clsEANBarCode.BarCodeData", cErrDescription
  244. End Property
  245.  
  246. Public Property Get BarColor() As OLE_COLOR
  247.     BarColor = cBarColor
  248. End Property
  249.  
  250. Public Property Let BarColor(bColor As OLE_COLOR)
  251.     cBarColor = bColor
  252. End Property
  253.  
  254. Public Property Get BarCodeX() As Long
  255.     BarCodeX = cBarCodeLeft
  256. End Property
  257.  
  258. Public Property Let BarCodeX(X As Long)
  259.     cBarCodeLeft = X
  260. End Property
  261.  
  262. Public Property Get BarCodeY() As Long
  263.     BarCodeY = cBarCodeTop
  264. End Property
  265.  
  266. Public Property Let BarCodeY(Y As Long)
  267.     cBarCodeTop = Y
  268. End Property
  269.  
  270. Public Property Get BarcodeHeight() As Long
  271.     BarcodeHeight = cBarCodeHeight
  272. End Property
  273.  
  274. Public Property Let BarcodeHeight(bHeight As Long)
  275.     If bHeight < 8 Then bHeight = 8
  276.     cBarCodeHeight = bHeight
  277.     GuardBarHeight = bHeight + (bHeight / 10)
  278. End Property
  279.  
  280. Public Property Get BarTextGap() As Long
  281.     BarTextGap = cBarTextGap
  282. End Property
  283.  
  284. Public Property Let BarTextGap(bGap As Long)
  285.     cBarTextGap = bGap
  286. End Property
  287.  
  288. Public Property Get HRText() As Boolean
  289.     HRText = cHumanReadable
  290. End Property
  291.  
  292. Public Property Let HRText(bReadable As Boolean)
  293.     cHumanReadable = bReadable
  294. End Property
  295.  
  296. Public Property Get AutoTextFont() As Boolean
  297.     AutoTextFont = cAutoFontSize
  298. End Property
  299. Public Property Let AutoTextFont(bAutoFS As Boolean)
  300.     cAutoFontSize = bAutoFS
  301. End Property
  302.  
  303. Public Property Get TotalBarWidth() As Long
  304.     Dim BarH As Integer
  305.     
  306.     If cBarCodeData = "" Then
  307.         cErrorNumber = B_ErrNoData
  308.         cErrDescription = S_ErrNoData
  309.         err.Raise cErrorNumber, "clsEANBarCode.BarWidth", cErrDescription
  310.         Exit Property
  311.     End If
  312.    Call GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  313.     Select Case cBarRotation
  314.         Case 0, 180
  315.             TotalBarWidth = BarCodeWidth
  316.         Case 90, 270
  317.             If cHumanReadable = True Then
  318.                 BarH = cBarCodeHeight + HRTextHeight + cBarTextGap
  319.                 TotalBarWidth = IIf(BarH > GuardBarHeight, BarH, GuardBarHeight)
  320.             Else: TotalBarWidth = GuardBarHeight
  321.             End If
  322.     End Select
  323. End Property
  324.  
  325. Public Property Get TotalBarHeight() As Long
  326.     Dim BarH As Integer
  327.     
  328.     If cBarCodeData = "" Then
  329.         cErrorNumber = B_ErrNoData
  330.         cErrDescription = S_ErrNoData
  331.         err.Raise cErrorNumber, "clsEANBarCode.BarWidth", cErrDescription
  332.         Exit Property
  333.     End If
  334.     Call GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  335.     Select Case cBarRotation
  336.         Case 0, 180
  337.             If cHumanReadable = True Then
  338.                 BarH = cBarCodeHeight + HRTextHeight + cBarTextGap
  339.                 TotalBarHeight = IIf(BarH > GuardBarHeight, BarH, GuardBarHeight)
  340.             Else: TotalBarHeight = GuardBarHeight
  341.             End If
  342.         Case 90, 270
  343.             TotalBarHeight = BarCodeWidth
  344.     End Select
  345. End Property
  346.  
  347. '=====================================
  348. 'PUBLIC FUNCTIONS
  349. '=====================================
  350.  
  351. Public Function BarTextFont(bFont As String, bFontSize As Single, bFontBold As Boolean, bFontItalic As Boolean)
  352.     If bFont = "" Then
  353.         bFont = cBarTextFont.FontName
  354.         bFontSize = cBarTextFont.FontSize
  355.         bFontBold = cBarTextFont.FontBold
  356.         bFontItalic = cBarTextFont.FontItalic
  357.     Else
  358.         cBarTextFont.FontName = bFont
  359.         cBarTextFont.FontSize = bFontSize
  360.         cBarTextFont.FontBold = bFontBold
  361.         cBarTextFont.FontItalic = bFontItalic
  362.     End If
  363. End Function
  364.  
  365. Public Function DrawBarCode()
  366. If cHDC = 0 Then Exit Function
  367.     'Check that a symbology has been set
  368.     If cSymbology < 1 Or cSymbology > 12 Then
  369.         cErrorNumber = B_ErrSymbology
  370.         cErrDescription = S_ErrSymbology
  371.         err.Raise cErrorNumber, "clsEANBarCode.Draw_BarCode", cErrDescription
  372.         Exit Function
  373.     End If
  374.     'Check that the barcode data has been set
  375.     If cBarCodeData = "" Then
  376.         cErrorNumber = B_ErrNoData
  377.         cErrDescription = S_ErrNoData
  378.         err.Raise cErrorNumber, "clsEANBarCode.BarWidth", cErrDescription
  379.         Exit Function
  380.     End If
  381.     
  382.     Select Case cBarRotation
  383.         Case 0
  384.             Draw_Normal
  385.         Case 90
  386.             Draw_90
  387.         Case 180
  388.             Draw_180
  389.         Case 270
  390.             Draw_270
  391.     End Select
  392. Exit Function
  393.  
  394. Err_Handler:
  395.     cErrorNumber = B_ErrInvalidControl
  396.     cErrDescription = S_ErrInvalidControl
  397.     err.Raise cErrorNumber, "clsEANBarCode.Draw_BarCode", cErrDescription
  398. End Function
  399.  
  400. '======================================
  401. 'PRIVATE ROUTINES
  402. '======================================
  403.  
  404. Private Sub Draw_Normal()
  405.  
  406.     Dim NextBar As Single       'Postion to start the next bar
  407.     Dim BarH As Integer         'Bar height
  408.     Dim iCntr As Integer        'Loop counter
  409.     Dim bColor As OLE_COLOR     'Current bar color
  410.     Dim rtn As Long
  411.     
  412.     'DRAW THE BARCODE
  413.     '================
  414.     'Get the current height and width of the HR text
  415.     rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  416.     If rtn = 0 Then GoTo Raise_Error
  417.     BarH = cBarCodeHeight
  418.     BarStartX = cBarCodeLeft: BarStartY = cBarCodeTop
  419.     bColor = vbWhite
  420.     NextBar = BarStartX + LeftDigitWidth
  421.     For iCntr = 1 To Len(EncodedData)
  422.         'Toggle the color
  423.         If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  424.         BarBit = Mid(EncodedData, iCntr, 1)
  425.         Select Case BarBit
  426.             '"T" & "S" are embedded in the encoded data and are used to toggle between bar heights
  427.             Case "T"
  428.                 BarH = GuardBarHeight
  429.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  430.                 
  431.             Case "S"
  432.                 BarH = cBarCodeHeight
  433.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  434.                 
  435.             'Narrow Bar
  436.             Case "1"
  437.                 If bColor <> vbWhite Then
  438.                     rtn = DrawBar(cHDC, CLng(NextBar), BarStartY, CLng(NextBar + NarrowBar), BarStartY + BarH, bColor)
  439.                     If rtn = 0 Then GoTo Raise_Error
  440.                 End If
  441.                 NextBar = NextBar + NarrowBar
  442.  
  443.               'Wide Bar
  444.             Case "2", "3", "4"
  445.                 WideBar = NarrowBar * BarBit
  446.                 If bColor <> vbWhite Then
  447.                     rtn = DrawBar(cHDC, CLng(NextBar), BarStartY, CLng(NextBar + WideBar), BarStartY + BarH, bColor)
  448.                     If rtn = 0 Then GoTo Raise_Error
  449.                 End If
  450.                 NextBar = NextBar + WideBar
  451.             'Gap for supplement 2 or supplement 5
  452.             Case "9"
  453.                 NextBar = NextBar + SupplementGap
  454.                 BarStartY = cBarCodeTop + HRTextHeight
  455.                 BarH = GuardBarHeight - HRTextHeight
  456.         End Select
  457.     Next iCntr
  458.     
  459.    'DRAW THE HUMAN READABLE TEXT
  460.    '============================
  461.     If cHumanReadable Then
  462.         TextStartY = cBarCodeTop + cBarCodeHeight + cBarTextGap
  463.         TextStartX = cBarCodeLeft
  464.         'Draw the first digit left of the left guard bars
  465.         If HRLeftDigit <> "" Then
  466.             rtn = DrawBarText(cHDC, TextStartX, TextStartY - 3, cBarTextFont, cBarColor, cBarRotation, HRLeftDigit)
  467.             If rtn = 0 Then GoTo Raise_Error
  468.         End If
  469.         'Draw the left hand text right of the left guard bars
  470.         TextStartX = TextStartX + LeftDigitWidth + ((3 + UPCAOffset) * NarrowBar)
  471.         rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  472.         'If rtn = 0 Then GoTo Raise_Error
  473.         rtn = DrawBarText(cHDC, TextStartX + ((TextGap - HRTextWidth) / 2), TextStartY, cBarTextFont, _
  474.                             cBarColor, cBarRotation, HRLeftText)
  475.         If rtn = 0 Then GoTo Raise_Error
  476.         'Draw the right hand text right of the centre guard bars
  477.         If HRRightText <> "" Then
  478.             TextStartX = TextStartX + TextGap + (5 * NarrowBar)
  479.             rtn = GetTextSize(cHDC, cBarTextFont, HRRightText, HRTextWidth, HRTextHeight)
  480.             If rtn = 0 Then GoTo Raise_Error
  481.             rtn = DrawBarText(cHDC, TextStartX + ((TextGap - HRTextWidth) / 2), TextStartY, cBarTextFont, _
  482.                             cBarColor, cBarRotation, HRRightText)
  483.             If rtn = 0 Then GoTo Raise_Error
  484.         End If
  485.         'Draw the check digit to the right of the right hand guard bars (UPC only)
  486.         If HRRightDigit <> "" Then
  487.             TextStartX = TextStartX + TextGap + ((3 + UPCEOffset + UPCAOffset) * NarrowBar)
  488.             rtn = DrawBarText(cHDC, TextStartX, TextStartY - 3, cBarTextFont, cBarColor, cBarRotation, HRRightDigit)
  489.             If rtn = 0 Then GoTo Raise_Error
  490.         End If
  491.         'Draw the supplemental text above the supplemental bars (if using supplementals)
  492.         If HRSupplementText <> "" Then
  493.             rtn = GetTextSize(cHDC, cBarTextFont, HRSupplementText, HRTextWidth, HRTextHeight)
  494.             If rtn = 0 Then GoTo Raise_Error
  495.             TextStartX = (BarCodeWidth + BarStartX - SupplementWidth) + ((SupplementWidth - HRTextWidth) / 2)
  496.             TextStartY = BarStartY - HRTextHeight
  497.             rtn = DrawBarText(cHDC, TextStartX, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRSupplementText)
  498.             If rtn = 0 Then GoTo Raise_Error
  499.         End If
  500.     End If
  501. Exit Sub
  502.     
  503. Raise_Error:
  504.     cErrorNumber = B_ErrDraw
  505.     cErrDescription = S_ErrDraw
  506.     err.Raise cErrorNumber, "clsEANBarCode.Draw_Normal", cErrDescription
  507. End Sub
  508.  
  509. Private Sub Draw_90()
  510.  
  511.     Dim NextBar As Single       'Postion to start the next bar
  512.     Dim BarH As Integer         'Bar height
  513.     Dim iCntr As Integer        'Loop counter
  514.     Dim bColor As OLE_COLOR     'Current bar color
  515.     Dim rtn As Long
  516.     
  517.     'DRAW THE BARCODE
  518.     '================
  519.     'Get the current height and width of the HR text
  520.     rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  521.     If rtn = 0 Then GoTo Raise_Error
  522.     BarH = cBarCodeHeight
  523.     BarStartX = cBarCodeLeft: BarStartY = cBarCodeTop + BarCodeWidth
  524.     bColor = vbWhite
  525.     NextBar = BarStartY - LeftDigitWidth
  526.     For iCntr = 1 To Len(EncodedData)
  527.         'Toggle the color
  528.         If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  529.         BarBit = Mid(EncodedData, iCntr, 1)
  530.         Select Case BarBit
  531.             '"T" & "S" are embedded in the encoded data and are used to toggle between bar heights
  532.             Case "T"
  533.                 BarH = GuardBarHeight
  534.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  535.                 
  536.             Case "S"
  537.                 BarH = cBarCodeHeight
  538.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  539.                 
  540.             'Narrow Bar
  541.             Case "1"
  542.                 If bColor <> vbWhite Then
  543.                     rtn = DrawBar(cHDC, BarStartX, CLng(NextBar - NarrowBar), BarStartX + BarH, CLng(NextBar), bColor)
  544.                     If rtn = 0 Then GoTo Raise_Error
  545.                 End If
  546.                 NextBar = NextBar - NarrowBar
  547.                 
  548.             'Wide Bar
  549.             Case "2", "3", "4"
  550.                 WideBar = NarrowBar * BarBit
  551.                 If bColor <> vbWhite Then
  552.                     rtn = DrawBar(cHDC, BarStartX, CLng(NextBar - WideBar), BarStartX + BarH, CLng(NextBar), bColor)
  553.                     If rtn = 0 Then GoTo Raise_Error
  554.                 End If
  555.                 NextBar = NextBar - WideBar
  556.                 
  557.             'Gap for supplement 2 or supplement 5
  558.             Case "9"
  559.                 NextBar = NextBar - SupplementGap
  560.                 BarStartX = cBarCodeLeft + HRTextHeight
  561.                 BarH = GuardBarHeight - HRTextHeight
  562.             End Select
  563.     Next iCntr
  564.     
  565.     'DRAW THE HUMAN READABLE TEXT
  566.     '============================
  567.     If cHumanReadable Then
  568.         TextStartY = BarStartY
  569.         TextStartX = cBarCodeLeft + cBarCodeHeight + cBarTextGap
  570.         'Draw the first digit left of the left guard bars
  571.         If HRLeftDigit <> "" Then
  572.             rtn = DrawBarText(cHDC, TextStartX - 3, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRLeftDigit)
  573.             If rtn = 0 Then GoTo Raise_Error
  574.         End If
  575.         'Draw the left hand text right of the left guard bars
  576.         TextStartY = TextStartY - LeftDigitWidth - ((3 + UPCAOffset) * NarrowBar)
  577.         rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  578.         If rtn = 0 Then GoTo Raise_Error
  579.         rtn = DrawBarText(cHDC, TextStartX, TextStartY - ((TextGap - HRTextWidth) / 2), cBarTextFont, _
  580.                             cBarColor, cBarRotation, HRLeftText)
  581.         If rtn = 0 Then GoTo Raise_Error
  582.         'Draw the right hand text right of the centre guard bars
  583.         If HRRightText <> "" Then
  584.             TextStartY = TextStartY - TextGap - (5 * NarrowBar)
  585.             rtn = GetTextSize(cHDC, cBarTextFont, HRRightText, HRTextWidth, HRTextHeight)
  586.             If rtn = 0 Then GoTo Raise_Error
  587.             rtn = DrawBarText(cHDC, TextStartX, TextStartY - ((TextGap - HRTextWidth) / 2), cBarTextFont, _
  588.                             cBarColor, cBarRotation, HRRightText)
  589.             If rtn = 0 Then GoTo Raise_Error
  590.         End If
  591.         'Draw the check digit to the right of the right hand guard bars (UPC only)
  592.         If HRRightDigit <> "" Then
  593.             TextStartY = TextStartY - TextGap - ((4 + UPCEOffset + UPCAOffset) * NarrowBar)
  594.             rtn = DrawBarText(cHDC, TextStartX - 3, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRRightDigit)
  595.             If rtn = 0 Then GoTo Raise_Error
  596.         End If
  597.         'Draw the supplemental text above the supplemental bars (if using supplementals)
  598.         If HRSupplementText <> "" Then
  599.             rtn = GetTextSize(cHDC, cBarTextFont, HRSupplementText, HRTextWidth, HRTextHeight)
  600.             If rtn = 0 Then GoTo Raise_Error
  601.             TextStartY = (BarStartY - BarCodeWidth + SupplementWidth) - ((SupplementWidth - HRTextWidth) / 2)
  602.             TextStartX = BarStartX - HRTextHeight
  603.             rtn = DrawBarText(cHDC, TextStartX, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRSupplementText)
  604.             If rtn = 0 Then GoTo Raise_Error
  605.         End If
  606.     End If
  607. Exit Sub
  608.  
  609. Raise_Error:
  610.     cErrorNumber = B_ErrDraw
  611.     cErrDescription = S_ErrDraw
  612.     err.Raise cErrorNumber, "clsEANBarCode.Draw_90", cErrDescription
  613. End Sub
  614.  
  615. Private Sub Draw_180()
  616.  
  617.     Dim NextBar As Single       'Postion to start the next bar
  618.     Dim BarH As Integer         'Bar height
  619.     Dim iCntr As Integer        'Loop counter
  620.     Dim bColor As OLE_COLOR     'Current bar color
  621.     Dim rtn As Long
  622.     
  623.     'DRAW THE BARCODE
  624.     '================
  625.     'Get the current height and width of the HR text
  626.     rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  627.     If rtn = 0 Then GoTo Raise_Error
  628.     BarH = cBarCodeHeight
  629.     BarStartX = cBarCodeLeft + BarCodeWidth
  630.     BarStartY = cBarCodeTop
  631.     
  632.     bColor = vbWhite
  633.     NextBar = BarStartX - LeftDigitWidth
  634.     For iCntr = 1 To Len(EncodedData)
  635.         'Toggle the color
  636.         If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  637.         BarBit = Mid(EncodedData, iCntr, 1)
  638.         Select Case BarBit
  639.             '"T" & "S" are embedded in the encoded data and are used to toggle between bar heights
  640.             Case "T"
  641.                 BarH = GuardBarHeight
  642.                 If cHumanReadable Then
  643.                     BarStartY = cBarCodeTop + HRTextHeight + cBarTextGap - (GuardBarHeight - cBarCodeHeight)
  644.                 Else
  645.                     BarStartY = cBarCodeTop
  646.                 End If
  647.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  648.                 
  649.             Case "S"
  650.                 BarH = cBarCodeHeight
  651.                 If cHumanReadable Then
  652.                     BarStartY = cBarCodeTop + HRTextHeight + cBarTextGap
  653.                 Else
  654.                     BarStartY = cBarCodeTop + (GuardBarHeight - cBarCodeHeight)
  655.                 End If
  656.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  657.  
  658.             'Narrow Bar
  659.             Case "1"
  660.                 If bColor <> vbWhite Then
  661.                     rtn = DrawBar(cHDC, CLng(NextBar - NarrowBar), BarStartY, CLng(NextBar), BarStartY + BarH, bColor)
  662.                     If rtn = 0 Then GoTo Raise_Error
  663.                 End If
  664.                 NextBar = NextBar - NarrowBar
  665.                 
  666.             'Wide Bar
  667.             Case "2", "3", "4"
  668.                 WideBar = NarrowBar * BarBit
  669.                 If bColor <> vbWhite Then
  670.                     rtn = DrawBar(cHDC, CLng(NextBar - WideBar), BarStartY, CLng(NextBar), BarStartY + BarH, bColor)
  671.                     If rtn = 0 Then GoTo Raise_Error
  672.                 End If
  673.                 NextBar = NextBar - WideBar
  674.                 
  675.             'Gap for supplement 2 or supplement 5
  676.             Case "9"
  677.                 NextBar = NextBar - SupplementGap
  678.                 If cHumanReadable Then
  679.                     BarStartY = cBarCodeTop + HRTextHeight + cBarTextGap - (GuardBarHeight - cBarCodeHeight)
  680.                 Else
  681.                     BarStartY = cBarCodeTop
  682.                 End If
  683.                 BarH = GuardBarHeight - HRTextHeight
  684.             End Select
  685.     Next iCntr
  686.     
  687.     'DRAW THE HUMAN READABLE TEXT
  688.     '============================
  689.     If cHumanReadable Then
  690.         TextStartY = cBarCodeTop + HRTextHeight
  691.         TextStartX = cBarCodeLeft + BarCodeWidth
  692.         'Draw the first digit left of the left guard bars
  693.         If HRLeftDigit <> "" Then
  694.             rtn = DrawBarText(cHDC, TextStartX, TextStartY + 3, cBarTextFont, cBarColor, cBarRotation, HRLeftDigit)
  695.             If rtn = 0 Then GoTo Raise_Error
  696.         End If
  697.         'Draw the left hand text right of the left guard bars
  698.         TextStartX = TextStartX - LeftDigitWidth - ((3 + UPCAOffset) * NarrowBar)
  699.         rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  700.         If rtn = 0 Then GoTo Raise_Error
  701.         rtn = DrawBarText(cHDC, TextStartX - ((TextGap - HRTextWidth) / 2), TextStartY, cBarTextFont, _
  702.                             cBarColor, cBarRotation, HRLeftText)
  703.         If rtn = 0 Then GoTo Raise_Error
  704.         'Draw the right hand text right of the centre guard bars
  705.         If HRRightText <> "" Then
  706.             TextStartX = TextStartX - TextGap - (5 * NarrowBar)
  707.             rtn = GetTextSize(cHDC, cBarTextFont, HRRightText, HRTextWidth, HRTextHeight)
  708.             If rtn = 0 Then GoTo Raise_Error
  709.             rtn = DrawBarText(cHDC, TextStartX - ((TextGap - HRTextWidth) / 2), TextStartY, cBarTextFont, _
  710.                             cBarColor, cBarRotation, HRRightText)
  711.             If rtn = 0 Then GoTo Raise_Error
  712.         End If
  713.         'Draw the check digit to the right of the right hand guard bars (UPC only)
  714.         If HRRightDigit <> "" Then
  715.             TextStartX = TextStartX - TextGap - ((4 + UPCEOffset + UPCAOffset) * NarrowBar)
  716.             rtn = DrawBarText(cHDC, TextStartX, TextStartY + 3, cBarTextFont, cBarColor, cBarRotation, HRRightDigit)
  717.             If rtn = 0 Then GoTo Raise_Error
  718.         End If
  719.         'Draw the supplemental text above the supplemental bars (if using supplementals)
  720.         If HRSupplementText <> "" Then
  721.             rtn = GetTextSize(cHDC, cBarTextFont, HRSupplementText, HRTextWidth, HRTextHeight)
  722.             If rtn = 0 Then GoTo Raise_Error
  723.             TextStartX = (cBarCodeLeft + SupplementWidth) - ((SupplementWidth - HRTextWidth) / 2)
  724.             TextStartY = cBarCodeTop + cBarCodeHeight + HRTextHeight + cBarTextGap
  725.             rtn = DrawBarText(cHDC, TextStartX, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRSupplementText)
  726.             If rtn = 0 Then GoTo Raise_Error
  727.         End If
  728.     End If
  729. Exit Sub
  730.  
  731. Raise_Error:
  732.     cErrorNumber = B_ErrDraw
  733.     cErrDescription = S_ErrDraw
  734.     err.Raise cErrorNumber, "clsEANBarCode.Draw_180", cErrDescription
  735. End Sub
  736.  
  737. Private Sub Draw_270()
  738.  
  739.     Dim NextBar As Single       'Postion to start the next bar
  740.     Dim BarH As Integer         'Bar height
  741.     Dim iCntr As Integer        'Loop counter
  742.     Dim bColor As OLE_COLOR     'Current bar color
  743.     Dim rtn As Long
  744.     
  745.     'DRAW THE BARCODE
  746.     '================
  747.     'Get the current height and width of the HR text
  748.     rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  749.     If rtn = 0 Then GoTo Raise_Error
  750.     BarH = cBarCodeHeight
  751.     BarStartX = cBarCodeLeft: BarStartY = cBarCodeTop + LeftDigitWidth
  752.     bColor = vbWhite
  753.     NextBar = BarStartY
  754.     For iCntr = 1 To Len(EncodedData)
  755.         'Toggle the color
  756.         If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  757.         BarBit = Mid(EncodedData, iCntr, 1)
  758.         Select Case BarBit
  759.             '"T" & "S" are embedded in the encoded data and are used to toggle between bar heights
  760.             Case "T"
  761.                 BarH = GuardBarHeight
  762.                 If cHumanReadable Then
  763.                     BarStartX = cBarCodeLeft + HRTextHeight + cBarTextGap - (GuardBarHeight - cBarCodeHeight)
  764.                 Else
  765.                     BarStartX = cBarCodeLeft
  766.                 End If
  767.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  768.                 
  769.             Case "S"
  770.                 BarH = cBarCodeHeight
  771.                 If cHumanReadable Then
  772.                     BarStartX = cBarCodeLeft + HRTextHeight + cBarTextGap
  773.                 Else
  774.                     BarStartX = cBarCodeLeft + (GuardBarHeight - cBarCodeHeight)
  775.                 End If
  776.                 If bColor = vbWhite Then bColor = cBarColor Else bColor = vbWhite
  777.                 
  778.             'Narrow Bar
  779.             Case "1"
  780.                 If bColor <> vbWhite Then
  781.                     rtn = DrawBar(cHDC, BarStartX, CLng(NextBar), BarStartX + BarH, CLng(NextBar + NarrowBar), bColor)
  782.                     If rtn = 0 Then GoTo Raise_Error
  783.                 End If
  784.                 NextBar = NextBar + NarrowBar
  785.                 
  786.             'Wide Bar
  787.             Case "2", "3", "4"
  788.                 WideBar = NarrowBar * BarBit
  789.                 If bColor <> vbWhite Then
  790.                     rtn = DrawBar(cHDC, BarStartX, CLng(NextBar), BarStartX + BarH, CLng(NextBar + WideBar), bColor)
  791.                     If rtn = 0 Then GoTo Raise_Error
  792.                 End If
  793.                 NextBar = NextBar + WideBar
  794.                 
  795.             'Gap for supplement 2 or supplement 5
  796.             Case "9"
  797.                 NextBar = NextBar + SupplementGap
  798.                 If cHumanReadable Then
  799.                     BarStartX = cBarCodeLeft + HRTextHeight + cBarTextGap - (GuardBarHeight - cBarCodeHeight)
  800.                 Else
  801.                     BarStartX = cBarCodeLeft
  802.                 End If
  803.                 BarH = GuardBarHeight - HRTextHeight
  804.             End Select
  805.     Next iCntr
  806.     
  807.     'DRAW THE HUMAN READABLE TEXT
  808.     '============================
  809.     If cHumanReadable Then
  810.         TextStartY = cBarCodeTop
  811.         TextStartX = cBarCodeLeft + HRTextHeight
  812.         'Draw the first digit left of the left guard bars
  813.         If HRLeftDigit <> "" Then
  814.             rtn = DrawBarText(cHDC, TextStartX + 3, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRLeftDigit)
  815.             If rtn = 0 Then GoTo Raise_Error
  816.         End If
  817.         'Draw the left hand text right of the left guard bars
  818.         TextStartY = TextStartY + LeftDigitWidth + ((3 + UPCAOffset) * NarrowBar)
  819.         rtn = GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  820.         If rtn = 0 Then GoTo Raise_Error
  821.         rtn = DrawBarText(cHDC, TextStartX, TextStartY + ((TextGap - HRTextWidth) / 2), cBarTextFont, _
  822.                             cBarColor, cBarRotation, HRLeftText)
  823.         If rtn = 0 Then GoTo Raise_Error
  824.         'Draw the right hand text right of the centre guard bars
  825.         If HRRightText <> "" Then
  826.             TextStartY = TextStartY + TextGap + (5 * NarrowBar)
  827.             rtn = GetTextSize(cHDC, cBarTextFont, HRRightText, HRTextWidth, HRTextHeight)
  828.             If rtn = 0 Then GoTo Raise_Error
  829.             rtn = DrawBarText(cHDC, TextStartX, TextStartY + ((TextGap - HRTextWidth) / 2), cBarTextFont, _
  830.                             cBarColor, cBarRotation, HRRightText)
  831.             If rtn = 0 Then GoTo Raise_Error
  832.         End If
  833.         'Draw the check digit to the right of the right hand guard bars (UPC only)
  834.         If HRRightDigit <> "" Then
  835.             TextStartY = TextStartY + TextGap + ((4 + UPCEOffset + UPCAOffset) * NarrowBar)
  836.             rtn = DrawBarText(cHDC, TextStartX + 3, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRRightDigit)
  837.             If rtn = 0 Then GoTo Raise_Error
  838.         End If
  839.         'Draw the HR text above the supplemental bars (if using supplementals)
  840.         If HRSupplementText <> "" Then
  841.             rtn = GetTextSize(cHDC, cBarTextFont, HRSupplementText, HRTextWidth, HRTextHeight)
  842.             If rtn = 0 Then GoTo Raise_Error
  843.             TextStartY = (cBarCodeTop + BarCodeWidth - SupplementWidth) + ((SupplementWidth - HRTextWidth) / 2)
  844.             TextStartX = cBarCodeLeft + cBarCodeHeight + HRTextHeight + cBarTextGap
  845.             rtn = DrawBarText(cHDC, TextStartX, TextStartY, cBarTextFont, cBarColor, cBarRotation, HRSupplementText)
  846.             If rtn = 0 Then GoTo Raise_Error
  847.         End If
  848.     End If
  849. Exit Sub
  850.  
  851. Raise_Error:
  852.     cErrorNumber = B_ErrDraw
  853.     cErrDescription = S_ErrDraw
  854.     err.Raise cErrorNumber, "clsEANBarCode.Draw_270", cErrDescription
  855. End Sub
  856.  
  857. Private Function EncodeEAN13(DataToEncode As String)
  858.     Dim iCntr As Integer
  859.     Dim iWeight As Integer
  860.     Dim sChkDigit As String * 1
  861.     Dim TotalWeight As Integer
  862.     Dim sDataChar As String * 1
  863.     Dim sParity As String
  864.     Dim sParityBit As String * 1
  865.     
  866.     'Calculate the check digit
  867.     iWeight = 3
  868.     For iCntr = Len(DataToEncode) To 1 Step -1
  869.         TotalWeight = TotalWeight + (CInt(Mid(DataToEncode, iCntr, 1)) * iWeight)
  870.         If iWeight = 3 Then iWeight = 1 Else iWeight = 3
  871.     Next iCntr
  872.     TotalWeight = TotalWeight Mod 10
  873.     sChkDigit = IIf(TotalWeight = 0, "0", CStr(10 - TotalWeight))
  874.     DataToEncode = DataToEncode & sChkDigit
  875.     'Add the start guard bars
  876.     EncodedData = "T111S"
  877.     For iCntr = 1 To Len(DataToEncode)
  878.         sDataChar = Mid(DataToEncode, iCntr, 1)
  879.         If iCntr = 1 Then
  880.             sParity = EANUPC_Parity(CInt(sDataChar)) 'Get parity based on first character
  881.         Else
  882.             'Left hand data
  883.             If iCntr < 8 Then
  884.                 sParityBit = Mid(sParity, iCntr - 1, 1)
  885.                 Select Case sParityBit
  886.                     Case "0"    'EVEN
  887.                         EncodedData = EncodedData & EANUPC_LHE(CInt(sDataChar))
  888.                     Case "1"    'ODD
  889.                         EncodedData = EncodedData & EANUPC_LHO(CInt(sDataChar))
  890.                     End Select
  891.             End If
  892.             'Insert the centre guard bars
  893.             If iCntr = 8 Then EncodedData = EncodedData & "T11111S"
  894.             'Right hand data
  895.             If iCntr > 7 Then EncodedData = EncodedData & EANUPC_RH(CInt(sDataChar))
  896.         End If
  897.     Next iCntr
  898.     'Add the end guard bars
  899.     EncodedData = EncodedData & "T111S"
  900.     'Set some drawing values
  901.     UPCAOffset = 0
  902.     UPCEOffset = 0
  903.     NumberOfBars = 42
  904.     HRLeftDigit = Left(DataToEncode, 1)
  905.     HRRightDigit = ""
  906.     HRLeftText = Mid(DataToEncode, 2, 6)
  907.     HRRightText = Mid(DataToEncode, 8, 6)
  908.     HRSupplementText = ""
  909.     GetBarWidth
  910. End Function
  911.  
  912. Private Function EncodeEAN8(DataToEncode As String)
  913.     Dim iCntr As Integer
  914.     Dim iWeight As Integer
  915.     Dim sChkDigit As String * 1
  916.     Dim TotalWeight As Integer
  917.     Dim sDataChar As String * 1
  918.     
  919.     'Calculate the check digit
  920.     iWeight = 3
  921.     For iCntr = Len(DataToEncode) To 1 Step -1
  922.         TotalWeight = TotalWeight + (CInt(Mid(DataToEncode, iCntr, 1)) * iWeight)
  923.         If iWeight = 3 Then iWeight = 1 Else iWeight = 3
  924.     Next iCntr
  925.     TotalWeight = TotalWeight Mod 10
  926.     sChkDigit = IIf(TotalWeight = 0, "0", CStr(10 - TotalWeight))
  927.     DataToEncode = DataToEncode & sChkDigit
  928.     'Add the start guard bars
  929.     EncodedData = "T111S"
  930.     For iCntr = 1 To Len(DataToEncode)
  931.         sDataChar = Mid(DataToEncode, iCntr, 1)
  932.         'Left hand data
  933.         If iCntr < 5 Then EncodedData = EncodedData & EANUPC_LHO(CInt(sDataChar))
  934.         'Insert the centre guard bars
  935.         If iCntr = 5 Then EncodedData = EncodedData & "T11111S"
  936.         'Right hand data
  937.         If iCntr > 4 Then EncodedData = EncodedData & EANUPC_RH(CInt(sDataChar))
  938.     Next iCntr
  939.     'Add the end gaurd bars
  940.     EncodedData = EncodedData & "T111S"
  941.     'Set some drawing values
  942.     UPCAOffset = 0
  943.     UPCEOffset = 0
  944.     NumberOfBars = 28
  945.     HRLeftDigit = ""
  946.     HRRightDigit = ""
  947.     HRLeftText = Mid(DataToEncode, 1, 4)
  948.     HRRightText = Mid(DataToEncode, 5, 4)
  949.     HRSupplementText = ""
  950.     GetBarWidth
  951. End Function
  952.  
  953. Private Function EncodeUPCA(DataToEncode As String)
  954.     Dim iCntr As Integer
  955.     Dim iWeight As Integer
  956.     Dim TotalWeight As Integer
  957.     Dim sChkDigit As String * 1
  958.     Dim sDataChar As String * 1
  959.     
  960.     'Calculate the check digit
  961.     iWeight = 3
  962.     For iCntr = Len(DataToEncode) To 1 Step -1
  963.         TotalWeight = TotalWeight + (CInt(Mid(DataToEncode, iCntr, 1)) * iWeight)
  964.         If iWeight = 3 Then iWeight = 1 Else iWeight = 3
  965.     Next iCntr
  966.     TotalWeight = TotalWeight Mod 10
  967.     sChkDigit = IIf(TotalWeight = 0, "0", CStr(10 - TotalWeight))
  968.     DataToEncode = DataToEncode & sChkDigit
  969.     'Add the start guard bars
  970.     EncodedData = "T111"
  971.     For iCntr = 1 To Len(DataToEncode)
  972.         sDataChar = Mid(DataToEncode, iCntr, 1)
  973.             'Left hand data
  974.             If iCntr < 7 Then EncodedData = EncodedData & EANUPC_LHO(CInt(sDataChar))
  975.             If iCntr = 1 Then EncodedData = EncodedData & "S"
  976.             'Insert the centre guard bars
  977.             If iCntr = 7 Then EncodedData = EncodedData & "T11111S"
  978.             'Right hand data
  979.             If iCntr > 6 Then EncodedData = EncodedData & EANUPC_RH(CInt(sDataChar))
  980.             If iCntr = 11 Then EncodedData = EncodedData & "T"
  981.     Next iCntr
  982.     'Add the end gaurd bars
  983.     EncodedData = EncodedData & "111S"
  984.     'Set some drawing values
  985.     UPCAOffset = 7
  986.     UPCEOffset = 0
  987.     NumberOfBars = 35
  988.     HRLeftDigit = Left(DataToEncode, 1)
  989.     HRRightDigit = Right(DataToEncode, 1)
  990.     HRLeftText = Mid(DataToEncode, 2, 5)
  991.     HRRightText = Mid(DataToEncode, 7, 5)
  992.     HRSupplementText = ""
  993.     GetBarWidth
  994. End Function
  995.  
  996. Private Function EncodeUPCE(DataToEncode As String)
  997.     Dim iCntr As Integer
  998.     Dim iChkDigit As Integer
  999.     Dim sDataChar As String * 1
  1000.     Dim sParity As String
  1001.     Dim sParityBit As String * 1
  1002.     
  1003.     iChkDigit = UPCeToUPCa(DataToEncode)
  1004.     sParity = UPCe_Parity(iChkDigit)
  1005.     'Add the start guard bars
  1006.     EncodedData = "T111S"
  1007.     For iCntr = 1 To Len(DataToEncode)
  1008.         sDataChar = Mid(DataToEncode, iCntr, 1)
  1009.         sParityBit = Mid(sParity, iCntr, 1)
  1010.         Select Case sParityBit
  1011.             Case "0"    'EVEN
  1012.                 EncodedData = EncodedData & EANUPC_LHE(CInt(sDataChar))
  1013.             Case "1"    'ODD
  1014.                 EncodedData = EncodedData & EANUPC_LHO(CInt(sDataChar))
  1015.         End Select
  1016.     Next iCntr
  1017.     'Add the end guard bars
  1018.     EncodedData = EncodedData & "T111111S"
  1019.     'Set some drawing values
  1020.     UPCAOffset = 0
  1021.     UPCEOffset = 3
  1022.     NumberOfBars = 42
  1023.     HRLeftDigit = "0"
  1024.     HRRightDigit = CStr(iChkDigit)
  1025.     HRLeftText = DataToEncode
  1026.     HRRightText = ""
  1027.     HRSupplementText = ""
  1028.     GetBarWidth
  1029. End Function
  1030.  
  1031. Private Function EncodePlus2(DataToEncode As String)
  1032.     Dim iCntr As Integer
  1033.     Dim iBarCnt As Integer
  1034.     Dim iBW As Integer
  1035.     Dim iChkDigit As Integer
  1036.     Dim sDataChar As String * 1
  1037.     Dim sParity As String
  1038.     Dim sParityBit As String * 1
  1039.     Dim iGap As Integer
  1040.     
  1041.     iChkDigit = CInt(DataToEncode) Mod 4
  1042.     sParity = Plus2_Parity(iChkDigit)
  1043.     'Add the start guard bars
  1044.     SupplementData = "112"
  1045.     For iCntr = 1 To Len(DataToEncode)
  1046.         sDataChar = Mid(DataToEncode, iCntr, 1)
  1047.         sParityBit = Mid(sParity, iCntr, 1)
  1048.         Select Case sParityBit
  1049.             Case "0"    'EVEN
  1050.                 SupplementData = SupplementData & EANUPC_LHE(CInt(sDataChar))
  1051.             Case "1"    'ODD
  1052.                 SupplementData = SupplementData & EANUPC_LHO(CInt(sDataChar))
  1053.         End Select
  1054.         If iCntr < Len(DataToEncode) Then SupplementData = SupplementData & "11"
  1055.     Next iCntr
  1056.     HRSupplementText = DataToEncode
  1057.     NarrowBar = cBarX * cBarMultiplier
  1058.     For iCntr = 1 To Len(SupplementData)
  1059.         iBW = CInt(Mid(SupplementData, iCntr, 1))
  1060.         If iBW > 1 Then iBW = iBW
  1061.         iBarCnt = iBarCnt + iBW
  1062.     Next iCntr
  1063.     SupplementWidth = iBarCnt * NarrowBar
  1064.     EncodedData = EncodedData & "9" & SupplementData
  1065.     iGap = MilsToPixels(cHDC, 0.125)
  1066.     SupplementGap = IIf(iGap > RightDigitWidth, iGap, RightDigitWidth)
  1067.     BarCodeWidth = BarCodeWidth + SupplementGap + SupplementWidth - RightDigitWidth
  1068. End Function
  1069.  
  1070. Private Function EncodePlus5(DataToEncode As String)
  1071.     Dim iCntr As Integer
  1072.     Dim iBarCnt As Integer
  1073.     Dim iBW As Integer
  1074.     Dim iWeight As Integer
  1075.     Dim TotalWeight As Integer
  1076.     Dim sParity As String
  1077.     Dim iChkDigit As Integer
  1078.     Dim sDataChar As String * 1
  1079.     Dim sParityBit As String * 1
  1080.     Dim iGap As Integer
  1081.     
  1082.     iWeight = 3
  1083.     For iCntr = Len(DataToEncode) To 1 Step -1
  1084.         TotalWeight = TotalWeight + (CInt(Mid(DataToEncode, iCntr, 1)) * iWeight)
  1085.         If iWeight = 3 Then iWeight = 9 Else iWeight = 3
  1086.     Next iCntr
  1087.     iChkDigit = TotalWeight Mod 10
  1088.     sParity = Plus5_Parity(iChkDigit)
  1089.     'Add the start guard bars
  1090.     SupplementData = "112"
  1091.     For iCntr = 1 To Len(DataToEncode)
  1092.         sDataChar = Mid(DataToEncode, iCntr, 1)
  1093.         sParityBit = Mid(sParity, iCntr, 1)
  1094.         Select Case sParityBit
  1095.             Case "0"    'EVEN
  1096.                 SupplementData = SupplementData & EANUPC_LHE(CInt(sDataChar))
  1097.             Case "1"    'ODD
  1098.                 SupplementData = SupplementData & EANUPC_LHO(CInt(sDataChar))
  1099.         End Select
  1100.         If iCntr < Len(DataToEncode) Then SupplementData = SupplementData & "11"
  1101.     Next iCntr
  1102.     HRSupplementText = DataToEncode
  1103.     NarrowBar = cBarX * cBarMultiplier
  1104.     For iCntr = 1 To Len(SupplementData)
  1105.         iBW = CInt(Mid(SupplementData, iCntr, 1))
  1106.         If iBW > 1 Then iBW = iBW
  1107.         iBarCnt = iBarCnt + iBW
  1108.     Next iCntr
  1109.     SupplementWidth = iBarCnt * NarrowBar
  1110.     EncodedData = EncodedData & "9" & SupplementData
  1111.     iGap = MilsToPixels(cHDC, 0.125)
  1112.     SupplementGap = IIf(iGap > RightDigitWidth, iGap, RightDigitWidth)
  1113.     BarCodeWidth = BarCodeWidth + SupplementGap + SupplementWidth - RightDigitWidth
  1114. End Function
  1115.  
  1116. Private Sub GetBarWidth()
  1117.     'Calculate the width of the barcode by first counting the total number of bars.
  1118.     'If "autofontsize" is true, detects the largest font size that will fit between
  1119.     'the guard bars, else use the current font size.
  1120.     'Then calculate the width of the character either side of the barcode (if any)
  1121.     'based on the font size. Sum these values to get the total barcode width.
  1122.     
  1123.     Dim iCntr As Integer
  1124.     Dim iBarCnt As Integer
  1125.     Dim c As String
  1126.     Dim w1 As Integer
  1127.     Dim w2 As Integer
  1128.     Dim fs As Integer       'Font size
  1129.     Dim rtn As Long
  1130.     
  1131.     'Count the bars to calculate the bar width
  1132.     NarrowBar = cBarX * cBarMultiplier
  1133.     For iCntr = 1 To Len(EncodedData)
  1134.         c = Mid(EncodedData, iCntr, 1)
  1135.         'Mask off the bar height switch markers "T" & "S"
  1136.         If c <> "T" And c <> "S" Then iBarCnt = iBarCnt + CInt(c)
  1137.     Next iCntr
  1138.     
  1139.     'Calculate the gap between the guard bars
  1140.     TextGap = NarrowBar * NumberOfBars
  1141.     If cAutoFontSize Then
  1142.         fs = 0
  1143.         Do
  1144.             fs = fs + 1     'Start with a fontsize of 1
  1145.             cBarTextFont.FontSize = fs
  1146.             'Check both the left and right side text in case one is wider than the other
  1147.             Call GetTextSize(cHDC, cBarTextFont, HRLeftText, HRTextWidth, HRTextHeight)
  1148.             w1 = HRTextWidth
  1149.             If HRRightText <> "" Then Call GetTextSize(cHDC, cBarTextFont, HRRightText, HRTextWidth, HRTextHeight)
  1150.             w2 = HRTextWidth
  1151.             HRTextWidth = IIf(w1 > w2, w1, w2)
  1152.         Loop Until HRTextWidth >= TextGap
  1153.         cBarTextFont.FontSize = fs - 1
  1154.     End If
  1155.     'Get the width of the left hand digit
  1156.     LeftDigitWidth = 0: RightDigitWidth = 0
  1157.     If HRLeftDigit <> "" Then Call GetTextSize(cHDC, cBarTextFont, HRLeftDigit, LeftDigitWidth, HRTextHeight)
  1158.     'Get the width of the right hand digit
  1159.     If HRRightDigit <> "" Then Call GetTextSize(cHDC, cBarTextFont, HRRightDigit, RightDigitWidth, HRTextHeight)
  1160.     If Not cHumanReadable Then LeftDigitWidth = 0: RightDigitWidth = 0
  1161.     BarCodeWidth = LeftDigitWidth + RightDigitWidth + (NarrowBar * iBarCnt)
  1162. End Sub
  1163.  
  1164. Private Function UPCeToUPCa(ByVal DataToConvert As String) As Integer
  1165.     'Expand the UCPe code to UPCa for the purpose of
  1166.     'calculating the check digit.
  1167.     'The check digit is used to determine the parity
  1168.     'for encoding the UPCe data.
  1169.     
  1170.     Dim iCntr As Integer
  1171.     Dim iWeight As Integer
  1172.     Dim TotalWeight As Integer
  1173.     Dim UPCaCode As String
  1174.     Dim D1 As String
  1175.     Dim D2 As String
  1176.     Dim D3 As String
  1177.     Dim D4 As String
  1178.     Dim D5 As String
  1179.     Dim D6 As String
  1180.     Dim D7 As String
  1181.     
  1182.     DataToConvert = "0" & DataToConvert
  1183.     D1 = Mid(DataToConvert, 1, 1)
  1184.     D2 = Mid(DataToConvert, 2, 1)
  1185.     D3 = Mid(DataToConvert, 3, 1)
  1186.     D4 = Mid(DataToConvert, 4, 1)
  1187.     D5 = Mid(DataToConvert, 5, 1)
  1188.     D6 = Mid(DataToConvert, 6, 1)
  1189.     D7 = Mid(DataToConvert, 7, 1)
  1190.     Select Case D7
  1191.         Case "0"
  1192.             UPCaCode = D1 & D2 & D3 & "00000" & D4 & D5 & D6
  1193.         Case "1", "2"
  1194.             UPCaCode = D1 & D2 & D3 & D7 & "0000" & D4 & D5 & D6
  1195.         Case "3"
  1196.             UPCaCode = D1 & D2 & D3 & D4 & "00000" & D5 & D6
  1197.         Case "4"
  1198.             UPCaCode = D1 & D2 & D3 & D4 & D5 & "00000" & D6
  1199.         Case "5", "6", "7", "8", "9"
  1200.             UPCaCode = D1 & D2 & D3 & D4 & D5 & D6 & "0000" & D7
  1201.     End Select
  1202.     iWeight = 3
  1203.     For iCntr = Len(UPCaCode) To 1 Step -1
  1204.         TotalWeight = TotalWeight + (CInt(Mid(UPCaCode, iCntr, 1)) * iWeight)
  1205.         If iWeight = 3 Then iWeight = 1 Else iWeight = 3
  1206.     Next iCntr
  1207.     TotalWeight = TotalWeight Mod 10
  1208.     UPCeToUPCa = IIf(TotalWeight = 0, 0, 10 - TotalWeight)
  1209. End Function
  1210.  
  1211. Private Sub Class_Initialize()
  1212.     'Initialize some default values
  1213.     'Barcode
  1214.     cSymbology = 0
  1215.     cBarX = 1
  1216.     cBarMultiplier = 1
  1217.     cBarRotation = 0
  1218.     cBarCodeTop = 1
  1219.     cBarCodeLeft = 1
  1220.     cBarCodeHeight = 45
  1221.     cBarTextGap = 0
  1222.     cBarCodeData = ""
  1223.     
  1224.     'Barcode text
  1225.     cHumanReadable = True
  1226.     cAutoFontSize = False
  1227.     cBarTextFont.FontName = "Tahoma"
  1228.     cBarTextFont.FontSize = 8
  1229.     cBarTextFont.FontBold = False
  1230.     cBarTextFont.FontItalic = False
  1231.     
  1232.     'Errors
  1233.     cErrorNumber = 0
  1234.     cErrDescription = ""
  1235.     
  1236.     'Barcode characters
  1237.     Initialise_BC
  1238. End Sub
  1239.  
  1240. Private Sub Initialise_BC()
  1241.  
  1242.     BC_ValidCharacters = "0123456789"
  1243.         
  1244.     'Left hand ODD encoding
  1245.     EANUPC_LHO(0) = "3211": EANUPC_LHO(1) = "2221"
  1246.     EANUPC_LHO(2) = "2122": EANUPC_LHO(3) = "1411"
  1247.     EANUPC_LHO(4) = "1132": EANUPC_LHO(5) = "1231"
  1248.     EANUPC_LHO(6) = "1114": EANUPC_LHO(7) = "1312"
  1249.     EANUPC_LHO(8) = "1213": EANUPC_LHO(9) = "3112"
  1250.     
  1251.     'Left hand EVEN
  1252.     EANUPC_LHE(0) = "1123": EANUPC_LHE(1) = "1222"
  1253.     EANUPC_LHE(2) = "2212": EANUPC_LHE(3) = "1141"
  1254.     EANUPC_LHE(4) = "2311": EANUPC_LHE(5) = "1321"
  1255.     EANUPC_LHE(6) = "4111": EANUPC_LHE(7) = "2131"
  1256.     EANUPC_LHE(8) = "3121": EANUPC_LHE(9) = "2113"
  1257.     
  1258.     'Right hand encoding
  1259.     EANUPC_RH(0) = "3211": EANUPC_RH(1) = "2221"
  1260.     EANUPC_RH(2) = "2122": EANUPC_RH(3) = "1411"
  1261.     EANUPC_RH(4) = "1132": EANUPC_RH(5) = "1231"
  1262.     EANUPC_RH(6) = "1114": EANUPC_RH(7) = "1312"
  1263.     EANUPC_RH(8) = "1213": EANUPC_RH(9) = "3112"
  1264.     
  1265.     'EAN UPCa Parity values "1" = Odd, "0" = Even
  1266.     EANUPC_Parity(0) = "111111": EANUPC_Parity(1) = "110100"
  1267.     EANUPC_Parity(2) = "110010": EANUPC_Parity(3) = "110001"
  1268.     EANUPC_Parity(4) = "101100": EANUPC_Parity(5) = "100110"
  1269.     EANUPC_Parity(6) = "100011": EANUPC_Parity(7) = "101010"
  1270.     EANUPC_Parity(8) = "101001": EANUPC_Parity(9) = "100101"
  1271.     
  1272.     'UPC-E parity values "1" = Odd, "0" = Even
  1273.     UPCe_Parity(0) = "000111": UPCe_Parity(1) = "001011"
  1274.     UPCe_Parity(2) = "001101": UPCe_Parity(3) = "001110"
  1275.     UPCe_Parity(4) = "010011": UPCe_Parity(5) = "011001"
  1276.     UPCe_Parity(6) = "011100": UPCe_Parity(7) = "010101"
  1277.     UPCe_Parity(8) = "010110": UPCe_Parity(9) = "011010"
  1278.     
  1279.     '2 digit extention parity values "1" = ODD, "0" = EVEN
  1280.     Plus2_Parity(0) = "11": Plus2_Parity(1) = "10"
  1281.     Plus2_Parity(2) = "01": Plus2_Parity(3) = "00"
  1282.     
  1283.     '5 digit extention parity values "1" = ODD, "0" = EVEN
  1284.     Plus5_Parity(0) = "00111": Plus5_Parity(1) = "01011"
  1285.     Plus5_Parity(2) = "01101": Plus5_Parity(3) = "01110"
  1286.     Plus5_Parity(4) = "10011": Plus5_Parity(5) = "11001"
  1287.     Plus5_Parity(6) = "11100": Plus5_Parity(7) = "10101"
  1288.     Plus5_Parity(8) = "10110": Plus5_Parity(9) = "11010"
  1289. End Sub
  1290.  
  1291.  
  1292.  
  1293.