支持打印效能的ListView种

新近用C#做了个小收费系统,亟需用到支持打印效能的ListView种,在网上找了一个别人的如次:
http://www.chinaaspx.com/comm/dotnetbbs/Showtopic.aspx?Forum_ID=六&Id=156821

不过用的时分发现打印的时分打出来了页首和页脚,但就打不出来报表自身。
透过追踪,更正代码如次:
C#代码
///
/// ListView 的所见即所得的打印支持种。
///
public class PrintListView : ListView
{
///
/// 指示是不是开展打印预览,默认值为 true
///
private bool m_bIsPreview;

///
/// 指示是不是老是打印题目,默认值为 true
///
private bool m_bIsAlwaysPrintHeader;

///
/// 亟需打印的页首字符串
///
private string m_sPrintHeaderString;

///
/// 页首字体
///
private Font m_oHeaderFont;

///
/// 本文字体
///
private Font m_oBodyFont;

///
/// 页尾字体
///
private Font m_oTailFont;

///
/// 本文一部分的 ColumnHeader 字体,该字体为本文字体的加粗模式
///
private Font m_oColumnHeaderFont;

///
/// 打印文档
///
private PrintDocument m_oPrintDoc;


///
/// 行间距
///
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;

///
/// 设立或获取是不是开展打印预览
///
public bool IsPreview
{
get { return m_bIsPreview; }
set { m_bIsPreview = value; }
}


///
/// 设立或获取是不是老是打印题目
///
public bool IsAlwaysPrintHeader
{
get { return m_bIsAlwaysPrintHeader; }
set { m_bIsAlwaysPrintHeader = value; }
}


///
/// 设立或获取打印的页首字符串
///
public string PrintHeaderString
{
get { return m_sPrintHeaderString; }
set { if (value != null) m_sPrintHeaderString = value; }
}


///
/// 设立或获取页首字体
///
public Font HeaderFont
{
set { m_oHeaderFont = value; }
get { return m_oHeaderFont; }
}


///
/// 设立或获取本文字体
/// 如其对本文字体的 Bold 属性设立为 true ,则产生 Exception 门类的异常
///
public Font BodyFont
{
set
{
if (value == null)
return;
if (value.Bold == true)
{
throw new Exception("本文字体不能进展 [加粗] 设立.");
}
else
{
m_oBodyFont = value;
}
}
get { return m_oBodyFont; }
}


///
/// 设立或获取页尾字体
///
public Font TailFont
{
set { m_oTailFont = value; }
get { return m_oTailFont; }
}


///
/// 设立或获取行间距
///
public int LineSpace
{
get { return m_nLineSpace; }
set
{
if (value < 零)
{
m_nLineSpace = 零;
}
m_nLineSpace = value;
}
}


///
/// 结构函数,设立打印信息的一些默认值
///
public PrintListView()
{
m_bIsPreview = true;
m_bIsAlwaysPrintHeader = 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, 一);
}


///
/// 初始化打印文档的属性
///
///
private void InitPrintDocument()
{
m_oPrintDoc = new PrintDocument();
m_oPrintDoc.DocumentName = m_sPrintHeaderString;
if (m_oPrintDoc.PrinterSettings.PrinterName == "")
{
throw new Exception("未找出默许打印机.");
}
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("楷书_GB2312", 16, FontStyle.Bold, GraphicsUnit.World);
}

if (m_oBodyFont == null)
{
m_oBodyFont = new Font("楷书_GB2312", 14, FontStyle.Regular, GraphicsUnit.World);
}

m_oColumnHeaderFont = new Font(m_oBodyFont, FontStyle.Bold);

if (m_oTailFont == null)
{
m_oTailFont = new Font("楷书_GB2312", 12, FontStyle.Regular, GraphicsUnit.World);
}
}


///
/// 初始化打印纸张设立
///
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("纸张设立错处.");
}

/* 将目前 listview 的各column的长度和变换为英寸,判断打印范畴是不是偷越 */
int listViewWidth = 零;
for (int i = 零; i < this.Columns.Count; i++)
{
listViewWidth += this.Columns[i].Width;
}
if (Convert.ToInt32(listViewWidth / x * 100) > m_nPrintWidth)
{
//throw new Exception("打印内容超出纸张范畴 !/r/n请尝试整合纸张或纸张边距;/r/n或缩小报表各列的宽度。");
}
m_oPrintDoc.DefaultPageSettings = ps.PageSettings;
return true;
}


///
/// 初始化打印页码设立
///
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;
}
}


///
/// 起动 ListView 的打印工作
///
public virtual void DoPrint()
{
if (this.Items.Count <= 零)
{
throw new Exception("没急需打印的元素.");
}
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();
}
}


///
/// 打印页首
///
///
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 - 二);
}

///
/// 打印本文
///
///
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;
}
m_nCurPrintItem = 零; // added by Wooce
}

///
/// 打印页尾
///
///
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);
}


///
/// 打印一页
///
///
///
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++;

}


///
/// 打印前的初始化设立
///
///
///
private void BeginPrint(object sender, PrintEventArgs e)
{
m_nCurPrintPage = 一;
}


///
/// 打印完结后的资源开释
///
///
///
private void EndPrint(object sender, PrintEventArgs e)
{
}


///
/// 正中展示文本信息
///
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);
}


///
/// 居右展示文本信息
///
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);
}


///
/// 居左展示文本信息
///
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);
}

///
/// 打印 listview 的 columnheader
///
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;
}
}


///
/// 打印 listview 的 item
///
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;
}
}
}

本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值