1 新建项目(窗体)
2 在窗体上添加printDocument 控件 添加 Button textBox 控件
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDialog printDialog1 = new PrintDialog(); // 声明定义 打印对话框
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();//弹出列印界面
if (result == DialogResult.OK)
{
//Print()方法打印內容,在打印之前,此方法會在PrntController類的幫助下先呼叫PrintPage事件的方法
printDocument1.Print();
}
}
/// <summary>
/// 绘制打印文本,用於得到打印內容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//textBox1 是打印的文本内容
//Arial 指定打印字体 20 指定打印字体大小 150, 125 指定在页面中的打印位置
e.Graphics.DrawString(textBox1.Text, new Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125);
}
/// <summary>
/// 打印结束后输出提示文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
label1.Text = "打印完毕";
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDialog printDialog1 = new PrintDialog(); // 声明定义 打印对话框
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();//弹出列印界面
if (result == DialogResult.OK)
{
//Print()方法打印內容,在打印之前,此方法會在PrntController類的幫助下先呼叫PrintPage事件的方法
printDocument1.Print();
}
}
/// <summary>
/// 绘制打印文本,用於得到打印內容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//textBox1 是打印的文本内容
//Arial 指定打印字体 20 指定打印字体大小 150, 125 指定在页面中的打印位置
e.Graphics.DrawString(textBox1.Text, new Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125);
}
/// <summary>
/// 打印结束后输出提示文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
label1.Text = "打印完毕";
}
}
}