c#中如何打印窗体

我现在想实现的功能是当form2窗体打开后,不需要提示,直接就打印form2窗体中的全部内容,不知道有什么控件没有,直接添加就可以完成此功能。3我现在是在load事件中写this.PrintForm();但是不对,c#中可以这样写吗??如果不能这么写,该怎么修改下,谢谢!!!!!!!

Bitmap memoryImage = null; //定义一个图片
public static PrintDocument printDocument = new PrintDocument(); //定义一个Print文档对象
private void CaptureScreen()//截屏
{
Form form = this;
if (form != null)
{
Graphics mygraphics = form.CreateGraphics();
memoryImage = new Bitmap(form.Width, form.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, form.Size);
}
}

在窗体的load里面写:

printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
printDocument.Print(); //打印

加上这个事件处理:
void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.DrawImage(memoryImage, 0, 0);
}

代码经过测试了。若还不明白,Hi我或email:lijiuning(a)gmail.com
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-07-20
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

public class Form1 :
Form
{//实现C#打印窗体
private Button printButton = new Button();
private PrintDocument printDocument1 = new PrintDocument();

public Form1()
{
printButton.Text = "Print Form";
printButton.Click += new EventHandler(printButton_Click);
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
this.Controls.Add(printButton);
}

void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}
//实现C#打印窗体
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);
memoryGraphics.CopyFromScreen(
this.Location.X, this.Location.Y, 0, 0, s);
}

private void printDocument1_PrintPage(System.Object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}

//实现C#打印窗体

public static void Main()
{
Application.Run(new Form1());
}
}
第2个回答  2010-07-20
请使用PrintForm类,它可以直接打印窗体,具体用法请参看msdn

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网