利用 printdocument控件
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名、雅安服务器托管、营销软件、网站建设、宜宾网站维护、网站推广。
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim stringFont As New Font("Arial", 16)
Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim strFormat As New StringFormat
Dim s As String
s = "print word" '打印的内容
e.Graphics.DrawString(s, stringFont, Brushes.AliceBlue, rectDraw, strFormat)
End Sub
用PrintForm控件,在Visual Basic PowerPacks项目列表中vb2008 SP1以后版本就有了,下面是代码
Imports System.Drawing.Printing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'先设置打印页面的页边距
With Me.PrintForm1
Dim myMargins As New Margins '页边距设置信息是存放在这个Margins类型的对象中的
With myMargins '分别设置上下左右边距,
.Left = 12
.Right = 12
.Top = 12
.Bottom = 12
End With
.PrinterSettings.DefaultPageSettings.Margins = myMargins '把myMargins对象赋给PrintForm1的设置属性
End With
Me.Button1.Visible = False '这个是在打印的时候隐藏打印按钮
Me.PrintForm1.Form = Me '设置要打印的窗体
Me.PrintForm1.Print() '调用打印窗体方法
Me.Button1.Visible = True '再把隐藏的打印按钮显示出来
End Sub
Imports System.Drawing.Printing
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim PDoc As New PrintDocument
AddHandler PDoc.PrintPage, AddressOf Me.PText
PDoc.Print()
Catch ex As Exception
MessageBox.Show("error", ex.ToString)
End Try
End Sub
Private Sub PText(ByVal sender As Object, ByVal e As PrintPageEventArgs)
e.Graphics.DrawString(TextBox1.Text, New Font("Arial", 11, FontStyle.Regular), Brushes.Black, 120, 120)
e.HasMorePages = False
End Sub
End Class