如何打印窗体中的控件?
我创建的控件显示在窗体里,要把所有的控件打印出来,怎样实现打印功能?
(最好给出例子代码)
由于初上csdn,积分较低,给的分少,请大家多多支持,在此多谢了。
问题点数:50、回复次数:8Top
1 楼TheAres(班门斧)回复于 2003-01-07 21:59:36 得分 30
调用BitBlt API将整个Form画到一个Image上,然后再将这个Image打印出来。基本步骤如下:
1. Import the BitBlt API function
2. Capture the image of the form
3. Draw the image in the PrintPage event
具体代码如下例所示:
[DllImport( "gdi32.dll ")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
private const Int32 SRCCOPY = 0xCC0020;
private Bitmap memImage;
private void PrepareImage()
{
Graphics graphic = this.CreateGraphics();
Size s = this.Size;
memImage = new Bitmap(s.Width, s.Height, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width,
this.ClientRectangle.Height,dc1, 0, 0, SRCCOPY);
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
}
private void button1_Click(object sender, System.EventArgs e)
{
PrepareImage();
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memImage,0,0);
}Top
2 楼wwangmax(横刀立马)回复于 2003-01-08 09:26:28 得分 0
感谢TheAres。
如果不转换成image,怎样实现呢?
而且打印预览功能呢?Top
3 楼hhzh426(春之风)回复于 2003-01-08 10:18:47 得分 0
关注Top
4 楼stdao(道可道)回复于 2003-01-08 11:46:14 得分 5
其实转成图片是最好的做法了,不然你转成xml文件之类的,在web页面中打印也行。Top
5 楼project()回复于 2003-01-08 13:28:12 得分 5
可是,如果窗体没有显示所有的控件怎么办啊?
比如:窗体是可滚动的。Top
6 楼wwangmax(横刀立马)回复于 2003-01-09 15:16:29 得分 0
upTop
7 楼stdao(道可道)回复于 2003-01-09 17:03:56 得分 10
你在论坛搜索下“报表”,有人把窗体生成xml文件来实现打印,这个应该可以解决你的问题 :)Top
8 楼wwangmax(横刀立马)回复于 2003-01-10 09:45:04 得分 0
感谢stdao的回答,不过现在好像搜索不到了也,可不可以贴个例子上来。Top




