设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2826|回复: 0
打印 上一主题 下一主题

VB_条码打印

[复制链接]

16

主题

0

好友

216

积分

中级会员

Rank: 3Rank: 3

生肖
性别

最佳新人 论坛元老

跳转到指定楼层
楼主
发表于 2012-4-28 21:54:19 |只看该作者 |倒序浏览
本帖最后由 sky_yx 于 2015-12-30 14:23 编辑

'CODE39码的编码规则是:
'1、每五条线表示一个字符;
'2、粗线表示1,细线表示0;
'3、线条间的间隙宽的表示1,窄的表示0;
'4、五条线加上它们之间的四条间隙就是九位二进制编码,而且这九位中必定有三位是1,所以称为39码;
'5、条形码的首尾各一个 * 标识开始和结束

  1. Public Sub PrintBarCode(ByVal strBarCode As String, _
  2.      Optional ByVal intXPos As Integer = 0, _
  3.      Optional ByVal intYPos As Integer = 0, _
  4.      Optional ByVal intPrintHeight As Integer = 10, _
  5.      Optional ByVal bolPrintText As Boolean = True)
  6. '参数说明:
  7. 'strBarCode -要打印的条形码字符串
  8. 'intXPos, intYPos - 打印条形码的左上角坐标(缺省为(0,0),坐标刻度为:毫米)
  9. 'intHeight     - 打印高度(缺省为一厘米,坐标刻度为:毫米)
  10. 'bolPrintText -是否打印人工识别字符(缺省为true)
  11. '"0"-"9","A-Z","-","%","$"和"*" 的条码编码格式,总共 40 个字符
  12. Static strBarTable(39) As String
  13. '初始化条码编码格式表
  14.      strBarTable(0) = "001100100"     ' 0
  15.      strBarTable(1) = "100010100"     ' 1
  16.      strBarTable(2) = "010010100"     ' 2
  17.      strBarTable(3) = "110000100"     ' 3
  18.      strBarTable(4) = "001010100"     ' 4
  19.      strBarTable(5) = "101000100"     ' 5
  20.      strBarTable(6) = "011000100"     ' 6
  21.      strBarTable(7) = "000110100"     ' 7
  22.      strBarTable(8) = "100100100"     ' 8
  23.      strBarTable(9) = "010100100"     ' 9
  24.      strBarTable(10) = "100010010"    ' A
  25.      strBarTable(11) = "010010010"    ' B
  26.      strBarTable(12) = "110000010"    ' C
  27.      strBarTable(13) = "001010010"    ' D
  28.      strBarTable(14) = "101000010"    ' E
  29.      strBarTable(15) = "011000010"    ' F
  30.      strBarTable(16) = "000110010"    ' G
  31.      strBarTable(17) = "100100010"    ' H
  32.      strBarTable(18) = "010100010"    ' I
  33.      strBarTable(19) = "001100010"    ' J
  34.      strBarTable(20) = "100010001"    ' K
  35.      strBarTable(21) = "010010001"    ' L
  36.      strBarTable(22) = "110000001"    ' M
  37.      strBarTable(23) = "001010001"    ' N
  38.      strBarTable(24) = "101000001"    ' O
  39.      strBarTable(25) = "011000001"    ' P
  40.      strBarTable(26) = "000110001"    ' Q
  41.      strBarTable(27) = "100100001"    ' R
  42.      strBarTable(28) = "010100001"    ' S
  43.      strBarTable(29) = "001100001"    ' T
  44.      strBarTable(30) = "100011000"    ' U
  45.      strBarTable(31) = "010011000"    ' V
  46.      strBarTable(32) = "110001000"    ' W
  47.      strBarTable(33) = "001011000"    ' X
  48.      strBarTable(34) = "101001000"    ' Y
  49.      strBarTable(35) = "011001000"    ' Z
  50.      strBarTable(36) = "000111000"    ' -
  51.      strBarTable(37) = "100101000"    ' %
  52.      strBarTable(38) = "010101000"    ' $
  53.      strBarTable(39) = "001101000"    ' *
  54.      If strBarCode = "" Then Exit Sub ' 不打印空串
  55. '       保存打印机 ScaleMode
  56.      Dim intOldScaleMode As ScaleModeConstants
  57.      intOldScaleMode = Form1.ScaleMode
  58. '       保存打印机 DrawWidth
  59.      Dim intOldDrawWidth As Integer
  60.      intOldDrawWidth = Form1.DrawWidth
  61. '       保存打印机 Font
  62.      Dim fntOldFont As StdFont
  63.      Set fntOldFont = Form1.Font
  64.      Form1.ScaleMode = vbTwips ' 设置打印用的坐标刻度为缇(twip=1)
  65.      Form1.DrawWidth = 1     ' 线宽为 1
  66.      Form1.FontName = "宋体" ' 打印在条码下方字符的字体和大小
  67.      Form1.FontSize = 10
  68.      Dim strBC As String         ' 要打印的条码字符串
  69.      strBC = UCase(strBarCode)
  70.      ' 将以毫米表示的 X 坐标转换为以缇表示
  71.      Dim x As Integer
  72.      x = Form1.ScaleX(intXPos, vbMillimeters, vbTwips)
  73.      ' 将以毫米表示的 Y 坐标转换为以缇表示
  74.      Dim y As Integer
  75.      y = Form1.ScaleY(intYPos, vbMillimeters, vbTwips)
  76. '       将以毫米表示的高度转换为以缇表示
  77.      Dim intHeight As Integer
  78.      intHeight = Form1.ScaleY(intPrintHeight, vbMillimeters, vbTwips)
  79. '       是否在条形码下方打印人工识别字符
  80.      If bolPrintText = True Then
  81. '           条码打印高度要减去下面的字符显示高度
  82.          intHeight = intHeight - Form1.TextHeight(strBC)
  83.      End If
  84.      Const intWidthCU As Integer = 30 ' 粗线和宽间隙宽度
  85.      Const intWidthXI As Integer = 10 ' 细线和窄间隙宽度
  86.      Dim intIndex As Integer            ' 当前处理的字符串索引
  87.      Dim i As Integer, j As Integer, k As Integer    ' 循环控制变量
  88. '       添加起始字符
  89.      If Left(strBC, 1) <> "*" Then
  90.          strBC = "*" & strBC
  91.      End If
  92. '       添加结束字符
  93.      If Right(strBC, 1) <> "*" Then
  94.          strBC = strBC & "*"
  95.      End If
  96. '       循环处理每个要显示的条码字符
  97.      For i = 1 To Len(strBC)
  98.          ' 确定当前字符在 strBarTable 中的索引
  99.          Select Case Mid(strBC, i, 1)
  100.          Case "*"
  101.              intIndex = 39
  102.          Case "$"
  103.              intIndex = 38
  104.          Case "%"
  105.              intIndex = 37
  106.          Case "-"
  107.              intIndex = 36
  108.          Case "0" To "9"
  109.              intIndex = CInt(Mid(strBC, i, 1))
  110.          Case "A" To "Z"
  111.              intIndex = Asc(Mid(strBC, i, 1)) - Asc("A") + 10
  112.          Case Else
  113.              MsgBox "要打印的条形码字符串中包含无效字符!当前版本只支持字符 '0-9,A-Z,-,%,$*"
  114.          End Select
  115. '           是否在条形码下方打印人工识别字符
  116.          If bolPrintText = True Then
  117.              Form1.CurrentX = x
  118.              Form1.CurrentY = y + intHeight
  119.              Form1.Print Mid(strBC, i, 1)
  120.          End If
  121.          For j = 1 To 5
  122. '               画细线
  123.              If Mid(strBarTable(intIndex), j, 1) = "0" Then
  124.                  For k = 0 To intWidthXI - 1
  125.                      Form1.Line (x + k, y)-Step(0, intHeight)
  126.                  Next k
  127.                  x = x + intWidthXI
  128. '               画宽线
  129.              Else
  130.                  For k = 0 To intWidthCU - 1
  131.                      Form1.Line (x + k, y)-Step(0, intHeight)
  132.                  Next k
  133.                  x = x + intWidthCU
  134.              End If
  135. '               每个字符条码之间为窄间隙
  136.              If j = 5 Then
  137.                  x = x + intWidthXI * 3
  138.                  Exit For
  139.              End If
  140. '               窄间隙
  141.              If Mid(strBarTable(intIndex), j + 5, 1) = "0" Then
  142.                  x = x + intWidthXI * 3
  143. '               宽间隙
  144.              Else
  145.                  x = x + intWidthCU * 2
  146.              End If
  147.          Next j
  148.      Next i
  149. '       恢复打印机 ScaleMode
  150.      Form1.ScaleMode = intOldScaleMode
  151. '       恢复打印机 DrawWidth
  152.      Form1.DrawWidth = intOldDrawWidth
  153. '       恢复打印机 Font
  154.      Set Form1.Font = fntOldFont
  155. End Sub
复制代码


分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享淘帖0 收藏收藏0 评分评分
您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-4-25 19:09 , Processed in 0.110811 second(s), 27 queries .

回顶部