C# winform 窗体打印

using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   System.Drawing.Printing;

namespace   WindowsApplication1
{
  ///   <summary>
  ///   Form1   的摘要说明。
  ///   </summary>
  public   class   Form1   :   System.Windows.Forms.Form
  {
    private   System.Drawing.Printing.PrintDocument   printDocument1;
    private   System.Windows.Forms.PageSetupDialog   pageSetupDialog1;
    private   System.Windows.Forms.PrintPreviewDialog   printPreviewDialog1;
    private   System.Windows.Forms.PrintDialog   printDialog1;
    private   System.Windows.Forms.Button   button1;
    private   System.Windows.Forms.Button   button2;
    private   System.Windows.Forms.Button   button3;
    private   System.Windows.Forms.Button   tbDraw;
    private   PageSettings   pageSet;
    ///   <summary>
    ///   必需的设计器变量。
    ///   </summary>
    private   System.ComponentModel.Container   components   =   null;

    public   Form1()
    {
      //
      //   Windows   窗体设计器支持所必需的
      //
      InitializeComponent();

      //
      //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码
      //
    }

    ///   <summary>
    ///   清理所有正在使用的资源。
    ///   </summary>
    protected   override   void   Dispose(   bool   disposing   )
    {
      if(   disposing   )
      {
        if   (components   !=   null)  
        {
          components.Dispose();
        }
      }
      base.Dispose(   disposing   );
    }

    #region   Windows   窗体设计器生成的代码
    ///   <summary>
    ///   设计器支持所需的方法   -   不要使用代码编辑器修改
    ///   此方法的内容。
    ///   </summary>
    private   void   InitializeComponent()
    {
      System.Resources.ResourceManager   resources   =   new   System.Resources.ResourceManager(typeof(Form1));
      this.printDocument1   =   new   System.Drawing.Printing.PrintDocument();
      this.pageSetupDialog1   =   new   System.Windows.Forms.PageSetupDialog();
      this.printPreviewDialog1   =   new   System.Windows.Forms.PrintPreviewDialog();
      this.printDialog1   =   new   System.Windows.Forms.PrintDialog();
      this.button1   =   new   System.Windows.Forms.Button();
      this.button2   =   new   System.Windows.Forms.Button();
      this.button3   =   new   System.Windows.Forms.Button();
      this.tbDraw   =   new   System.Windows.Forms.Button();
      this.SuspendLayout();
      //  
      //   printDocument1
      //  
      this.printDocument1.PrintPage   +=   new   System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
      //  
      //   printPreviewDialog1
      //  
      this.printPreviewDialog1.AutoScrollMargin   =   new   System.Drawing.Size(0,   0);
      this.printPreviewDialog1.AutoScrollMinSize   =   new   System.Drawing.Size(0,   0);
      this.printPreviewDialog1.ClientSize   =   new   System.Drawing.Size(400,   300);
      this.printPreviewDialog1.Enabled   =   true;
      this.printPreviewDialog1.Icon   =   ((System.Drawing.Icon)(resources.GetObject( "printPreviewDialog1.Icon ")));
      this.printPreviewDialog1.Location   =   new   System.Drawing.Point(305,   17);
      this.printPreviewDialog1.MinimumSize   =   new   System.Drawing.Size(375,   250);
      this.printPreviewDialog1.Name   =   "printPreviewDialog1 ";
      this.printPreviewDialog1.TransparencyKey   =   System.Drawing.Color.Empty;
      this.printPreviewDialog1.Visible   =   false;
      //  
      //   button1
      //  
      this.button1.Location   =   new   System.Drawing.Point(144,   40);
      this.button1.Name   =   "button1 ";
      this.button1.Size   =   new   System.Drawing.Size(144,   23);
      this.button1.TabIndex   =   0;
      this.button1.Text   =   "printDialog ";
      this.button1.Click   +=   new   System.EventHandler(this.button1_Click);
      //  
      //   button2
      //  
      this.button2.Location   =   new   System.Drawing.Point(144,   88);
      this.button2.Name   =   "button2 ";
      this.button2.Size   =   new   System.Drawing.Size(144,   23);
      this.button2.TabIndex   =   1;
      this.button2.Text   =   "pageSetupDialog ";
      this.button2.Click   +=   new   System.EventHandler(this.button2_Click);
      //  
      //   button3
      //  
      this.button3.Location   =   new   System.Drawing.Point(144,   136);
      this.button3.Name   =   "button3 ";
      this.button3.Size   =   new   System.Drawing.Size(144,   23);
      this.button3.TabIndex   =   2;
      this.button3.Text   =   "printPreviewDialog ";
      this.button3.Click   +=   new   System.EventHandler(this.button3_Click);
      //  
      //   tbDraw
      //  
      this.tbDraw.Location   =   new   System.Drawing.Point(32,   48);
      this.tbDraw.Name   =   "tbDraw ";
      this.tbDraw.TabIndex   =   3;
      this.tbDraw.Text   =   "Draw ";
      this.tbDraw.Click   +=   new   System.EventHandler(this.tbDraw_Click);
      //  
      //   Form1
      //  
      this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);
      this.ClientSize   =   new   System.Drawing.Size(424,   302);
      this.Controls.Add(this.tbDraw);
      this.Controls.Add(this.button3);
      this.Controls.Add(this.button2);
      this.Controls.Add(this.button1);
      this.Name   =   "Form1 ";
      this.Text   =   "Form1 ";
      this.ResumeLayout(false);

    }
    #endregion

    ///   <summary>
    ///   应用程序的主入口点。
    ///   </summary>
    [STAThread]
    static   void   Main()  
    {
      Application.Run(new   Form1());
    }

    private   void   PaintDocument   (Graphics   g)
    {

      g.DrawRectangle(Pens.Black,100,100,100,100);   //图形
      g.DrawString( "打印测试 ",   this.Font,   Brushes.Black,100,300);   //如果多行,StreamRead
     
    }

    private   void   tbDraw_Click(object   sender,   System.EventArgs   e)
    {
      Graphics   g   =   this.CreateGraphics();   //为当前控件创建画布
      PaintDocument(g);
    }

    private   void   printDocument1_PrintPage(object   sender,   System.Drawing.Printing.PrintPageEventArgs   e)
    {
      Graphics   g   =   e.Graphics;   //先建立画布
      PaintDocument(g);      
    }

    private   void   button1_Click(object   sender,   System.EventArgs   e)
    {
      try
      {
        PrinterSettings   printerSet   =   new   PrinterSettings();   //打印机设置
        if(this.pageSet   !=   null)
        {
          printDocument1.DefaultPageSettings   =   this.pageSet;
        }
        printDialog1.PrinterSettings   =   printerSet;   //打印前设置打打印机

        printDialog1.Document   =   printDocument1;
        printDocument1.DocumentName   =   "test ";
        if(printDialog1.ShowDialog()   ==   DialogResult.OK)
        {
          printDocument1.Print();
        }
      }
      catch(   Exception   ex)
      {
        MessageBox.Show(ex.Message);
      }

    }

    private   void   button2_Click(object   sender,   System.EventArgs   e)
    {
      try
      {
        if(this.pageSet   ==   null)
        this.pageSet   =   new   PageSettings();
        pageSetupDialog1.PageSettings   =   this.pageSet;   //必须设置pageSet
//         pageSetupDialog1.Document   =   printDocument1;        
        pageSetupDialog1.ShowDialog();
      }
      catch(Exception   ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    private   void   button3_Click(object   sender,   System.EventArgs   e)
    {
      try
      {
        if(this.pageSet   !=   null)
          printDocument1.DefaultPageSettings   =   this.pageSet;

        printPreviewDialog1.Document   =   printDocument1;
        printPreviewDialog1.ShowDialog();
      }
      catch(Exception   ex)
      {
        MessageBox.Show(ex.Message);
      }
   
    }

  }
}
如果要打印整个窗体
[System.Runtime.InteropServices.DllImport( "gdi32.dll ")]
public   static   extern   long   BitBlt   (IntPtr   hdcDest,   int   nXDest,   int   nYDest,   int   nWidth,   int   nHeight,   IntPtr   hdcSrc,   int   nXSrc,   int   nYSrc,   int   dwRop);
private   Bitmap   memoryImage;
private   void   CaptureScreen()
{
        Graphics   mygraphics   =   this.CreateGraphics();
        Size   s   =   this.Size;
        memoryImage   =   new   Bitmap(s.Width,   s.Height,   mygraphics);
        Graphics   memoryGraphics   =   Graphics.FromImage(memoryImage);
        IntPtr   dc1   =   mygraphics.GetHdc();
        IntPtr   dc2   =   memoryGraphics.GetHdc();
        BitBlt(dc2,   0,   0,   this.ClientRectangle.Width,   this.ClientRectangle.Height,   dc1,   0,   0,   13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);
}
private   void   printDocument1_PrintPage(System.Object   sender,   System.Drawing.Printing.PrintPageEventArgs   e)
{
        e.Graphics.DrawImage(memoryImage,   0,   0);
}
private   void   printButton_Click(System.Object   sender,   System.EventArgs   e)
{
        CaptureScreen();
        printDocument1.Print();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值