利用 printdocument控件
讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、雅安服務(wù)器托管、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、宜賓網(wǎng)站維護(hù)、網(wǎng)站推廣。
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" '打印的內(nèi)容
e.Graphics.DrawString(s, stringFont, Brushes.AliceBlue, rectDraw, strFormat)
End Sub
用PrintForm控件,在Visual Basic PowerPacks項(xiàng)目列表中vb2008 SP1以后版本就有了,下面是代碼
Imports System.Drawing.Printing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'先設(shè)置打印頁(yè)面的頁(yè)邊距
With Me.PrintForm1
Dim myMargins As New Margins '頁(yè)邊距設(shè)置信息是存放在這個(gè)Margins類(lèi)型的對(duì)象中的
With myMargins '分別設(shè)置上下左右邊距,
.Left = 12
.Right = 12
.Top = 12
.Bottom = 12
End With
.PrinterSettings.DefaultPageSettings.Margins = myMargins '把myMargins對(duì)象賦給PrintForm1的設(shè)置屬性
End With
Me.Button1.Visible = False '這個(gè)是在打印的時(shí)候隱藏打印按鈕
Me.PrintForm1.Form = Me '設(shè)置要打印的窗體
Me.PrintForm1.Print() '調(diào)用打印窗體方法
Me.Button1.Visible = True '再把隱藏的打印按鈕顯示出來(lái)
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