当前页面位置: » 丰搜网 » 文档中心 » 详细内容
实战visual basic条形码编程
一、条形码的读取
用过键盘口式的扫条码工具的朋友就知道,它就如同在鍵盘上按下数字鍵一样,基本不需任何编程和处理。但如果你使用的是其它接口的话,可能你就要为该设备编写通讯代码了。以下有一段简单的25针串口的条码读取器通讯代码。
option explicit dim sdata as string private sub form_load() with mscomm1 .commport = 3 '设为com3,试运行的系统而定,你可提供一个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 '如果未结束就继续努力 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 '区域表达变量 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. pos.net" .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 |