明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

條形碼編程摘錄

[摘要]廣東 李玉君 一、條形碼的讀取 用過鍵盤口式的掃條碼工具的朋友就知道,它就如同在鍵盤上按下數字鍵一樣,基本不需任何編程和處理。但如果你使用的是其它接口的話,可能你就要為該設備編寫通訊代碼了。以下有一段簡單的25針串口的條碼讀取器通訊代碼。 Option Explicit Dim...
廣東 李玉君

  
一、條形碼的讀取
    用過鍵盤口式的掃條碼工具的朋友就知道,它就如同在鍵盤上按下數字鍵一樣,基本不需任何編程和處理。但如果你使用的是其它接口的話,可能你就要為該設備編寫通訊代碼了。以下有一段簡單的25針串口的條碼讀取器通訊代碼。
    Option Explicit
    Dim sData As String
    Private Sub Form_Load()
    With MSComm1
       .CommPort = 3 '設為COM3,試運行的系統(tǒng)而定,你可提供一個Combox讓用戶選擇。
       .PortOpen = True '打開通訊端口
    End With
    End Sub

    Private Sub MSComm1_OnComm()
        Dim EndPos As Integer
        Select Case MSComm1.CommEvent
        Case comEvReceive '當有數據傳送過來時
            sData = sData & Trim(MSComm1.Input)
            '檢索回車,通常讀卡機每組數據結尾都返回一個回車作為結束符
             EndPos = InStr(1, sData, Chr(13))
             If EndPos = 0 Then '如果未結束就繼續(xù)努力
             Else '讀完一組。
             lblBarCode.Caption = sData '顯示一組條形碼
             With lstBarCode
                 .AddItem Mid(sData, 1, EndPos - 1) '添加一組條形碼到列表
             End With
             sData = "" '清空
             End If
         End Select
    End Sub

    Private Sub cmdEnd_Click()
        MSComm1.PortOpen = False '關閉端口
        End
    End Sub
    
二、條形碼的生成
    看完以上關于條碼讀取的代碼是否覺得很容易呢?對,在VB上編程本來就不難。以下關于條形碼生成的代碼也是很容易理解,只需使用一個OFFICE的附帶的BarCode控件就可以輕松打印出11種不同標準的條形碼,足以滿足我們的要求。想起我書架上的一本書中的一篇用Turbo C編寫條形碼打印程序文章,長篇大論,那時不知看了n天,打了n小時字結果也不盡人意,現在真是幸福多了:)。廢話說完,得回歸正題。且看條形碼生成的代碼及有關說明。
    源代碼主要由兩個窗體(frmMain主窗體和frmOption條碼設置窗體)和兩個模塊組成(modGetScreen.bas、SysDLG32.bas)?紤]到篇幅,這里只列出部分較為關鍵的代碼。
    新建一個標準工程,添加一個名為(Microsoft Access BarCode Control9)的條形碼部件,并添加一個條碼控件到窗口,并將窗口改名為frmMain,如圖所示。由于控件比較多,這里不便細說,詳細內容請看源代碼。
    模塊modGetScreen.bas代碼如下:
    Option Explicit
    '聲明BitBlt、GetDesktopWindow、GetWindowDC、ReleaseDC這幾個API函數略
    Public RegUser As Boolean

    Sub GetObjImage1(Obj As Object, OwnerForm As PictureBox, Picture1 As PictureBox)
     'hDC
        Dim hWndDesk As Long
        Dim hDCDesk As Long
        '區(qū)域表達變量
        Dim x As Long
        Dim y As Long
        Dim w As Long
        Dim h As Long
        x = Obj.Left  Screen.TwipsPerPixelX
        y = Obj.Top  Screen.TwipsPerPixelY
        w = Obj.Width  Screen.TwipsPerPixelX
        h = Obj.Height  Screen.TwipsPerPixelY
        hDCDesk = OwnerForm.hdc
        '取出圖像
        Call BitBlt(Picture1.hdc, 0, 0, w, h, hDCDesk, x, y, vbSrcCopy)
        Call ReleaseDC(hWndDesk, hDCDesk)
    End Sub

     
    主窗體frmMain.frm部分代碼如下:
    Private Sub cmdPrint_Click()
    '生成條形碼圖像
        Dim r As Long, i As Integer, t As String,cfile As String '臨時變量
        t = BarCode
        For i = 0 To Val(Times) - 1
            BarCode1.Value = BarCode + i
            DoEvents
            Picture1.Refresh
            GetObjImage1 BarCode1, Conel, Picture1
            If RegUser = False Then '如果未注冊添加MASK標記
                Picture1.PaintPicture Picture2.Picture, 300, 300
            End If
            If Dir(SavePath, vbDirectory) = "" Then MkDir SavePath
            SavePath = SavePath & IIf(Right(SavePath, 1) <> "", "", "")
            cfile = SavePath & BarCode1.Value & ".bmp"
            SavePicture Picture1.Image, cfile '將條形碼保存為圖像文件以便打印
        Next
        BarCode = t
    End Sub

     
    條形碼設置窗體frmOption.frm代碼如下:
    Option Explicit
    '條形碼設置模塊

    Private Sub cboBig_Click()
        BarCode1.Style = cboBig.ListIndex '改變標準
    End Sub

    Private Sub cboDirection_Click()
        BarCode1.Direction = cboDirection.ListIndex '改變方向
    End Sub
    
    Private Sub cboLine_Click()
        BarCode1.LineWeight = cboLine.ListIndex '改變線寬
    End Sub

    Private Sub cboSmall_Click()
        BarCode1.SubStyle = cboSmall.ListIndex '改變樣式
    End Sub

    Private Sub Check1_Click()
        BarCode1.ShowData = Check1.Value '是否顯示數據
    End Sub

    Private Sub cmdChange_Click()
    '設置長、寬大小
        BarWidth = BarCode1.Height
        BarHeight = BarCode1.Width
        cmdRefresh_Click
    End Sub

    Private Sub cmdOK_Click()
    '傳送條形碼設定到主界面
    With frmMain.BarCode1
        .LineWeight = BarCode1.LineWeight
        .Style = BarCode1.Style
        .SubStyle = BarCode1.SubStyle
        .Direction = BarCode1.Direction
        .Width = BarCode1.Width
        .Height = BarCode1.Height
        .ShowData = BarCode1.ShowData
        Me.Hide
    End With
    With frmMain
        .Picture1.Width = .BarCode1.Width
        .Picture1.Height = .BarCode1.Height
        .Conel.Width = .BarCode1.Width
        .Conel.Height = .BarCode1.Height
    End With
    End Sub
     
    Private Sub cmdRefresh_Click()
        BarCode1.Width = BarWidth
        BarCode1.Height = BarHeight
    End Sub
     
    Private Sub Form_Load()
        LoadBarInfo
        BarWidth = BarCode1.Width
        BarHeight = BarCode1.Height
    End Sub

    Sub LoadBarInfo() '初始化選項
        LoadBigClass cboBig
        LoadSmallClass cboSmall
        LoadLineSize cboLine
        LoadDirection cboDirection
    End Sub


    Sub LoadBigClass(cbo As ComboBox) '條碼標準
    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 "U.S. Postnet"
        .AddItem "U.S. Postal FIM"
        .AddItem "JP Post"
        .ListIndex = 2
    End With
    End Sub


    Sub LoadSmallClass(cbo As ComboBox) '條碼樣式
    With cbo
        .AddItem "Standard"
        .AddItem "2-Digit Supplement"
        .AddItem "5-Digit Supplement"
        .AddItem "POS Case Code"
        .ListIndex = 0
    End With
    End Sub
 

 

許多人在編寫數據庫應用程序時,都想要加上條形碼功能加強工作效率,尤其是銷售管理,圖書館管理這類流量大的應用軟件,但由于條形碼技術難以掌握、標誰又多以及過去的技術種種原因,使得許多人望而卻步。本文介紹的一套簡單實用的條形碼解決方法,希望能幫助各位完善軟件系統(tǒng)的功能。文章源代碼可以發(fā)信到peedy@shareware.com.cn索取。



相關文章