感谢您使用微软产品。在microsoft.public.dotnet.general 新闻组有一个VB.NET的例子,您可以参考一下:新建一个项目,添加:
一个Windows form
一个ListView
一个button
一个PageSetupDialog
一个PrintPreviewDialog
一个PrintDocument然后添加如下代码:Public Class Form1
Inherits System.Windows.Forms.Form Dim tableFont, titleFont As Font Dim X1, X2, X3 As Integer Dim W1, W2, W3 As Integer Dim Y As Integer Dim itm As Integer
......Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings() If PageSetupDialog1.ShowDialog() Then PrintDocument1.DefaultPageSettings = _
PageSetupDialog1.PageSettings End If tableFont = New Font("Arial", 8) titleFont = New Font("Arial", 12, FontStyle.Bold) X1 = PrintDocument1.DefaultPageSettings.Margins.Left Dim pageWidth As Integer With PrintDocument1.DefaultPageSettings pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right End With X2 = X1 + 100 X3 = X2 + pageWidth * 0.5 W1 = X2 - X1 W2 = X3 - X2 W3 = pageWidth - X3 PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() itm = 0
End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim BookItem As New ListViewItem() BookItem.Text = "0393049515" BookItem.SubItems.Add("The Dream of Reason: " & "A History of Philosophy from the Greeks to the Renaissance") BookItem.SubItems.Add("Anthony Gottlieb") ListView1.Items.Add(BookItem) BookItem = New ListViewItem() BookItem.Text = "0156445085" BookItem.SubItems.Add("In Search of the Miraculous: " & "Fragments of an Unknown Teaching") BookItem.SubItems.Add("P. D. Ouspensky") ListView1.Items.Add(BookItem)
End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Y = PrintDocument1.DefaultPageSettings.Margins.Top + 20 e.Graphics.DrawString("ISBN", titleFont, Brushes.Black, X1, Y) e.Graphics.DrawString("Title", titleFont, Brushes.Black, X2, Y) e.Graphics.DrawString("Author(s)", titleFont, Brushes.Black, X3, Y) Y = Y + 30 While itm < ListView1.Items.Count Dim str As String str = ListView1.Items(itm).Text e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y) str = ListView1.Items(itm).SubItems(1).Text Dim R As New RectangleF(X2, Y, W2, 80) e.Graphics.DrawString(str, tableFont, Brushes.Black, R) Dim lines, cols As Integer e.Graphics.MeasureString(str, tableFont, New SizeF(W2, 50), New StringFormat(), cols, lines) Dim subitm As Integer, Yc As Integer Yc = Y For subitm = 2 To ListView1.Items(itm).SubItems.Count - 1 str = ListView1.Items(itm).SubItems(subitm).Text e.Graphics.DrawString(str, tableFont, _
Brushes.Black, X3, Yc) Yc = Yc + tableFont.Height + 2 Next Y = Y + lines * tableFont.Height + 5 Y = Math.Max(Y, Yc) With PrintDocument1.DefaultPageSettings e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y, .PaperSize.Width - .Margins.Right, Y) If Y > 0.95 * (.PaperSize.Height - .Margins.Bottom) Then e.HasMorePages = True Exit Sub End If End With itm = itm + 1 End While e.HasMorePages = False
End Sub希望能对您有所帮助。======================
- 微软全球技术中心本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
一个Windows form
一个ListView
一个button
一个PageSetupDialog
一个PrintPreviewDialog
一个PrintDocument然后添加如下代码:Public Class Form1
Inherits System.Windows.Forms.Form Dim tableFont, titleFont As Font Dim X1, X2, X3 As Integer Dim W1, W2, W3 As Integer Dim Y As Integer Dim itm As Integer
......Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings() If PageSetupDialog1.ShowDialog() Then PrintDocument1.DefaultPageSettings = _
PageSetupDialog1.PageSettings End If tableFont = New Font("Arial", 8) titleFont = New Font("Arial", 12, FontStyle.Bold) X1 = PrintDocument1.DefaultPageSettings.Margins.Left Dim pageWidth As Integer With PrintDocument1.DefaultPageSettings pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right End With X2 = X1 + 100 X3 = X2 + pageWidth * 0.5 W1 = X2 - X1 W2 = X3 - X2 W3 = pageWidth - X3 PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() itm = 0
End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim BookItem As New ListViewItem() BookItem.Text = "0393049515" BookItem.SubItems.Add("The Dream of Reason: " & "A History of Philosophy from the Greeks to the Renaissance") BookItem.SubItems.Add("Anthony Gottlieb") ListView1.Items.Add(BookItem) BookItem = New ListViewItem() BookItem.Text = "0156445085" BookItem.SubItems.Add("In Search of the Miraculous: " & "Fragments of an Unknown Teaching") BookItem.SubItems.Add("P. D. Ouspensky") ListView1.Items.Add(BookItem)
End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Y = PrintDocument1.DefaultPageSettings.Margins.Top + 20 e.Graphics.DrawString("ISBN", titleFont, Brushes.Black, X1, Y) e.Graphics.DrawString("Title", titleFont, Brushes.Black, X2, Y) e.Graphics.DrawString("Author(s)", titleFont, Brushes.Black, X3, Y) Y = Y + 30 While itm < ListView1.Items.Count Dim str As String str = ListView1.Items(itm).Text e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y) str = ListView1.Items(itm).SubItems(1).Text Dim R As New RectangleF(X2, Y, W2, 80) e.Graphics.DrawString(str, tableFont, Brushes.Black, R) Dim lines, cols As Integer e.Graphics.MeasureString(str, tableFont, New SizeF(W2, 50), New StringFormat(), cols, lines) Dim subitm As Integer, Yc As Integer Yc = Y For subitm = 2 To ListView1.Items(itm).SubItems.Count - 1 str = ListView1.Items(itm).SubItems(subitm).Text e.Graphics.DrawString(str, tableFont, _
Brushes.Black, X3, Yc) Yc = Yc + tableFont.Height + 2 Next Y = Y + lines * tableFont.Height + 5 Y = Math.Max(Y, Yc) With PrintDocument1.DefaultPageSettings e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y, .PaperSize.Width - .Margins.Right, Y) If Y > 0.95 * (.PaperSize.Height - .Margins.Bottom) Then e.HasMorePages = True Exit Sub End If End With itm = itm + 1 End While e.HasMorePages = False
End Sub希望能对您有所帮助。======================
- 微软全球技术中心本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货