Visual Basic 限定的サンプル |
必要なコントロール |
Button1 |
PrintDocument1 |
Private
Sub Button1_Click(ByVal
sender As System.Object,
ByVal e As System.EventArgs)
Handles Button1.Click Dim Previewer As New PrintPreviewDialog Previewer.Document = PrintDocument1 Previewer.ShowDialog() 'プレビュー実行 End Sub |
Private Sub
PrintDocument1_PrintPage(ByVal sender
As System.Object, ByVal
e As System.Drawing.Printing.PrintPageEventArgs)
Handles PrintDocument1.PrintPage Dim f As Font Dim i As Integer Dim Y As Single '次の印字縦位置 Dim X As Single '次の印字横位置 Dim MaxHeight As Integer '現在の行で一番背が高い文字の高さ Dim Lines As Integer Y = e.MarginBounds.Top For Lines = 0 To 4 X = e.MarginBounds.Left MaxHeight = 0 '段々と文字を大きくして1行を印刷する For i = 0 To 9 f = New Font("MS明朝", 10 + (i * 2), FontStyle.Bold) MaxHeight = Math.Max(MaxHeight, f.GetHeight(e.Graphics)) e.Graphics.DrawString("あ", f, Brushes.Red, X, Y) X += e.Graphics.MeasureString("あ", f).Width Next '同じ大きさの文字で一行を印刷する Y += MaxHeight f = New Font("MS明朝", 12, FontStyle.Bold) e.Graphics.DrawString("この行はすべて同じ大きさで印字されます。", f, Brushes.Red, e.MarginBounds.Left, Y) Y += f.GetHeight(e.Graphics) Next End Sub |