Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal _
hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As _
Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal _
hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, _
ByVal dwRop As System.Int32) As Long
Dim memoryImage As Bitmap
Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, _
Me.ClientRectangle.Height, dc1, 0, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
PrintDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub
Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles PrintButton.Click
CaptureScreen()
PrintDocument1.Print()
End Sub
'在用户按下页面设置按钮时激发的事件
Private Sub pageSetupButton_Click(sender As object, e As System.EventArgs)
Try
Dim psDlg As New PageSetupDialog
If (storedPageSettings Is Nothing) Then
storedPageSettings = new PageSettings()
End If
psDlg.PageSettings = storedPageSettings
psDlg.ShowDialog
Catch ex As Exception
MessageBox.Show("发生错误 - " + ex.Message)
End Try
End Sub
'在用户按下打印按钮时激发的事件
Private Sub printButton_Click(sender As object, e As System.EventArgs)
Try
Dim streamToPrint As StreamReader = new StreamReader ("PrintMe.Txt")
Try
'假定为默认打印机
Dim pd As TextFilePrintDocument = new TextFilePrintDocument(streamToPrint)
If Not (storedPageSettings Is Nothing) Then
pd.DefaultPageSettings = storedPageSettings
End If
Dim dlg As New PrintDialog()
dlg.Document = pd
Dim result As DialogResult = dlg.ShowDialog()
If (result = System.Windows.Forms.DialogResult.OK) Then
pd.Print()
End If
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("打印文件时发生错误 - " + ex.Message)
End Try
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region " Windows 窗体设计器生成的代码 "
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.Container
Private WithEvents printButton As System.Windows.Forms.Button
Private WithEvents pageSetupButton As System.Windows.Forms.Button
Private WithEvents PrintForm As System.Windows.Forms.Form
'注意:下列过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改它。
'不要使用代码编辑器修改它。
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.printButton = New System.Windows.Forms.Button()
Me.pageSetupButton = New System.Windows.Forms.Button()
Me.Text = "打印示例 4"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 381)
'在用户按下打印按钮时激发的事件
Private Sub printButton_Click(sender As object, e As System.EventArgs)
Try
streamToPrint = new StreamReader ("PrintMe.Txt")
Try
printFont = new Font("Arial", 10)
Dim pd as PrintDocument = new PrintDocument() '假定为默认打印机
AddHandler pd.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.pd_PrintPage)
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("打印文件时发生错误 - " + ex.Message)
End Try
End Sub
'每个要打印的页所激发的事件
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
'如果有多行,则另外打印一页
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region " Windows 窗体设计器生成的代码 "
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.Container
Private WithEvents printButton As System.Windows.Forms.Button
Private WithEvents PrintingExample1 As System.Windows.Forms.Form
' 注意:下列代码是 Windows 窗体设计器所必需的
' 不要修改它
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.printButton = New System.Windows.Forms.Button()
Me.Text = "打印示例 1"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 381)
printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat
printButton.Size = New System.Drawing.Size(136, 40)
printButton.TabIndex = 0
printButton.Location = New System.Drawing.Point(32, 112)
printButton.Text = "打印文件"
Me.Controls.Add(printButton)
End Sub
#End Region
'应用程序的主要入口点
<STAThread()> Shared Sub Main()
System.Windows.Forms.Application.Run(New PrintingExample1())
End Sub