zoukankan      html  css  js  c++  java
  • 如何打印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
    此示例,不能打印表格线!
  • 相关阅读:
    20200917-2 词频统计
    20200910-2 博客作业
    20200910-1 每周例行报告
    20200910-3命令行和控制台编程
    使用Requests库实现api接口测试(Python)
    Python Lambda函数的几种使用方法
    文本与向量之间的转换
    Oracle连接出现error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
    一图看懂新一代人工智能知识体系大全
    SqlDeveloper连接MySQL出现The connection property ‘zeroDateTimeBehavior’ acceptable values are: ‘CONVERT_TO_NULL’, ‘EXCEPTION’ or ‘ROUND’. The value ‘convertToNull’ is not acceptable 错误
  • 原文地址:https://www.cnblogs.com/zqonline/p/1610721.html
Copyright © 2011-2022 走看看