C# ListView打印类

根据给定文件的信息,本文将深入探讨如何在C#中创建一个可以打印的ListView控件。这涉及到ListView的自定义实现、打印文档设置以及页面布局调整等知识点。 ### C# ListView打印类概述 #### 1. 重写ListView控件的目的 在C#中,ListView是一种常用的控件,用于展示列表数据。然而,默认情况下,ListView并不支持直接打印其内容。为了实现这一功能,我们需要通过继承`System.Windows.Forms.ListView`类并进行相应的重写和扩展来构建一个支持打印的ListView控件。 #### 2. 打印相关的字段和属性 在实现打印功能时,需要设置多个字段和属性来控制打印的行为。这些字段包括: - `PrintDocument`: 用于定义打印文档。 - `PageSetupDialog`: 用于页面设置对话框。 - `PrintPreviewDialog`: 用于预览打印效果。 - `PrintDialog`: 用于打印对话框。 - `int m_nPageNumber`: 页码。 - `int m_nStartRow`: 开始行。 - `int m_nStartCol`: 开始列。 - `bool m_bPrintSel`: 是否只打印选中的项。 - `bool m_bFitToPage`: 内容是否适应页面宽度。 - `float m_fListWidth`: 列表宽度。 - `float[] m_arColsWidth`: 列宽数组。 - `float m_fDpi`: 分辨率。 - `string m_strTitle`: 打印标题。 - `string m_strCount`: 统计信息。 这些字段为打印提供了基础配置,使得用户可以根据需要调整打印的外观和布局。 #### 3. FitToPage属性 FitToPage属性决定了ListView的内容是否应该缩放以适应单个页面的宽度。默认情况下,此属性为`false`,这意味着如果列表宽度超过页面宽度,则列表会被拆分成多页打印。如果将其设置为`true`,则列表会自动缩放以适应页面宽度。 #### 4. Title属性 Title属性用于获取或设置打印页面的标题。该标题通常作为页面头部的一部分出现,可以用来标识打印内容的身份。 #### 5. strCount属性 strCount属性用于获取或设置与ListView中项目数量相关的统计信息。这对于打印出包含项目总数或其他相关信息的页脚非常有用。 ### 实现打印功能的关键步骤 1. **初始化打印文档**:创建一个`PrintDocument`实例,并为其添加打印事件处理程序,例如`PrintDocument.PrintPage`,以便在打印时执行所需的操作。 2. **设置页面布局**:利用`PageSetupDialog`和`PrintDialog`等控件来设置打印的页面大小、边距和其他布局选项。 3. **实现打印预览**:通过`PrintPreviewDialog`控件实现打印前的预览功能,让用户能够检查打印结果是否符合预期。 4. **控制打印行为**:通过`m_bPrintSel`和`m_bFitToPage`等属性来控制打印哪些内容以及如何布局这些内容。 5. **绘制打印内容**:在`PrintDocument.PrintPage`事件中,使用`Graphics`对象绘制ListView的内容到打印页面上。 ### 示例代码片段 下面是一个简化的示例代码,展示了如何创建一个可打印的ListView控件: ```csharp public class PrintableListView : System.Windows.Forms.ListView { private PrintDocument m_printDoc = new PrintDocument(); private PageSetupDialog m_setupDlg = new PageSetupDialog(); private PrintPreviewDialog m_previewDlg = new PrintPreviewDialog(); private PrintDialog m_printDlg = new PrintDialog(); public bool FitToPage { get { return m_bFitToPage; } set { m_bFitToPage = value; } } public string Title { get { return m_strTitle; } set { m_strTitle = value; } } public string strCount { get { return m_strCount; } set { m_strCount = value; } } // 构造函数和其他方法... } ``` ### 总结 通过上述介绍,我们了解到如何在C#中实现一个支持打印的ListView控件。这不仅涉及到了继承和重写ListView类的基础知识,还包括了对打印文档、页面布局以及其他相关属性的设置。这样的实现可以极大地提高应用程序的功能性和用户体验。





























private PrintableListView listView1;//listview改成PrintableListView
this.listView1 = new PrintableListView();
listView1.PrintPreview();
//======================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
namespace 金蔷薇美发会员管理系统
{
/// <summary>
/// Summary description for PrintableListView.
/// </summary>
public class PrintableListView : System.Windows.Forms.ListView
{
// Print fields
private PrintDocument m_printDoc = new PrintDocument();
private PageSetupDialog m_setupDlg = new PageSetupDialog();
private PrintPreviewDialog m_previewDlg = new PrintPreviewDialog();
private PrintDialog m_printDlg = new PrintDialog();
private int m_nPageNumber=1;
private int m_nStartRow=0;
private bool m_bPrintSel=false;
private bool m_bFitToPage=false;
private float m_fListWidth=0.0f;
private float[] m_arColsWidth;
private float m_fDpi=96.0f;
private string m_strTitle="设置Title属性打印标题";
private string m_strCount="";
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#region 属性
/// <summary>
/// Gets or sets whether to fit the list width on a single page
/// </summary>
/// <value>
/// <c>True</c> if you want to scale the list width so it will fit on a single page.
/// </value>
/// <remarks>
/// If you choose false (the default value), and the list width exceeds the page width, the list
/// will be broken in multiple page.
/// </remarks>
剩余14页未读,继续阅读

- 粉丝: 218
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 论述GIS技术在交通工程中的应用.doc
- docopt.go-Go资源
- ATC单片机的SD卡的读写设计.doc
- G技术的浮动车交通信息采集系统研究.doc
- “计算机科学与技术”专业自评报告.doc
- AntFlow.net-C#资源
- 基于PLC的电梯控制系统方案设计书67842.doc
- 基于光电传感器的转速测量系统方案设计书单片机光电转速传感器转速测量数据处理.doc
- 齐鲁软件设计实施方案大赛题目.doc
- zibbs开源php轻论坛,Bootstrap论坛-PHP资源
- 三网络技术第章练习题.doc
- 基于智慧城市的物联网新技术的研究.docx
- Photoshop通道蒙版实例(21):使用通道锐化图像.doc
- 人工智能需要翻越三道坎.docx
- pdfh5-JavaScript资源
- 供应链金融与互联网金融.ppt



- 1
- 2
前往页