【文件属性】:
文件名称:C#开发的程序打印源码
文件大小:66KB
文件格式:HTM
更新时间:2012-12-27 15:30:07
打印
求打印预览和打印的C#代码和命名空间
VS2005 C#
提问者:tomore 提问时间:08-10-20 20:29
其他答案
--------------------------------------------------------------------------------
winu 的答案
///
/// 定义打印文档
///
private PrintDocument printDocument = new PrintDocument();
///
/// 定义打印文本变量
///
private string text = string.Empty;
///
/// 打印事件处理代码
///
///
///
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
//关联打印预处理代码
printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
//关联打印代码
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
//定义打印对话框
PrintDialog printDialog = new PrintDialog();
//获得用户输入
DialogResult dr = printDialog.ShowDialog();
//如果确认则打印
if (dr == DialogResult.OK)
{
printDocument.Print();
}
else
{
return;
}
}
///
/// 打印前预处理代码
///
///
///
void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
//设置打印文本
text = textBox1.Text ;
}
///
/// 打印文档处理代码
///
///
///
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//用DrawString方法输出字符串
e.Graphics.DrawString(text, new Font("Arial", 10), Brushes.Black, 20, 20); ;
}