http://www.cnblogs.com/freeliver54/archive/2010/10/20/1856978.html
c# winform 打印 窗体 及 窗体控件内容 的 初级尝试
winform打印 用到的控件
PrintDialog控件
用于选择打印机、选择要打印的页以及确定其他与打印相关的设置。通过PrintDialog控件可以选择全部打印、打印选定的页范围或打印选定内容
PrintPreviewControl控件
用于按文档打印时的外观显示文档
PrintPreviewDialog控件
用于显示文档打印后的外观
PrintDocument控件(收藏)
设置打印的文档
PageSetupDialog控件
用于设置页面详细信息以便打印
public print()
{
InitializeComponent();
this.printDocument1.OriginAtMargins = true;//启用页边距
this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
}
//打印设置
private void btnSetPrint_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
//打印预览
private void btnPrePrint_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.ShowDialog();
}
//打印
private void btnPrint_Click(object sender, EventArgs e)
{
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
}
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
////打印内容 为 整个Form
//Image myFormImage;
//myFormImage = new Bitmap(this.Width, this.Height);
//Graphics g = Graphics.FromImage(myFormImage);
//g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
//e.Graphics.DrawImage(myFormImage, 0, 0);
////打印内容 为 局部的 this.groupBox1
//Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
//groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
//e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
//打印内容 为 自定义文本内容
//Font font = new Font("宋体", 12);
//Brush bru = Brushes.Blue;
//for (int i = 1; i <= 5; i++)
//{
// e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
//}
Graphics g = e.Graphics; //先建立画布
int x = 80;
int y = 60;
g.DrawImage(this.BackgroundImage, 50, 50);
foreach (Control item in this.Controls)
{
if (item is TextBox)
{
Control tx = (item as Control);
g.DrawString(tx.Text, tx.Font, Brushes.Black, tx.Left + x, tx.Top + y);
}
}
}