二、条形码的生成
看完以上关于条码读取的代码是否觉得很容易呢?对,在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
'区域表达变量
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
SysDLG32.bas模块代表啥?谁可以上传其内容一份吗?谢谢!