先拖過(guò)來(lái)控件PrintDocument1,然后雙擊PrintDocument1,在它的PrintPage事件中加入代碼如下:
我們提供的服務(wù)有:網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、禹王臺(tái)ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的禹王臺(tái)網(wǎng)站制作公司
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
dim a as String
a="abcd"
Dim mypen As Pen = New Pen(Color.Blue, 2)
e.Graphics.DrawString(a, New Font("宋體", 20), New Pen(Color.Black, 1).Brush, 30, 30)
End Sub
調(diào)用下面語(yǔ)句可直接用默認(rèn)打印機(jī)打印出來(lái):
PrintDocument1.Print()
毫無(wú)疑問(wèn),被窗口攔截工具攔截了。瀏覽器沒有設(shè)置,Google 工具條呢,3721呢,MSN工具條呢,這些號(hào)稱可以禁止垃圾信息的垃圾工具都可能導(dǎo)致彈出窗口消失。
Sub Macro1()
'
' Macro1 Macro
' 宏在 10-02-04 由 user 錄制
'
Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub
報(bào)表打印應(yīng)該也能實(shí)現(xiàn),但是我覺得你這個(gè)用文本打印更簡(jiǎn)單,將數(shù)據(jù)輸出到txt文件,結(jié)果用RichTextBox顯示,但是需要簡(jiǎn)單的排版,調(diào)用打印機(jī)打印RichTextBox即可的
排版用tab()、space()、vbcrlf或PrintLine(1)換行,代碼類似如下樣式
PrintLine(1, TAB(60), "準(zhǔn)考證" )
PrintLine(1)
PrintLine(1, "姓名:" xingming Space(3) "準(zhǔn)考證號(hào):" cel(1) Space(3) cel(2) Space(3) cel(3))
但是TAB()排版比較規(guī)整
打印代碼類似如下:
PrintDialog1.Document = PrintDocument1
PrintDocument1.DocumentName = "準(zhǔn)考證"
PrintDialog1.AllowSomePages = False
PrintDialog1.ShowHelp = False
PrintDialog1.ShowNetwork = False
PrintDialog1.AllowSelection = False
PrintDialog1.AllowPrintToFile = False
MySReader = New StringReader(RichTextBox1.Text)
stringToPrint = MySReader.ReadToEnd()
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.PageSettings.Margins.Bottom = 50
PageSetupDialog1.PageSettings.Margins.Top = 50
PageSetupDialog1.PageSettings.Margins.Left = 50
PageSetupDialog1.PageSettings.Margins.Right = 50
If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings '頁(yè)面設(shè)置
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
If PrintDialog1.PrinterSettings.IsValid = True Then
PrintDocument1.Print()
MsgBox("打印完成!" vbCrLf "Print completed!", , "Print hint(打印提示)")
Else
MsgBox("打印失??!打印機(jī)不可用。" vbCrLf "Print failed! The printer is not valid.", , "Print hint(打印提示)")
End If
Else
Exit Sub
End If
End If