前一阵子给 安徽省棉麻交易市场 作了一个咚咚
(类似于中国移动话费单打印)
其中一个模块的功能就是实现单据打印
单据是专用纸,不是白纸
模块程序简单,主要是思路要对,要不然就复杂了
开始走了弯路,老是想怎么用报表
比如datareport,ms的MSFlexGrid,还有更为复杂的第三方水晶报表
事实上用一个极为简单的Printer.CurrentX和printer.CurrentY就可以了
要做的准备工作就是量好单据每个要打印项的坐标
再乘以56.7,转化成“缇”
如果是水平打印,则要先将打印机的走纸设为横向(控制面板里。。)
还有个细节是换行问题
在VB中,text文本框的输入是可以自动换行的,但打印则不会自动换行
为此,我编写了一个函数实现每个文本框每行最多打印6个字符:
Sub PrintSingleLine(s As String, X As Long, n As Integer)
Dim nLines As Integer
nLines = Len(s) / n
If Len(s) > n * nLines Then nLines = nLines + 1
If Len(s) = 0 Then nLines = 1 ' 空 行
For i = 0 To nLines - 1
Printer.CurrentX = X
Printer.Print Mid(s, i * n + 1, n)
Next
End Sub
再实行以下调用:
Dim s As String, sLine As String
Printer.CurrentY = 72 * 56.7 '72是我例子的坐标
s = txtProductInfo(2).Text '我例子的文本框
pos = InStr(s, vbCrLf) 'vbCrlf索回车的意思。。
Do While pos > 0
sLine = Left(s, pos - 1)
s = Mid(s, pos + 2)
PrintSingleLine Trim(sLine), 49 * 56.7, 6
pos = InStr(s, vbCrLf)
Loop
PrintSingleLine Trim(s), 49 * 56.7, 6 '每行6个字符
---------作者卡卡---------