C
Chuck Gantz
I'm using vb2005.
I wrote a simple app to view and print all the fonts on my pc.To see how the
printout will look without wasting lots of paper, I use PrintPreviewDialog.
The first time I run PrintPreviewDialog, it correctly shows five pages of
printout. If I run it a second time, it only shows three pages, with the
original second page printing over the original first page.
If I run it a third time, only one page shows up, with all the fonts being
printed on that page.
Can anyone see what's wrong. Thanks.
p.s., I'm not sure why when I copy my code all my formatting dissapeared.
'First class
Imports System.Drawing.Printing
Public Class FontDocument
Inherits printdocument
Private m_fontFamilies() As FontFamily
Private m_pageNumber As Int32
Private m_offset As Int32
Public Property FontFamilies() As FontFamily()
Get
Return m_fontFamilies
End Get
Set(ByVal value As FontFamily())
m_fontFamilies = value
End Set
End Property
Public Property PageNumber() As Int32
Get
Return m_pageNumber
End Get
Set(ByVal value As Int32)
m_pageNumber = value
End Set
End Property
Public Property Offset() As Int32
Get
Return m_offset
End Get
Set(ByVal value As Int32)
m_offset = value
End Set
End Property
Public Sub FontDocument(ByVal fontFamilies() As FontFamily)
Me.m_fontFamilies = fontFamilies
End Sub
End Class
'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintPreviewToolStripMenuItem.Click
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal ev
As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
If sender Is Nothing OrElse ev Is Nothing Then
Return
End If
Dim fntDoc As FontDocument = CType(sender, FontDocument)
fntDoc.PageNumber += 1
While yPos < ev.MarginBounds.Bottom And fntDoc.Offset <
fntDoc.FontFamilies.Length
fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)
ev.Graphics.DrawString(fntFamily.Name, fnt, Brushes.Black, 150, yPos)
yPos += fnt.Height
If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Return
Else
ev.HasMorePages = False
End If
Catch ex As Exception
Finally
fntDoc.Offset += 1
End Try
End While
ev.HasMorePages = False
End Sub
I wrote a simple app to view and print all the fonts on my pc.To see how the
printout will look without wasting lots of paper, I use PrintPreviewDialog.
The first time I run PrintPreviewDialog, it correctly shows five pages of
printout. If I run it a second time, it only shows three pages, with the
original second page printing over the original first page.
If I run it a third time, only one page shows up, with all the fonts being
printed on that page.
Can anyone see what's wrong. Thanks.
p.s., I'm not sure why when I copy my code all my formatting dissapeared.
'First class
Imports System.Drawing.Printing
Public Class FontDocument
Inherits printdocument
Private m_fontFamilies() As FontFamily
Private m_pageNumber As Int32
Private m_offset As Int32
Public Property FontFamilies() As FontFamily()
Get
Return m_fontFamilies
End Get
Set(ByVal value As FontFamily())
m_fontFamilies = value
End Set
End Property
Public Property PageNumber() As Int32
Get
Return m_pageNumber
End Get
Set(ByVal value As Int32)
m_pageNumber = value
End Set
End Property
Public Property Offset() As Int32
Get
Return m_offset
End Get
Set(ByVal value As Int32)
m_offset = value
End Set
End Property
Public Sub FontDocument(ByVal fontFamilies() As FontFamily)
Me.m_fontFamilies = fontFamilies
End Sub
End Class
'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintPreviewToolStripMenuItem.Click
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal ev
As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
If sender Is Nothing OrElse ev Is Nothing Then
Return
End If
Dim fntDoc As FontDocument = CType(sender, FontDocument)
fntDoc.PageNumber += 1
While yPos < ev.MarginBounds.Bottom And fntDoc.Offset <
fntDoc.FontFamilies.Length
fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)
ev.Graphics.DrawString(fntFamily.Name, fnt, Brushes.Black, 150, yPos)
yPos += fnt.Height
If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Return
Else
ev.HasMorePages = False
End If
Catch ex As Exception
Finally
fntDoc.Offset += 1
End Try
End While
ev.HasMorePages = False
End Sub