请教关于printdocument打印的问题
请教下 printdocument
可以直接绘制 窗体的控件吗 不用用印屏幕的方式
因为我控件是根据数值自动增加的
[解决办法]
读窗体大小画窗体及其Text等属性
遍历窗体所有控件,画控件及其Text等属性
[解决办法]
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());
}
}
//实现C#打印窗体 public static void Main()
//实现C#打印窗体
public static void Main()
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
public class Form1 : Form
{
//实现C#打印窗体
private Button printButton = new Button();
//动态创建的可以打印控件
private PrintControl printControl = new PrintControl();
public Form1()
{
this.Size = new System.Drawing.Size(500, 400);
printButton.Text = "Print Button";
printButton.Size = new Size(180, 40);
printButton.Click += new EventHandler(printButton_Click);
this.Controls.Add(printButton);
printControl = new PrintControl();
printControl.Location = new Point(100, 100);
printControl.Size = new Size(280, 40);
printControl.Text = "点击Print Button按钮可以打印它";
this.Controls.Add(printControl);
}
void printButton_Click(object sender, EventArgs e)
{
printControl.print();
}
//实现C#打印窗体
public static void Main()
{
Application.Run(new Form1());
}
}
//可打印的控件,基类可以改成其他的如Control
public class PrintControl : Button
{
private PrintDocument printDocument1 = new PrintDocument();
public PrintControl()
{
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
public void print()
{
CaptureScreen();
printDocument1.Print();
}
//实现C#打印窗体
Bitmap memoryImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
Point p = this.PointToScreen(new Point(0, 0));
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(p.X,p.Y, 0, 0, s);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, this.Location.X, this.Location.Y);
}
}