在窗口Form中加三個控件:
文本框:Text1
命令按鈕:Command1
圖片框:PT1
把下列代碼copy進窗口程序:
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 _
)
 
' ???明:
' strBarCode        - 要打印的?形?字符串
' intXPos, intYPos  - 打印?形?的左上角坐?(缺省?(0,0),坐?刻度?:毫米)
' intHeight         - 打印高度(缺省?一厘米,坐?刻度?:毫米)
' bolPrintText      - 是否打印人工??字符(缺省?true)
 
' "0"-"9","A-Z","-","%","$"和"*" 的????格式,?共 40 ?字符
Static strBarTable(39) As String
 
' 初始化????格式表
 
    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"    ' *
 
    If strBarCode = "" Then Exit Sub   ' 不打印空串
 
    ' 保存打印机 ScaleMode
    Dim intOldScaleMode As ScaleModeConstants
    intOldScaleMode = PT1.ScaleMode
    ' 保存打印机 DrawWidth
    Dim intOldDrawWidth As Integer
    intOldDrawWidth = PT1.DrawWidth
    ' 保存打印机 Font
    
    Dim fntOldFont As StdFont
    Set fntOldFont = PT1.Font
    
    PT1.ScaleMode = vbTwips
         ' ?置打印用的坐?刻度??(twip=1)
    PT1.DrawWidth = 1
         ' ??? 1
 '   pt1.FontName = "宋体"
         ' 打印在??下方字符的字体和大小
    PT1.FontSize = 10
    
    Dim strBC As String
         ' 要打印的??字符串
    strBC = UCase(strBarCode)
    ' ?以毫米表示的 X 坐????以?表示
    Dim x As Integer
    x = PT1.ScaleX(intXPos, vbMillimeters, vbTwips)
    ' ?以毫米表示的 Y 坐????以?表示
    Dim y As Integer
    y = PT1.ScaleY(intYPos, vbMillimeters, vbTwips)
    ' ?以毫米表示的高度???以?表示
    Dim intHeight As Integer
    intHeight = PT1.ScaleY(intPrintHeight, vbMillimeters, vbTwips)
    
    ' 是否在?形?下方打印人工??字符
    If bolPrintText = True Then
        ' ??打印高度要?去下面的字符?示高度
        intHeight = intHeight - PT1.TextHeight(strBC)
    End If
    
    Const intWidthCU As Integer = 30
         ' 粗?和??隙?度
    Const intWidthXI As Integer = 10
         ' ??和窄?隙?度
    Dim intIndex As Integer
         ' ?前?理的字符串索引
    Dim i As Integer, j As Integer, k As Integer            ' 循?控制?量
 
    ' 添加起始字符
    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 "要打印的?形?字符串中包含?效字符!"
                          Print 前版本只支持字符
                          '0'-'9','A'-'Z','-','%','$'和'*'"
        End Select
        
        ' 是否在?形?下方打印人工??字符
        If bolPrintText = True Then
            PT1.CurrentX = x
            PT1.CurrentY = y + intHeight
            PT1.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
                    PT1.Line (x + k, y)-Step(0, intHeight)
                    
                Next k
                x = x + intWidthXI
            ' ???
            Else
                For k = 0 To intWidthCU - 1
                    PT1.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
    PT1.ScaleMode = intOldScaleMode
    ' 恢复打印机 DrawWidth
    PT1.DrawWidth = intOldDrawWidth
    ' 恢复打印机 Font
    Set PT1.Font = fntOldFont
End Sub
Private Sub Command1_Click()
    PT1.Cls
    PrintBarCode Text1.Text, 10, 10, 20, True
        
End SubPrivate Sub Text1_Change()
        PT1.Cls
    PrintBarCode Text1.Text, 10, 10, 20, TrueEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1.SetFocus
    End If
End Sub