上一节将PrintDocument、PageSetupDialog、PrintDialog、PrintPreviewDialog的重要属性/方法进行了个罗列。
本节开始正式学习这些组件的使用。
PageSetupDialog组件的使用,本节暂时不讲实际打印
设计时窗体如图(请自行添加PrintDialog组件):

直接上代码分析:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ps As Printing.PrinterSettings
If PrintDialog1.ShowDialog = DialogResult.OK Then
ps = PrintDialog1.PrinterSettings
Else
Exit Sub
End If
TextBox1.Text = "打印机名称:" & ps.PrinterName & ControlChars.CrLf &
"支持双面打印:" & IIf(ps.CanDuplex, "是", "否") & ControlChars.CrLf &
"打印份数:" & ps.Copies & ControlChars.CrLf &
"打印起始页:" & ps.FromPage & ControlChars.CrLf &
"打印终止页:" & ps.ToPage & ControlChars.CrLf &
"支持彩色打印:" & IIf(ps.SupportsColor, "是", "否") & ControlChars.CrLf
End Sub
可以看出来,打印对话框和之前将的对话框差不多,都需要 ShowDialog()
由于之前已经将重要属性方法进行了说明,这里不再讲,不明白的,请看
vb.net 教程 11-1 打印组件 1
按下按钮,出现打印对话框,我的电脑上没有真实打印机,但实际效果差不多:

先不管其中的灰色项目,将份数设置为4份,按下“打印”按钮:

已经将我们需要的打印机名称、打印份数显示出来了。
再将打印对话框和设计时的属性做个对比:

接下来将属性设置为True再看看效果

运行效果:

