如何打印ListView里的内容

需在用到的对象有PrintDocument,PrintPreviewControl

指定要打印的字体;

 

    Dim printFont As Font = New Font("宋体"10)

 

 

初始化PrintPreviewControl1

  ' Construct the PrintPreviewControl.
        Me.PrintPreviewControl1 = New Windows.Forms.PrintPreviewControl


        
' Set location, name, and dock style for PrintPreviewControl1.
        Me.PrintPreviewControl1.Location = New Point(8880)
        
Me.PrintPreviewControl1.Name = "PrintPreviewControl1"
        
Me.PrintPreviewControl1.Dock = DockStyle.Fill

        
' Set the Document property to the PrintDocument 
        ' for which the PrintPage event has been handled.
        Me.PrintPreviewControl1.Document = docToPrint

        
' Set the zoom to 25 percent.
        Me.PrintPreviewControl1.Zoom = 1

        
' Set the document name. This will show be displayed when 
        ' the document is loading into the control.

        
' Set the UseAntiAlias property to true so fonts are smoothed
        ' by the operating system.
        Me.PrintPreviewControl1.UseAntiAlias = True

        
Me.PrintPreviewControl1.Rows = 5 '纵向显示五页,用于调试

        
' Add the control to the form.
        Me.Controls.Add(Me.PrintPreviewControl1)
PrintDocument的PringPage事件

    
Private Sub docToPrint_PrintPage(ByVal sender As ObjectByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docToPrint.PrintPage
        
Dim linesPerPage As Single = 0
        
Dim yPos As Single = 0
        
Dim count As Integer = 0
        
Dim leftMargin As Single = e.MarginBounds.Left '左边距
        Dim topMargin As Single = e.MarginBounds.Top    '右边距
        Dim line As String = Nothing

        
' Calculate the number of lines per page.
        linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)


        
Static starRow As Integer
        
Dim row As Integer


        
For row = starRow To Me.lv.Items.Count - 1

            
If count > linesPerPage Then
                starRow 
= row
                
Exit For
            
End If

            
Dim item = Me.lv.Items(row)
            yPos 
= topMargin + count * printFont.GetHeight(e.Graphics)

            
Dim x As Single = leftMargin
            
For m As Integer = 0 To item.SubItems.Count - 1
                x 
+= Me.lv.Columns(m).Width
                line 
= item.SubItems(m).Text
                e.Graphics.DrawString(line, printFont, Brushes.Black, x, yPos, 
New StringFormat())
            
Next

            count 
+= 1
        
Next

        
If row = Me.lv.Items.Count Then
            e.HasMorePages 
= False
        
Else
            e.HasMorePages 
= True
        
End If


    
End Sub

 

此示例,不能打印表格线!

posted on 2009-11-25 17:29  zqonline  阅读(1340)  评论(0编辑  收藏  举报

导航