winform文本打印

这是我昨天根据网上的资料,实现的一个文本打印的测试

其它打印正在学习中,如果谁有DataGridView打印的可以给我分享下不?在这谢谢了啊》

 

 

新建一个窗体:文本打印

在-->文本打印.Designer.cs中

加入printDocument,并监听事件

 

代码
partialclass 文本打印
{
///<summary>
/// 必需的设计器变量。
///</summary>
private System.ComponentModel.IContainer components =null;
private PrintDocument printDocument;
///<summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///</summary>
privatevoid InitializeComponent()
{
this.printDocument =new System.Drawing.Printing.PrintDocument();
this.printDocument.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(this.printDocument_PrintPage);

//.......
}
}

 

在-->后台代码中:

 

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;

namespace PrintPageTest
{
publicpartialclass 文本打印 : Form
{
public 文本打印()
{
InitializeComponent();
}

privatevoid Form1_Load(object sender, EventArgs e)
{

}
StringReader lineReader
=null;


privatevoid printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g
= e.Graphics; //获得绘图对象
float linesPerPage =0; //页面的行号
float yPosition =0; //绘制字符串的纵向位置
int count =0; //行计数器
float leftMargin = e.MarginBounds.Left; //左边距
float topMargin = e.MarginBounds.Top; //上边距
string line =null; //行字符串
Font printFont =this.textBox1.Font; //当前的打印字体
SolidBrush myBrush =new SolidBrush(Color.Black);//刷子
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数
//逐行的循环打印一页
while (count < linesPerPage && ((line = lineReader.ReadLine()) !=null))
{
yPosition
= topMargin + (count * printFont.GetHeight(g));
g.DrawString(line, printFont, myBrush, leftMargin, yPosition,
new StringFormat());
count
++;
}

//如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件
//在下一次的打印中lineReader会自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员
//它可以记录当前读取的位置
if (line !=null)
e.HasMorePages
=true;
else
e.HasMorePages
=false;
}

//打印设置,构造打印对话框 将对话框中设置的Document属性赋给printDocument
//这样会将用户的设置自动保存到printDocument的PrinterSettings属性中
protectedvoid FileMenuItem_PrintSet_Click(object sender, EventArgs e)
{
PrintDialog printDialog
=new PrintDialog();
printDialog.Document
= printDocument;
printDialog.ShowDialog();
}


//页面设置和打印预览与打印设置原理相同都是构造对话框将用户在对话框中的设置保存到相应的类的属性中
protectedvoid FileMenuItem_PageSet_Click(object sender, EventArgs e)
{
PageSetupDialog pageSetupDialog
=new PageSetupDialog();
pageSetupDialog.Document
= printDocument;
pageSetupDialog.ShowDialog();
}

//打印预览
protectedvoid FileMenuItem_PrintView_Click(object sender, EventArgs e)
{
PrintPreviewDialog printPreviewDialog
=new PrintPreviewDialog();
printPreviewDialog.Document
= printDocument;
lineReader
=new StringReader(textBox1.Text);
try
{
printPreviewDialog.ShowDialog();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message,
"打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


//打印就可以直接调用printDocument的Print()方法因为用户可能在打印之前还要再更改打印设置
//所以在这里再次显示打印设置对话框
protectedvoid FileMenuItem_Print_Click(object sender, EventArgs e)
{
PrintDialog printDialog
=new PrintDialog();
printDialog.Document
= printDocument;
lineReader
=new StringReader(textBox1.Text);
if (printDialog.ShowDialog() == DialogResult.OK)
{
try
{
printDocument.Print();

}
catch (Exception excep)
{
MessageBox.Show(excep.Message,
"打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument.PrintController.OnEndPrint(printDocument,
new PrintEventArgs());
}
}
}
}
}

 

 

posted @ 2010-04-18 09:24  Mark_ybh  阅读(720)  评论(9编辑  收藏  举报