VB.Net 操作打印机

前端之家收集整理的这篇文章主要介绍了VB.Net 操作打印机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先创建一个打印机类,继承PrintDocument.重写OnBeginPrint 方法, OnPrintPage方法.@H_404_25@

@H_404_25@

@H_404_25@

' 使用VB.net操作打印机@H_404_25@

@H_404_25@

@H_404_25@

ImportsSystem.Drawing.Printing@H_404_25@

ImportsSystem.IO@H_404_25@

ImportsBarcodeLib@H_404_25@

@H_404_25@

@H_404_25@

PublicClassTextFilePrintDocument@H_404_25@

InheritsPrintDocument@H_404_25@

@H_404_25@

PrivateprintFontAsFont@H_404_25@

PrivatestreamToPrintAsStreamReader@H_404_25@

@H_404_25@

PublicSubNew(ByValstreamReaderAsStreamReader)@H_404_25@

MyBase.New()@H_404_25@

Me.streamToPrint = streamReader@H_404_25@

@H_404_25@

EndSub@H_404_25@

@H_404_25@

ProtectedOverridesSubOnBeginPrint(ByValeAsSystem.Drawing.Printing.PrintEventArgs)@H_404_25@

MyBase.OnBeginPrint(e)@H_404_25@

printFont= NewFont("Arial",10)@H_404_25@

EndSub@H_404_25@

@H_404_25@

ProtectedOverridesSubOnPrintPage(ByValeAsSystem.Drawing.Printing.PrintPageEventArgs)@H_404_25@

MyBase.OnPrintPage(e)@H_404_25@

DimlppAsSingle =e.MarginBounds.Height / printFont.GetHeight(e.Graphics)@H_404_25@

DimyPosAsSingle =0@H_404_25@

DimcountAsSingle =0@H_404_25@

DimleftMarginAsSingle =e.MarginBounds.Left@H_404_25@

DimtopMarginAsSingle =e.MarginBounds.Top@H_404_25@

DimlineAsString =streamToPrint.ReadLine()@H_404_25@

@H_404_25@

While (count < lppAndNotlineIsNothing)@H_404_25@

yPos= topMargin + (count * printFont.GetHeight(e.Graphics))@H_404_25@

e.Graphics.DrawString(line,printFont,Brushes.Black,leftMargin,yPos,NewStringFormat())@H_404_25@

count= count + 1@H_404_25@

@H_404_25@

If (count < lpp)Then@H_404_25@

line= streamToPrint.ReadLine()@H_404_25@

EndIf@H_404_25@

@H_404_25@

EndWhile@H_404_25@

@H_404_25@

@H_404_25@

If (line <> Nothing) Then@H_404_25@

e.HasMorePages= True@H_404_25@

Else@H_404_25@

e.HasMorePages= False@H_404_25@

EndIf@H_404_25@

@H_404_25@

@H_404_25@

@H_404_25@

EndSub@H_404_25@

@H_404_25@

EndClass@H_404_25@

@H_404_25@

-----------------@H_404_25@

@H_404_25@

@H_404_25@

新建一个页面.添加一个按钮.@H_404_25@


@H_404_25@


@H_404_25@


@H_404_25@

按钮的点击事件如下@H_404_25@

Imports System.IO@H_404_25@

Imports System.Drawing.Printing@H_404_25@

@H_404_25@

PrivateSubprintButton_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs) HandlesprintButton.Click@H_404_25@

Try@H_404_25@

DimstreamToPrintAsStreamReader= NewStreamReader("aa.txt")@H_404_25@

Try@H_404_25@

' 使用缺省打印机@H_404_25@

DimpdAsTextFilePrintDocument= NewTextFilePrintDocument(streamToPrint)@H_404_25@

@H_404_25@

@H_404_25@

IfNotstoredPageSettingsIsNothingThen@H_404_25@

pd.DefaultPageSettings = storedPageSettings@H_404_25@

EndIf@H_404_25@

@H_404_25@

DimdlgAsNewPrintDialog()@H_404_25@

dlg.Document= pd@H_404_25@

DimresultAsDialogResult= dlg.ShowDialog()@H_404_25@

@H_404_25@

If (result = System.Windows.Forms.DialogResult.OK) Then@H_404_25@

pd.Print()@H_404_25@

EndIf@H_404_25@

Finally@H_404_25@

streamToPrint.Close()@H_404_25@

EndTry@H_404_25@

CatchexAsException@H_404_25@

MessageBox.Show("error:" + ex.Message)@H_404_25@

EndTry@H_404_25@

EndSub@H_404_25@

@H_404_25@

文件存放目录@H_404_25@


@H_404_25@

DimstreamToPrintAsStreamReader = NewStreamReader("aa.txt")@H_404_25@

这个参数是需要打印文件的目录@H_404_25@

猜你在找的VB相关文章