首先创建一个打印机类,继承Printdocument.重写OnBeginPrint 方法,和 OnPrintPage方法.
' 使用VB.net *** 作打印机
importsSystem.Drawing.Printing
importsSystem.IO
importsbarcodelib
PublicClassTextfilePrintdocument
inheritsPrintdocument
PrivateprintFontAsFont
PrivatestreamtoprintAsStreamReader
PublicSubNew(ByValstreamReaderAsStreamReader)
MyBase.New()
Me.streamtoprint = streamReader
EndSub
ProtectedOverrIDesSubOnBeginPrint(ByValeAsSystem.Drawing.Printing.PrintEventArgs)
MyBase.OnBeginPrint(e)
printFont= NewFont("Arial",10)
EndSub
ProtectedOverrIDesSubOnPrintPage(ByValeAsSystem.Drawing.Printing.PrintPageEventArgs)
MyBase.OnPrintPage(e)
DimlppAsSingle =e.marginBounds.Height / printFont.GetHeight(e.Graphics)
DimyPosAsSingle =0
DimcountAsSingle =0
DimleftmarginAsSingle =e.marginBounds.left
DimtopmarginAsSingle =e.marginBounds.top
DimlineAsString =streamtoprint.Readline()
While (count < lppAndNotlineIsnothing)
yPos= topmargin + (count * printFont.GetHeight(e.Graphics))
e.Graphics.DrawString(line,printFont,Brushes.Black,leftmargin,yPos,NewStringFormat())
count= count + 1
If (count < lpp)Then
line= streamtoprint.Readline()
EndIf
EndWhile
If (line <> nothing) Then
e.HasMorePages= True
Else
e.HasMorePages= False
EndIf
EndSub
EndClass
-----------------
新建一个页面.添加一个按钮.
按钮的点击事件如下
imports System.IO
imports System.Drawing.Printing
PrivateSubprintbutton_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs) Handlesprintbutton.Click
Try
DimstreamtoprintAsStreamReader= NewStreamReader("aa.txt")
Try
' 使用缺省打印机
DimpdAsTextfilePrintdocument= NewTextfilePrintdocument(streamtoprint)
IfNotstoredPageSettingsIsnothingThen
pd.DefaultPageSettings = storedPageSettings
EndIf
DimdlgAsNewPrintDialog()
dlg.document= pd
DimresultAsDialogResult= dlg.ShowDialog()
If (result = System.windows.Forms.DialogResult.OK) Then
pd.Print()
EndIf
Finally
streamtoprint.Close()
EndTry
CatchexAsException
MessageBox.Show("error:" + ex.Message)
EndTry
EndSub
文件存放目录
DimstreamtoprintAsStreamReader = NewStreamReader("aa.txt")
这个参数是需要打印文件的目录
总结以上是内存溢出为你收集整理的VB.Net *** 作打印机全部内容,希望文章能够帮你解决VB.Net *** 作打印机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)