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);
}
}
}