简体   繁体   English

如何打印Winforms用户控件

[英]How to print a winforms usercontrol

I've created a winforms usercontrol doing a lot of custom painting in the OnPaint event. 我创建了一个winforms用户控件,它在OnPaint事件中进行了大量自定义绘制。 Now I would like to print this control. 现在,我想打印此控件。

  • is there a wysiwyg method to position this control on 'a document' and print the document including the usercontrol ? 是否有一种所见即所得的方法来将该控件放置在“文档”上并打印包含用户控件的文档?

or 要么

  • when using printpage() event of a printdocument, can I dynamically create & position my usercontrol on the 'page' and somehow call the paint method of my usercontrol ? 当使用printdocument的printpage()事件时,我可以在'page'上动态创建和放置用户控件,并以某种方式调用我的usercontrol的paint方法吗?

In PrintPage event aquire image of control, and draw it to printer graphics. 在PrintPage事件中,获取控件的图像,并将其绘制到打印机图形上。

private void PrintPageEventHandler(object sender, PrintPageEventArgs ev) {
            Rectangle rect = new Rectangle(0, 0, yourControl.Width, yourControl.Height);

            //aquire image of yourControl
            Bitmap bitmap = new Bitmap(yourControl.Width, yourControl.Height);     
            yourControl.DrawToBitmap(bitmap, rect);

            ev.Graphics.DrawImage(bitmap, new Point(0, 0)); //place image on right position istead of (0, 0)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM