c# listview数据预览(转载的放在这里备用)
public class PrintListView : ListView
{
/// <summary>
/// 指示是否进行打印预览,默认值为 true
/// </summary>
private bool m_bIsPreview; /// <summary>
/// 指示是否总是打印标题,默认值为 true
/// </summary>
private bool m_bIsAlwaysPrintHeader; /// <summary>
/// 需要打印的页首字符串
/// </summary>
private string m_sPrintHeaderString; /// <summary>
/// 标题的字体
/// </summary>
private Font m_oHeaderFont; /// <summary>
/// 正文字体
/// </summary>
private Font m_oBodyFont; /// <summary>
/// 页尾字体
/// </summary>
private Font m_oTailFont; /// <summary>
/// 正文部分的 ColumnHeader 字体,该字体为正文字体的加粗形式
/// </summary>
private Font m_oColumnHeaderFont; /// <summary>
/// 打印文档
/// </summary>
private PrintDocument m_oPrintDoc; /// <summary>
/// 行间距
/// </summary>
private int m_nLineSpace; private int m_nPrintWidth; // 打印区域宽度 private int m_nPrintHeight; // 打印区域长度 private int m_nPageCount; // 打印页数 private int m_nCurPrintPage; // 当前正在打印的页码 private int m_nTotalPage; // 共有的页数 private int m_nFromPage; // 用户选择的打印起始页 private int m_nCurPrintItem; // 当前正在打印的元素 private Rectangle m_oHeaderRect; // 打印页首的区域 private Rectangle m_oBodyRect; // 打印正文的区域 private Rectangle m_oTailRect; // 打印页尾的区域 private Brush defaultBrush; private Pen defaultPen; /// <summary>
/// 设置或获取是否进行打印预览
/// </summary>
public bool IsPreview
{
get { return m_bIsPreview; }
set { m_bIsPreview = value; }
} /// <summary>
/// 设置或获取是否总是打印标题
/// </summary>
public bool IsAlwaysPrintHeader
{
get { return m_bIsAlwaysPrintHeader; }
set { m_bIsAlwaysPrintHeader = value; }
} /// <summary>
/// 设置或获取打印的页首字符串
/// </summary>
public string PrintHeaderString
{
get { return m_sPrintHeaderString; }
set { if (value != null) m_sPrintHeaderString = value; }
} /// <summary>
/// 设置或获取页首字体
/// </summary>
public Font HeaderFont
{
set { m_oHeaderFont = value; }
get { return m_oHeaderFont; }
} /// <summary>
/// 设置或获取正文字体
/// 如果对正文字体的 Bold 属性设置为 true ,则发生 Exception 类型的异常
/// </summary>
public Font BodyFont
{
set
{
if (value == null)
return;
if (value.Bold == true)
{
//throw new Exception ("正文字体不能进行 [加粗] 设置.");
MessageBox.Show("正文字体不能进行 [加粗] 设置.", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
m_oBodyFont = value;
}
}
get { return m_oBodyFont; }
} /// <summary>
/// 设置或获取页尾字体
/// </summary>
public Font TailFont
{
set { m_oTailFont = value; }
get { return m_oTailFont; }
} /// <summary>
/// 设置或获取行间距
/// </summary>
public int LineSpace
{
get { return m_nLineSpace; }
set
{
if (value < )
{
m_nLineSpace = ;
}
m_nLineSpace = value;
}
} /// <summary>
/// 构造函数,设置打印信息的一些默认值
/// </summary>
public PrintListView()
{
m_bIsPreview = true;
m_bIsAlwaysPrintHeader = true;
IsAlwaysPrintHeader = true;
m_sPrintHeaderString = "";
m_oHeaderFont = null;
m_oTailFont = null;
m_oBodyFont = null;
m_oColumnHeaderFont = null;
m_oPrintDoc = null;
m_nPrintWidth = ;
m_nPrintHeight = ;
m_nPageCount = ;
m_nCurPrintPage = ;
m_nFromPage = ;
m_nLineSpace = ;
m_nCurPrintItem = ;
defaultBrush = Brushes.Black;
defaultPen = new Pen(defaultBrush, );
} /// <summary>
/// 初始化打印文档的属性
/// </summary>
/// <returns></returns>
private void InitPrintDocument()
{
m_oPrintDoc = new PrintDocument();
m_oPrintDoc.DocumentName = m_sPrintHeaderString;
if (m_oPrintDoc.PrinterSettings.PrinterName == "<no default printer>")
{
//throw new Exception ("未找到默认打印机.");
MessageBox.Show("未找到默认打印机.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
m_oPrintDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_oPrintDoc.BeginPrint += new PrintEventHandler(BeginPrint);
m_oPrintDoc.EndPrint += new PrintEventHandler(EndPrint);
} /* 设置打印字体 */
if (m_oHeaderFont == null)
{
m_oHeaderFont = new Font("宋体", , FontStyle.Bold, GraphicsUnit.World);
} if (m_oBodyFont == null)
{
m_oBodyFont = new Font("宋体", , FontStyle.Regular, GraphicsUnit.World);
} m_oColumnHeaderFont = new Font(m_oBodyFont, FontStyle.Bold); if (m_oTailFont == null)
{
m_oTailFont = new Font("宋体", , FontStyle.Regular, GraphicsUnit.World);
}
} /// <summary>
/// 初始化打印纸张设置
/// </summary>
private bool InitPrintPage(out Margins margins)
{
margins = null;
/* 获取当前 listview 的分辨率 */
Graphics g = this.CreateGraphics();
float x = g.DpiX;
g.Dispose();
/* 显示纸张设置对话框 */
PageSetupDialog ps = new PageSetupDialog();
ps.Document = m_oPrintDoc; if (ps.ShowDialog() == DialogResult.Cancel)
{
return false;
}
else
{ // 根据用户设置的纸张信息计算打印区域,计算结果的单位为 1/100 Inch
m_nPrintWidth = ps.PageSettings.Bounds.Width - ps.PageSettings.Margins.Left - ps.PageSettings.Margins.Right;
m_nPrintHeight = ps.PageSettings.Bounds.Height - ps.PageSettings.Margins.Top - ps.PageSettings.Margins.Bottom;
margins = ps.PageSettings.Margins;
} if (m_nPrintWidth <= || m_nPrintHeight <= )
{
//throw new Exception ("纸张设置错误.");
MessageBox.Show("纸张设置错误.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
} /* 将当前 listview 的各column的长度和转换为英寸,判断打印范围是否越界 */
int listViewWidth = ;
for (int i = ; i < this.Columns.Count; i++)
{
listViewWidth += this.Columns[i].Width;
}
if (Convert.ToInt32(listViewWidth / x * ) > m_nPrintWidth)
{
//throw new Exception ("打印内容超出纸张范围 !/r/n请尝试调整纸张或纸张边距;/r/n或缩小表格各列的宽度。");
MessageBox.Show("打印内容超出纸张范围 !/r/n请尝试调整纸张或纸张边距;/r/n或缩小表格各列的宽度。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
m_oPrintDoc.DefaultPageSettings = ps.PageSettings;
return true;
} /// <summary>
/// 初始化打印页码设置
/// </summary>
private bool InitPrintPageNumber(Margins margins)
{
/* 计算页数 */
int headerFontHeight = m_oHeaderFont.Height;
int columnHeaderFontHeight = m_oColumnHeaderFont.Height;
int bodyFontHeight = m_oBodyFont.Height;
int tailFontHeight = m_oTailFont.Height;
// 页首区域打印 页首字符串 以及一条横线
m_oHeaderRect = new Rectangle(margins.Left, margins.Top, m_nPrintWidth, headerFontHeight + m_nLineSpace + );
int tailHeight = tailFontHeight + m_nLineSpace + ;
// 页尾区域打印 一条横线,页码和打印时间(在同一行)
m_oTailRect = new Rectangle(margins.Left, margins.Top + m_nPrintHeight - tailHeight, m_nPrintWidth, tailHeight);
//正文区域为去掉页首和页尾区域的部分
m_oBodyRect = new Rectangle(margins.Left, m_oHeaderRect.Bottom + , m_nPrintWidth, m_oTailRect.Top - m_oHeaderRect.Bottom - );
/* 计算第一页能打印的元素个数 */
int printItemPerPage = ;
int firstPageItem = Convert.ToInt32((m_oBodyRect.Height - columnHeaderFontHeight - m_nLineSpace) / (bodyFontHeight + m_nLineSpace));
if (firstPageItem >= this.Items.Count)
{ // 需打印的元素只有一页
m_nPageCount = ;
}
else
{ // 需要打印的元素有多页
printItemPerPage = firstPageItem;
int leftItems = this.Items.Count - firstPageItem;
if (m_bIsAlwaysPrintHeader == false)
{
printItemPerPage = (m_oBodyRect.Height + m_oHeaderRect.Height + - columnHeaderFontHeight - m_nLineSpace) / (bodyFontHeight + m_nLineSpace);
}
if (leftItems % printItemPerPage == )
{
m_nPageCount = leftItems / printItemPerPage + ;
}
else
{
m_nPageCount = leftItems / printItemPerPage + ;
}
}
m_nTotalPage = m_nPageCount;
/* 显示打印对话框 */
PrintDialog pd = new PrintDialog();
pd.Document = m_oPrintDoc;
pd.PrinterSettings.MinimumPage = ;
pd.PrinterSettings.MaximumPage = m_nPageCount;
pd.PrinterSettings.FromPage = ;
pd.PrinterSettings.ToPage = m_nPageCount;
pd.AllowPrintToFile = false; // 不设置打印到文件
if (m_nPageCount > )
{
pd.AllowSelection = true;
}
else
{
pd.AllowSelection = false;
}
pd.AllowSomePages = true;
if (pd.ShowDialog() == DialogResult.OK)
{
m_nPageCount = pd.PrinterSettings.ToPage - pd.PrinterSettings.FromPage + ;
if (pd.PrinterSettings.FromPage > )
{
m_nCurPrintItem = firstPageItem + (pd.PrinterSettings.FromPage - ) * printItemPerPage;
}
else
{
m_nCurPrintItem = ;
}
m_nFromPage = pd.PrinterSettings.FromPage;
m_oPrintDoc.DocumentName = m_nPageCount.ToString();
m_oPrintDoc.PrinterSettings = pd.PrinterSettings;
return true;
}
else
{
return false;
}
} /// <summary>
/// 启动 ListView 的打印工作
/// </summary>
public virtual void DoPrint()
{
if (this.Items.Count <= )
{
//throw new Exception ("没有需要打印的元素.");
MessageBox.Show("没有需要打印的元素.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} InitPrintDocument();
Margins margins = null;
if (InitPrintPage(out margins) == false)
return; if (InitPrintPageNumber(margins) == false)
return; if (m_bIsPreview == false)
{
m_oPrintDoc.Print();
}
else
{
PrintPreviewDialog pd = new PrintPreviewDialog();
pd.Document = m_oPrintDoc;
pd.PrintPreviewControl.Zoom = 1.0;
pd.WindowState = FormWindowState.Maximized;
pd.ShowInTaskbar = true;
pd.ShowDialog();
}
} /// <summary>
/// 打印页首
/// </summary>
/// <param name="g"></param>
protected virtual void PrintPageHeader(Graphics g)
{
RectangleF textRect = new RectangleF(m_oHeaderRect.X, m_oHeaderRect.Y, m_oHeaderRect.Width, m_oHeaderRect.Height - );
CenterText(g, m_sPrintHeaderString, m_oHeaderFont, defaultBrush, textRect);
g.DrawLine(defaultPen, m_oHeaderRect.X, m_oHeaderRect.Bottom - , m_oHeaderRect.Right, m_oHeaderRect.Bottom - );
} /// <summary>
/// 打印正文
/// </summary>
/// <param name="g"></param>
protected virtual void PrintPageBody(Graphics g)
{
Rectangle textRect = m_oBodyRect;
if (m_bIsAlwaysPrintHeader == false && m_nCurPrintPage != )
{
textRect = new Rectangle(m_oHeaderRect.X, m_oHeaderRect.Y, m_oHeaderRect.Width, m_oHeaderRect.Height + + m_oBodyRect.Height);
} /* 打印标题,也就是 columnHeader */
int columnHeaderFontHeight = m_oColumnHeaderFont.Height;
int bodyFontHeight = m_oBodyFont.Height;
Rectangle columnHeaderRect = new Rectangle(textRect.X, textRect.Y, textRect.Width, columnHeaderFontHeight + m_nLineSpace);
DrawColumnHeader(g, m_oColumnHeaderFont, defaultBrush, columnHeaderRect);
/* 打印元素 */
int itemHeight = bodyFontHeight + m_nLineSpace;
Rectangle itemRect;
int yPos = columnHeaderRect.Bottom;
int printItemPerPage = (textRect.Height - columnHeaderRect.Height) / (bodyFontHeight + m_nLineSpace);
for (int i = ; (i < printItemPerPage) && (m_nCurPrintItem < this.Items.Count); i++)
{
itemRect = new Rectangle(textRect.X, yPos, textRect.Width, itemHeight);
DrawItem(g, m_oBodyFont, defaultBrush, itemRect);
m_nCurPrintItem++;
yPos += itemHeight;
}
} /// <summary>
/// 打印页尾
/// </summary>
/// <param name="g"></param>
protected virtual void PrintPageTail(Graphics g)
{
g.DrawLine(defaultPen, m_oTailRect.X, m_oTailRect.Top + , m_oTailRect.Right, m_oTailRect.Top + );
RectangleF textRect = new RectangleF(m_oTailRect.X, m_oTailRect.Y + , m_oTailRect.Width, m_oTailRect.Height - );
string text = "第 " + m_nFromPage.ToString() + " 页 共 " + m_nTotalPage.ToString() + " 页";
m_nFromPage++;
CenterText(g, text, m_oTailFont, defaultBrush, textRect);
string time = "打印时间:" + DateTime.Now.ToLongDateString() + " ";
RightText(g, time, m_oTailFont, defaultBrush, textRect);
} /// <summary>
/// 打印一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Inch;
e.Graphics.PageScale = .01f;
if (m_bIsAlwaysPrintHeader == true)
{
PrintPageHeader(e.Graphics);
}
else
{
if (m_nCurPrintPage == )
{
PrintPageHeader(e.Graphics);
}
} PrintPageBody(e.Graphics);
PrintPageTail(e.Graphics);
if (m_nCurPrintPage == m_nPageCount)
{
e.HasMorePages = false;
}
else
{
e.HasMorePages = true;
}
m_nCurPrintPage++;
} /// <summary>
/// 打印前的初始化设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeginPrint(object sender, PrintEventArgs e)
{
m_nCurPrintPage = ;
} /// <summary>
/// 打印结束后的资源释放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EndPrint(object sender, PrintEventArgs e)
{ } /// <summary>
/// 居中显示文本信息
/// </summary>
private void CenterText(Graphics g, string t, Font f, Brush b, RectangleF rect)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(t, f, b, rect, sf);
} /// <summary>
/// 居右显示文本信息
/// </summary>
private void RightText(Graphics g, string t, Font f, Brush b, RectangleF rect)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(t, f, b, rect, sf);
} /// <summary>
/// 居左显示文本信息
/// </summary>
private void LeftText(Graphics g, string t, Font f, Brush b, RectangleF rect)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(t, f, b, rect, sf);
} /// <summary>
/// 打印 listview 的 columnheader
/// </summary>
private void DrawColumnHeader(Graphics g, Font f, Brush b, Rectangle rect)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Rectangle itemRect;
Point location = new Point(rect.Location.X, rect.Location.Y);
Size itemSize;
for (int i = ; i < this.Columns.Count; i++)
{
itemSize = new Size(this.Columns[i].Width, rect.Height);
itemRect = new Rectangle(location, itemSize);
g.DrawRectangle(defaultPen, itemRect);
g.DrawString(this.Columns[i].Text, f, b, itemRect, sf);
location.X += this.Columns[i].Width;
}
} /// <summary>
/// 打印 listview 的 item
/// </summary>
private void DrawItem(Graphics g, Font f, Brush b, Rectangle rect)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
Rectangle itemRect;
Point location = new Point(rect.Location.X, rect.Location.Y);
Size itemSize;
for (int i = ; i < this.Columns.Count; i++)
{
itemSize = new Size(this.Columns[i].Width, rect.Height);
itemRect = new Rectangle(location, itemSize);
g.DrawRectangle(defaultPen, itemRect);
g.DrawString(this.Items[m_nCurPrintItem].SubItems[i].Text, f, b, itemRect, sf);
location.X += this.Columns[i].Width;
}
}
}
c# listview数据预览(转载的放在这里备用)的更多相关文章
- IDT 数据预览查询
前面做了一件非常愚蠢的事情,由于不会预览数据.我都是直接发布到webi去查看的.可以想象一下了.真是太年轻了.为自己感到十分的汗颜. 在数据基础层做好连接之后,可以查看数据基础 .会显示相应的join ...
- Transformer中引用iqd作为数据源的时候数据预览出现乱码
在cognos开发利用transform建模的过程中导入iqd数据源预览乱码问题,下面先描述一下环境 操作系统版本: [root@enfo212 ~]# cat /proc/version Linux ...
- 利用tornado实现表格文件预览
项目介绍 本文将介绍笔者的一个项目,主要是利用tornado实现表格文件的预览,能够浏览的表格文件支持CSV以及Excel文件.预览的界面如下: 下面我们将看到这个功能是如何通过tornado ...
- 自己动手开发更好用的markdown编辑器-04(实时预览)
这里文章都是从个人的github博客直接复制过来的,排版可能有点乱. 原始地址 http://benq.im/2015/04/25/hexomd-04/ 程序打包 文章目录 1. 打开新窗口 ...
- 图片恢复有新招,EasyRecovery预览模式助你快速恢复
EasyRecovery作为一款数据恢复软件,因其便捷的操作.低廉的价格深受大家的喜爱.EasyRecovery具有"傻瓜式"操作,就算你是第一次接触这款软件,通过主页提示也能很快 ...
- 微信小程序手机预览请求不到数据(最后一条不明所以)
本地开发调试小程序时,用手机预览需要有如下设置:1.微信开发者工具中设置:不校验安全域名.web-view 域名.TLS 版本以及 HTTPS 证书.这样在有网络请求的时候,就可以访问本地的服务器了, ...
- 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 2、数据框架 和 功能预览
说明:MvcMovieStore项目已经发布上线,想了解最新版本功能请登录 MVC 影视(MvcMovie.cn) 进行查阅.如需转载,请注明出处:http://www.cnblogs.com/Dod ...
- 告别收费BI!如何自己动手做一个免费的可视化数据报表还支持文档在线预览?
本人大学刚毕业目前在一家互联网公司从事产品运营工作,一季度刚过,公司需要我出一份产品运营数据报表,由于产品用户数据.订单数据等数据量太大,我希望找一款Bi产品,支持我做出一个精美的可视化报表,还可以让 ...
- Dynamics AX 2012 R2 SSRS报表在VS2010中预览没有数据
今天,Reinhard 在VS中制作SSRS报表,预览的时候发现显示不出数据. 仔细检查了数据处理环节和临时表里的数据,都发现没有问题. 用同事的账号登陆同样的开发环境,发现他的账号可以在VS中预览到 ...
随机推荐
- c#计算器
代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴. using System;using System.Collections.Generic;using System. ...
- 第一次 刷 WiFi 模块esp8266 感谢创客阿正
在正哥指导下 第一次 刷 WiFi 模块 少走了 不少弯路 套件里的 两块 机智云 ==== 我的电脑 需要单独供电 先 对应 接好 ic0要记得接gnd 等待上电时要断电重启 等 用助手 返回 ...
- MAC Homebrew安装和简单使用
前言: 这个周六日在刚刚买的macbookpro(系统版本是:10.13.5)上面安装angular,一开始是按照windows上的顺序安装的,先安装node.js,然后在安装angular的时候报错 ...
- 从输入URL按下回车到页面展现,中间发生了什么?
从输入URL按下回车到页面展现,总的来说发生了一下几个过程: DNS 解析:将域名解析成 IP 地址 TCP 连接:TCP 三次握手 发送 HTTP 请求 服务器处理请求并返回 HTTP 报文 浏览器 ...
- DELL H730P写策略write-through和write-back配置说明
write-through 数据在写入存储的同时,要写入缓存,这种方式安全但是会牺牲写性能,因为只有等数据完全落入硬盘后,才算是一次io完成,这个过程会造成cpu的iowait. write-back ...
- Excel 导入时如何下载模板信息(Java)
大家知道,我们在实现 Excel 上传的时候,会让我们去下载个模板,然后实现导入功能.在此我在这里记录下来,以便后续的使用... 首先思考一个问题是 这个模板这么给前台,还有这个模板是这么来的,刚开始 ...
- entrySet用法 以及遍历map的用法
entrySet用法 以及遍历map的用法 keySet是键的集合,Set里面的类型即key的类型entrySet是 键-值 对的集合,Set里面的类型是Map.Entry 1.keySet( ...
- Spark读HBase写MySQL
1 Spark读HBase Spark读HBase黑名单数据,过滤出当日新增userid,并与mysql黑名单表内userid去重后,写入mysql. def main(args: Array[Str ...
- Spring+Mybatis+SpringMVC+Maven+MySql搭建实例(转)
http://blog.csdn.net/evankaka/article/details/48785513?spm=5176.100239.blogcont28591.10.9Fdj9R
- js: 文件(excel)下载处理
以前很少接触文件下载的功能,昨天和后台开发人员联调下载功能出现了问题,一开始我请求接口,返回二进制文件流乱码,在网上找了方法,可以解决.后面后台开发人员改了一下,返回文件地址,然后就解决了.所以我了解 ...