如何在C#.NET中保存和打印包含许多页面的Windows窗体。 [英] How to save and print a windows form that has many pages in C#.NET.

查看:97
本文介绍了如何在C#.NET中保存和打印包含许多页面的Windows窗体。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有4页的Windows表单。我的表单包含带有值和标签的文本框。我并尝试保存表单并打印出来。我只能打印第一页。如何打印其他页面?当我尝试保存它时,我得到了空白页面。我使用的代码如下:谢谢。



我的尝试:



[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);

图形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,13368376);

mygrap hics.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 printToolStripButton_Click(object sender,EventArgs e)

{

CaptureScreen() ;

printDocument1.Print();

}





private void saveToolStripButton_Click(object sender,EventArgs e)

{

Stream myStream;

saveFileDialog1.Filter =所有文件(*。*)| * 。*;

saveFileDialog1.FilterIndex = 2;

saveFileDialog1.RestoreDirectory = true;



if( saveFileDialog1.ShowDia log()== DialogResult.OK)

{

if((myStream = saveFileDialog1.OpenFile())!= null)

{

使用(var writer = new StreamWriter(myStream))

{

CaptureScreen();

writer。 WriteLine();

}

myStream.Close();

}

}

}

I have a Windows form that has 4 pages. My form has text boxes with values, and labels. I and trying to save the form and print it. I am able to print only the first page. How can I print the other pages? When I try to save it, I get the blank page. The code I am using is below: Thanks.

What I have tried:

[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 printToolStripButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}


private void saveToolStripButton_Click(object sender, EventArgs e)
{
Stream myStream;
saveFileDialog1.Filter = "All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
using (var writer = new StreamWriter(myStream))
{
CaptureScreen();
writer.WriteLine();
}
myStream.Close();
}
}
}

推荐答案

不要这样做!

而不是拍摄屏幕图像,打印表格的内容 - 它在各种控件中显示的数据 - 直接显示在PrintDocument上。

PrintDocument Class(System.Drawing.Printing) [ ^ ]包含一个基本示例。
Don't do it like that!
Instead of taking a screen image, print the content of the form - the data it is displaying in the various controls - directly onto your PrintDocument.
PrintDocument Class (System.Drawing.Printing)[^] includes a basic example.


这篇关于如何在C#.NET中保存和打印包含许多页面的Windows窗体。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆