📄 form1.vb
字号:
'添加命名空间
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Public Class Form1
Inherits System.Windows.Forms.Form
Private PFont As Font
Private ToPrint As StreamReader
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(280, 72)
Me.Label1.TabIndex = 0
Me.Label1.Text = "打印的文件:"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(56, 96)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "打开文件"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(168, 96)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text = "打印"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 149)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "打印文件"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Print(ByVal sender As Object, ByVal ea As System.Drawing.Printing.PrintPageEventArgs)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ea.MarginBounds.Left
Dim topMargin As Single = ea.MarginBounds.Top
Dim line As String
'算出行数
lpp = ea.MarginBounds.Height / PFont.GetHeight(ea.Graphics)
line = ToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count * PFont.GetHeight(ea.Graphics))
ea.Graphics.DrawString(line, PFont, Brushes.Black, _
leftMargin, yPos, New StringFormat)
count = count + 1
If (count < lpp) Then
line = ToPrint.ReadLine()
End If
End While
'如果还有内容打印则下一页 ea.HasMorePages = True
If (line <> Nothing) Then
ea.HasMorePages = True
Else
'如果已经没有内容了不再打印
ea.HasMorePages = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With Me.OpenFileDialog1
.CheckFileExists = True
.Multiselect = False
.Filter = "文件类型(*.txt)|*.txt"
.Title = "请选择文本文件"
End With
If (Me.OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
And (OpenFileDialog1.FileName.Length > 0) Then
Label1.Text = "打印的文件 :" + OpenFileDialog1.FileName()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
ToPrint = New StreamReader(OpenFileDialog1.FileName)
Try
PFont = New Font("宋体", 12)
AddHandler PrintDocument1.PrintPage, New PrintPageEventHandler(AddressOf Print)
PrintDocument1.Print()
Finally
ToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("打印出错 -- " + ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -