WinForm 打印

WinForm 打印

操作步骤:
1、新建winform项目及创建窗体

2、拖取 打印 相关控件
   PageSetupDialog 、 PrintDialog 、 PrintDocument 、PrintPreviewDialog

3、设置上述控件的Document属性为相应的PrintDocument

    ** (这块要注意,所有打印控件Document属性都设定为PrinDocument)**

4、设置按钮等控件 及 添加相应按钮事件

5、示意代码如下

 

 

  1.  
    代码
  2.  
     
  3.  
    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public partial class Form3 : Form
  4.  
    {
  5.  
    public Form3()
  6.  
    {
  7.  
    InitializeComponent();
  8.  
    this.printDocument1.OriginAtMargins = true;//启用页边距
  9.  
    this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
  10.  
     
  11.  
    }
  12.  
     
  13.  
    //打印设置
  14.  
    private void btnSetPrint_Click(object sender, EventArgs e)
  15.  
    {
  16.  
    this.pageSetupDialog1.ShowDialog();
  17.  
    }
  18.  
     
  19.  
    //打印预览
  20.  
    private void btnPrePrint_Click(object sender, EventArgs e)
  21.  
    {
  22.  
    this.printPreviewDialog1.ShowDialog();
  23.  
    }
  24.  
     
  25.  
    //打印
  26.  
    private void btnPrint_Click(object sender, EventArgs e)
  27.  
    {
  28.  
    if (this.printDialog1.ShowDialog() == DialogResult.OK)
  29.  
    {
  30.  
    this.printDocument1.Print();
  31.  
    }
  32.  
    }
  33.  
     
  34.  
    //打印内容的设置
  35.  
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  36.  
    {
  37.  
    ////打印内容 为 整个Form
  38.  
    //Image myFormImage;
  39.  
    //myFormImage = new Bitmap(this.Width, this.Height);
  40.  
    //Graphics g = Graphics.FromImage(myFormImage);
  41.  
    //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
  42.  
    //e.Graphics.DrawImage(myFormImage, 0, 0);
  43.  
     
  44.  
    ////打印内容 为 局部的 this.groupBox1
  45.  
    //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
  46.  
    //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
  47.  
    //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
  48.  
     
  49.  
    //打印内容 为 自定义文本内容
  50.  
    Font font = new Font("宋体", 12);
  51.  
    Brush bru = Brushes.Blue;
  52.  
    for (int i = 1; i <= 5; i++)
  53.  
    {
  54.  
    e.Graphics.DrawString("Hello world ", font, bru, i*20, i*20);
  55.  
    }
  56.  
    }
  57.  
    }

  

 

转载于:https://www.cnblogs.com/sdwjm/archive/2013/04/10/3012794.html

posted @ 2020-04-03 08:04  JunGe的博客  阅读(108)  评论(0)    收藏  举报