当前位置:编程学习 > C#/ASP.NET >>

C# winform 窗体打印

如何打印在窗体上的控件呢?  -----   在窗体上,放入控件,然后要把控件里的值打印出来,谁有代码,共享一下,谢谢了! --------------------编程问答-------------------- 沙发,帮忙顶 --------------------编程问答-------------------- http://dev.csdn.net/author/javaprogramers/90b0d4c024fc4b758b5ed1cea7565447.html --------------------编程问答-------------------- 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);
   }
  
  }

 }
}

--------------------编程问答-------------------- up --------------------编程问答-------------------- 路过帮顶一下 --------------------编程问答-------------------- godgreat   那样不可以啊 
  我是要打印窗体上的控件里面的内容啊 --------------------编程问答-------------------- 如果要打印整个窗体
[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();
}




如果要打印内容,我是用RDLC,我用的是.NET2005
这个可以实现打印.不过我正在找直接打印的办法 --------------------编程问答-------------------- 我只能说个思路,太懒不想写代码。

做一个 PrintItem 类,为每个需要打印的控件创建一个 PrintItem,放到集合里,然后打印这个集合。 --------------------编程问答-------------------- mark --------------------编程问答-------------------- http://www.codeproject.com/dotnet/PrintingFormReport.asp --------------------编程问答-------------------- http://www.codeproject.com/dotnet/PrintingFormReport.asp
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,