如何打印ListView里的内容

需在用到的对象有PrintDocument,PrintPreviewControl

指定要打印的字体;

 

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

 

 

初始化PrintPreviewControl1
ContractedBlock.gif ExpandedBlockStart.gif Code
  ' 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   Object ByVal  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

 

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

转载于:https://www.cnblogs.com/zqonline/archive/2009/11/25/1610721.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值