我做过给你
Private strBarTable(39) As String '39条码表
Private strBarTable128(107) As String '128条码表
Public Declare Function MessageBeep Lib "user32" (ByVal wType As Long) As Long '警告声
Public Const MB_ICONEXCLAMATION = &H30&
'--------------------------------------------------------
'CODE39码的编码规则是:
' 1、每五条线表示一个字符;
' 2、粗线表示1,细线表示0;
' 3、线条间的间隙宽的表示1,窄的表示0;
' 4、五条线加上它们之间的四条间隙就是九位二进制编码,而且这九位中必定有三位是1;
' 5、 条形码的首尾各一个 * 标识开始和结束
' 解析时先读线宽再读间歇
'--------------------------------------------------------
'------------------------------------------------------------------------------------
'过程 Init_BarTable()初始化条码表
'
'入口参数: 无
'
'返回值:无
'------------------------------------------------------------------------------------
Private Sub Init_BarTable()
' "0"-"9","A-Z","-","%","$"和"*" 的条码编码格式,总共 40 个字符
' 初始化条码编码格式表
strBarTable(0) = "001100100" ' 0
strBarTable(1) = "100010100" ' 1
strBarTable(2) = "010010100" ' 2
strBarTable(3) = "110000100" ' 3
strBarTable(4) = "001010100" ' 4
strBarTable(5) = "101000100" ' 5
strBarTable(6) = "011000100" ' 6
strBarTable(7) = "000110100" ' 7
strBarTable(8) = "100100100" ' 8
strBarTable(9) = "010100100" ' 9
strBarTable(10) = "100010010" ' A
strBarTable(11) = "010010010" ' B
strBarTable(12) = "110000010" ' C
strBarTable(13) = "001010010" ' D
strBarTable(14) = "101000010" ' E
strBarTable(15) = "011000010" ' F
strBarTable(16) = "000110010" ' G
strBarTable(17) = "100100010" ' H
strBarTable(18) = "010100010" ' I
strBarTable(19) = "001100010" ' J
strBarTable(20) = "100010001" ' K
strBarTable(21) = "010010001" ' L
strBarTable(22) = "110000001" ' M
strBarTable(23) = "001010001" ' N
strBarTable(24) = "101000001" ' O
strBarTable(25) = "011000001" ' P
strBarTable(26) = "000110001" ' Q
strBarTable(27) = "100100001" ' R
strBarTable(28) = "010100001" ' S
strBarTable(29) = "001100001" ' T
strBarTable(30) = "100011000" ' U
strBarTable(31) = "010011000" ' V
strBarTable(32) = "110001000" ' W
strBarTable(33) = "001011000" ' X
strBarTable(34) = "101001000" ' Y
strBarTable(35) = "011001000" ' Z
strBarTable(36) = "000111000" ' -
strBarTable(37) = "100101000" ' %
strBarTable(38) = "010101000" ' $
strBarTable(39) = "001101000" ' *
End Sub
'------------------------------------------------------------------------------------
'过程 PrintBarCode打印条码
'入口参数: strBarCode - 要打印的条形码字符串
' intXPos, intYPos - 打印条形码的左上角坐标(缺省为(0,0),坐标刻度为:毫米)
' intHeight - 打印高度(缺省为10毫米,坐标刻度为:毫米)
' bolPrintText - 是否打印人工识别字符(缺省为true)
'
'返回值:无
'------------------------------------------------------------------------------------
Public 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)
'初始化条码表
Init_BarTable
If strBarCode = "" then
Exit Sub ' 不打印空串
' 保存打印机 ScaleMode
Dim intOldScaleMode As ScaleModeConstants
intOldScaleMode = Printer.ScaleMode
' 保存打印机 DrawWidth
Dim intOldDrawWidth As Integer
intOldDrawWidth = Printer.DrawWidth
' 保存打印机 Font
Dim fntOldFont As StdFont
Set fntOldFont = Printer.Font
Printer.ScaleMode = vbTwips ' 设置打印用的坐标刻度为缇(twip=1)
Printer.DrawWidth = 1 ' 线宽为 1
Printer.FontName = "宋体" ' 打印在条码下方字符的字体和大小
Printer.FontSize = 10
Dim strBC As String ' 要打印的条码字符串
strBC = UCase(strBarCode)
' 将以毫米表示的 X 坐标转换为以缇表示
Dim X As Integer
' 将以毫米表示的 Y 坐标转换为以缇表示
X = Printer.ScaleX(intXPos, vbMillimeters, vbTwips)
Dim Y As Integer
' 将以毫米表示的高度转换为以缇表示
Y = Printer.ScaleY(intYPos, vbMillimeters, vbTwips)
' 将以毫米表示的条码高度转换为以缇表示
Dim intHeight As Integer
intHeight = Printer.ScaleY(intPrintHeight, vbMillimeters, vbTwips)
' 是否在条形码下方打印人工识别字符
If bolPrintText = True then
' 条码打印高度要减去下面的字符显示高度
intHeight = intHeight - Printer.TextHeight(strBC)
End If
Const intWidthCU As Integer = 24 '30 ' 粗线和宽间隙宽度
Const intWidthXI As Integer = 8 '10 ' 细线和窄间隙宽度
Dim intIndex As Integer ' 当前处理的字符串索引
Dim i As Integer, J As Integer, k As Integer ' 循环控制变量
' 添加起始字符* --- CODE39码规定
If Left(strBC, 1) <> "*" then
strBC = "*" &
strBC
End If
' 添加结束字符
If Right(strBC, 1) <> "*" then
strBC = strBC &
"*"
End If
' 循环处理每个要显示的条码字符
For i = 1 To Len(strBC)
' 确定当前字符在 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 "要打印的条形码字符串中包含无效字符!当前版本只支持字符 '0'-'9','A'-'Z','-','%','$'和'*'"
End Select
' 是否在条形码下方打印人工识别字符 但不打印标志"*"
If bolPrintText = True And i <> 1 And i <> Len(strBC) then
Printer.CurrentX = X
Printer.CurrentY = Y + intHeight
Printer.Print Mid(strBC, i, 1)
End If
For J = 1 To 5
' 画细线
If Mid(strBarTable(intIndex), J, 1) = "0" then
For k = 0 To intWidthXI - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next k
X = X + intWidthXI
' 画宽线
else
For k = 0 To intWidthCU - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next k
X = X + intWidthCU
End If
' 每个字符条码之间为窄间隙
If J = 5 then
X = X + intWidthXI * 3
Exit For
End If
' 窄间隙
If Mid(strBarTable(intIndex), J + 5, 1) = "0" then
X = X + intWidthXI * 3
' 宽间隙
else
X = X + intWidthCU * 2
End If
Next J
Next i
' 恢复打印机 ScaleMode
Printer.ScaleMode = intOldScaleMode
' 恢复打印机 DrawWidth
Printer.DrawWidth = intOldDrawWidth
' 恢复打印机 Font
Set Printer.Font = fntOldFont
Printer.EndDoc
End Sub
'------------------------------------------------------------------------------------
'过程 Init_BarTable128()初始化条码表
'
'入口参数: 无
'
'返回值:无
'------------------------------------------------------------------------------------
Private Sub Init_BarTable128()
' 初始化条码编码格式表
strBarTable128(0) = "11011001100" ' 00
strBarTable128(1) = "11001101100" ' 01
strBarTable128(2) = "11001100110" ' 02
strBarTable128(3) = "10011011000" ' 03
strBarTable128(4) = "10010001100" ' 04
strBarTable128(5) = "10001001100" ' 5
strBarTable128(6) = "10011001000" ' 6
strBarTable128(7) = "10011000100" ' 7
strBarTable128(8) = "10001100100" ' 8
strBarTable128(9) = "11001001000" ' 9
strBarTable128(10) = "11001000100" ' 10
strBarTable128(11) = "11000100100" ' 11
strBarTable128(12) = "10110011100" ' 12
strBarTable128(13) = "10011011100" ' 13
strBarTable128(14) = "10011001110" ' 14
strBarTable128(15) = "10111001100" ' 15
strBarTable128(16) = "10011101100" ' 16
strBarTable128(17) = "10011100110" ' 17
strBarTable128(18) = "11001110010" ' 18
strBarTable128(19) = "11001011100" ' 19
strBarTable128(20) = "11001001110" ' 20
strBarTable128(21) = "11011100100" ' 21
strBarTable128(22) = "11001110100" ' 22
strBarTable128(23) = "11101101110" ' 23
strBarTable128(24) = "11101001100" ' 24
strBarTable128(25) = "11100101100" ' 25
strBarTable128(26) = "11100100110" ' 26
strBarTable128(27) = "11101100100" ' 27
strBarTable128(28) = "11100110100" ' 28
strBarTable128(29) = "11100110010" ' 29
strBarTable128(30) = "11011011000" ' 30
strBarTable128(31) = "11011000110" ' 31
strBarTable128(32) = "11000110110" ' 32
strBarTable128(33) = "10100011000" ' 33
strBarTable128(34) = "10001011000" ' 34
strBarTable128(35) = "10001000110" ' 35
strBarTable128(36) = "10110001000" ' 36
strBarTable128(37) = "10001101000" ' 37
strBarTable128(38) = "10001100010" ' 38
strBarTable128(39) = "11010001000" ' 39
strBarTable128(40) = "11000101000" ' 40
strBarTable128(41) = "11000100010" ' 41
strBarTable128(42) = "10110111000" ' 42
strBarTable128(43) = "10110001110" ' 43
strBarTable128(44) = "10001101110" ' 44
strBarTable128(45) = "10111011000" ' 45
strBarTable128(46) = "10111000110" ' 46
strBarTable128(47) = "10001110110" ' 47
strBarTable128(48) = "11101110110" ' 48
strBarTable128(49) = "11010001110" ' 49
strBarTable128(50) = "11000101110" ' 50
strBarTable128(51) = "11011101000" ' 51
strBarTable128(52) = "11011100010" ' 52
strBarTable128(53) = "11011101110" ' 53
strBarTable128(54) = "11101011000" ' 54
strBarTable128(55) = "11101000110" ' 55
strBarTable128(56) = "11100010110" ' 56
strBarTable128(57) = "11101101000" ' 57
strBarTable128(58) = "11101100010" ' 58
strBarTable128(59) = "11100011010" ' 59
strBarTable128(60) = "11101111010" ' 60
strBarTable128(61) = "11001000010" ' 61
strBarTable128(62) = "11110001010" ' 62
strBarTable128(63) = "10100110000" ' 63
strBarTable128(64) = "10100001100" ' 64
strBarTable128(65) = "10010110000" ' 65
strBarTable128(66) = "10010000110" ' 66
strBarTable128(67) = "10000101100" ' 67
strBarTable128(68) = "10000100110" ' 68
strBarTable128(69) = "10110010000" ' 69
strBarTable128(70) = "10110000100" ' 70
strBarTable128(71) = "10011010000" ' 71
strBarTable128(72) = "10011000010" ' 72
strBarTable128(73) = "10000110100" ' 73
strBarTable128(74) = "10000110010" ' 74
strBarTable128(75) = "11000010010" ' 75
strBarTable128(76) = "11001010000" ' 76
strBarTable128(77) = "11110111010" ' 77
strBarTable128(78) = "11000010100" ' 78
strBarTable128(79) = "10001111010" ' 79
strBarTable128(80) = "10100111100" ' 80
strBarTable128(81) = "10010111100" ' 81
strBarTable128(82) = "10010011110" ' 82
strBarTable128(83) = "10111100100" ' 83
strBarTable128(84) = "10011110100" ' 84
strBarTable128(85) = "10011110010" ' 85
strBarTable128(86) = "11110100100" ' 86
strBarTable128(87) = "11110010100" ' 87
strBarTable128(88) = "11110010010" ' 88
strBarTable128(89) = "11011011110" ' 89
strBarTable128(90) = "11011110110" ' 90
strBarTable128(91) = "11110110110" ' 91
strBarTable128(92) = "10101111000" ' 92
strBarTable128(93) = "10100011110" ' 93
strBarTable128(94) = "10001011110" ' 94
strBarTable128(95) = "10111101000" ' 95
strBarTable128(96) = "10111100010" ' 96
strBarTable128(97) = "11110101000" ' 97
strBarTable128(98) = "11110100010" ' 98
strBarTable128(99) = "10111011110" ' 99
strBarTable128(100) = "10111101110" ' CODE B
strBarTable128(101) = "11101011110" ' CODE A
strBarTable128(102) = "11110101110" ' FNCI
strBarTable128(103) = "11010000100" ' START A
strBarTable128(104) = "11010010000" ' START B
strBarTable128(105) = "11010011100" ' START C
strBarTable128(106) = "11000111010" ' STOP
End Sub
'------------------------------------------------------------------------------------
'过程 PrintBarCode打印128条码 本过程选CODE C字符集
'入口参数: strBarCode - 要打印的条形码字符串
' intXPos, intYPos - 打印条形码的左上角坐标(缺省为(0,0),坐标刻度为:毫米)
' intHeight - 打印高度(缺省为10毫米,坐标刻度为:毫米)
' bolPrintText - 是否打印人工识别字符(缺省为true)
'
'返回值:无
'------------------------------------------------------------------------------------
Public Sub PrintBarCode128(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)
'初始化条码表
Init_BarTable128
'If strBarCode = "" then
Exit Sub ' 不打印空串
' 保存打印机 ScaleMode
Dim intOldScaleMode As ScaleModeConstants
intOldScaleMode = Printer.ScaleMode
' 保存打印机 DrawWidth
Dim intOldDrawWidth As Integer
intOldDrawWidth = Printer.DrawWidth
' 保存打印机 Font
Dim fntOldFont As StdFont
Set fntOldFont = Printer.Font
Printer.ScaleMode = vbTwips ' 设置打印用的坐标刻度为缇(twip=1)
Printer.DrawWidth = 1 ' 线宽为 1
Printer.FontName = "宋体" ' 打印在条码下方字符的字体和大小
Printer.FontSize = 10
Dim strBC As String ' 要打印的条码字符串
strBC = UCase(strBarCode)
' 将以毫米表示的 X 坐标转换为以缇表示
Dim X As Integer
' 将以毫米表示的 Y 坐标转换为以缇表示
X = Printer.ScaleX(intXPos, vbMillimeters, vbTwips)
Dim Y As Integer
' 将以毫米表示的高度转换为以缇表示
Y = Printer.ScaleY(intYPos, vbMillimeters, vbTwips)
' 将以毫米表示的条码高度转换为以缇表示
Dim intHeight As Integer
intHeight = Printer.ScaleY(intPrintHeight, vbMillimeters, vbTwips)
' 是否在条形码下方打印人工识别字符
If bolPrintText = True then
' 条码打印高度要减去下面的字符显示高度
intHeight = intHeight - Printer.TextHeight(strBC)
End If
Const intWidth As Integer = 20 '10 ' 条码宽度
Dim CheckCode As Long '效验码
Dim intIndex As Integer ' 当前处理的字符串索引
Dim i As Integer, J As Integer, k As Integer ' 循环控制变量
'打印CODE C开始字符 即105的值
For J = 1 To 11
If Mid(strBarTable128(105), J, 1) = "1" then
For k = 0 To intWidth - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next
X = X + intWidth
else
X = X + intWidth * 2
End If
Next
CheckCode = 105
' 循环处理每个要显示的条码字符
For i = 1 To Len(strBC) / 2
'每次取2个数字
'确定当前字符在 strBarTable128 中的索引
intIndex = Mid(strBC, (i - 1) * 2 + 1, 2)
'计算效验码
CheckCode = CheckCode + (i + 1) * Mid(strBC, (i - 1) * 2 + 1, 2)
' 是否在条形码下方打印人工识别字符
If bolPrintText = True then
Printer.CurrentX = X
Printer.CurrentY = Y + intHeight
Printer.Print Mid(strBC, (i - 1) * 2 + 1, 2)
End If
For J = 1 To 11
' 画细线
If Mid(strBarTable128(intIndex), J, 1) = "1" then
For k = 0 To intWidth - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next k
X = X + intWidth
else
X = X + intWidth * 2
End If
Next J
Next i
'打印效验码
intIndex = CheckCode Mod 103
For J = 1 To 11
' 画细线
If Mid(strBarTable128(intIndex), J, 1) = "1" then
For k = 0 To intWidth - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next k
X = X + intWidth
else
X = X + intWidth * 2
End If
Next J
'打印终止符
intIndex = 106
For J = 1 To 11
' 画细线
If Mid(strBarTable128(intIndex), J, 1) = "1" then
For k = 0 To intWidth - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next k
X = X + intWidth
else
X = X + intWidth * 2
End If
Next J
'打印2个单位宽的结束符
' 画细线
For k = 0 To intWidth * 2 - 1
Printer.Line (X + k, Y)-Step(0, intHeight)
Next k
' 恢复打印机 ScaleMode
Printer.ScaleMode = intOldScaleMode
' 恢复打印机 DrawWidth
Printer.DrawWidth = intOldDrawWidth
' 恢复打印机 Font
Set Printer.Font = fntOldFont
End Sub