Imports System.Drawing.Printing
Public Class 对话框示例
Private strPrintRecord As String
Private WithEvents DialogsPrintDocument As PrintDocument
Private strfileName As String
'打开文件的对话框
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
With OpenFileDialog1
.Filter = "Text Document (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 1 '确定在File filter组合框中显示第几个过滤器
.Title = "demo Open file Dialogs"
End With
'showdialog返回一个DialogResult值,只有两个结果:OK,Cancel
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
'保存文件路径和文件名
strfileName = OpenFileDialog1.FileName
'读取文本文件内容
'可以用插入代码段功能:快捷键ctrl+k,ctrl+x,
'基本元素->文件系统->从文本中读取文件
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(strfileName)
'在文本框显示读出来的内容
txtFile.Text = fileContents
Catch ex As Exception '处理打开文件时可能发生的错误。
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
'保存文件的对话框
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
With SaveFileDialog1
.DefaultExt = "txt"
.FileName = strfileName
.Filter = "Text document (*.txt)|*.txt|all files (*.*)|*.*"
.FilterIndex = 1
.OverwritePrompt = True
.Title = "Demo Save File Dialog"
End With
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
'可以用插入代码段功能:快捷键ctrl+k,ctrl+x,
'基本元素->文件系统->创建文件
strfileName = SaveFileDialog1.FileName
My.Computer.FileSystem.WriteAllText(strfileName, txtFile.Text, True)
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
'字体对话框
Private Sub btnFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFont.Click
FontDialog1.ShowDialog()
txtFile.Font = FontDialog1.Font
End Sub
'颜色对话框
Private Sub btnColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
ColorDialog1.ShowDialog()
txtFile.ForeColor = ColorDialog1.Color
End Sub
Private Sub DialogsPrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DialogsPrintDocument.PrintPage
Dim intCharactersToPrint As Integer
Dim intLinesPerPage As Integer
'包含在单个页面上打印的所有数据
Dim strPrintData As String
'封装了用于格式化要打花不了的数据的文本布局信息,用于按词界修整数据,这样文本不会超出打印区域
Dim objStringFormat As New StringFormat
Dim objPrintFont As New Font("Arial", 10) '设置用于打印文本的字体
Dim objPageBoundaries As Rectangle '定义页面的顶坐标和左坐标,以及宽度和高度
Dim objPrintArea As SizeF '包含面面打印区域的高度和宽度,这是可打印的实际区域,而非页面的实际尺寸
'获取打印边界
objPageBoundaries = New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
'获取打印区域
objPrintArea = New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - objPrintFont.GetHeight(e.Graphics))
'获取单词断点
objStringFormat.Trimming = StringTrimming.Word
'获取字符数
e.Graphics.MeasureString(strPrintRecord, objPrintFont, objPrintArea, objStringFormat, intCharactersToPRint, intLinesPerPage)
'获取打印数据
strPrintData = strPrintRecord.Substring(0, intCharactersToPRint)
'打印页
e.Graphics.DrawString(strPrintData, objPrintFont, Brushes.Black, objPageBoundaries, objStringFormat)
If intCharactersToPRint < strPrintRecord.Length Then
strPrintRecord = strPrintRecord.Remove(0, intCharactersToPRint)
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
DialogsPrintDocument = New PrintDocument
With PrintDialog1
.AllowCurrentPage = False
.AllowPrintToFile = False
.AllowSelection = False
.AllowSomePages = False
.Document = DialogsPrintDocument
.PrinterSettings.DefaultPageSettings.Margins.Top = 25
.PrinterSettings.DefaultPageSettings.Margins.Bottom = 25
.PrinterSettings.DefaultPageSettings.Margins.Left = 25
.PrinterSettings.DefaultPageSettings.Margins.Right = 25
End With
If PrintDialog1.ShowDialog = DialogResult.OK Then
DialogsPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings
End If
strPrintRecord = txtFile.Text
DialogsPrintDocument.Print()
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
With FolderBrowserDialog1
.Description = "选择一个文件夹"
.RootFolder = Environment.SpecialFolder.MyComputer
.ShowNewFolderButton = False
End With
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFile.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
End Class