如何在c#窗口应用程序中打印部分窗体? [英] How to print part of the window form in c# window application?

查看:325
本文介绍了如何在c#窗口应用程序中打印部分窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Window应用程序,其中一个要求是通过单击打印按钮打印Invoice,但是它给出了整个窗口形式的打印,而不是仅在组框中给出控件。

Hi, I'm working on Window application , where one of my requirement is to print Invoice by clicking print button, but it's giving me the print out of whole window form instead of giving only Controls in group box.

推荐答案

首先关闭:没有看到代码,很难帮到你。但是,如果要将屏幕控制打印到打印机,请立即停止!



查看设置 PrintDocument [ ^ ]而是以这种方式处理它 - 你会得到更好的结果!该链接有一个简单的例子。
First off: without see the code, it is very difficult to help you much. However, if you are printing screen controls to the printer, stop it now!

Look at setting up a PrintDocument[^] instead, and handling it that way - you will get much better results! The link has a simple example.


您需要使用类 System.Drawing.Printing.PrintDocument 。使用它的想法是:获取System.Drawing.Graphics类的实例并渲染所需的所有图形基元。



请参阅此处的代码示例:

http:/ /msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(v=VS.100).aspx [ ^ ],它解释了一切。



此外,请看这篇文章:

在C#中简化.NET打印 [ ^ ]。



-SA
You need to use the class System.Drawing.Printing.PrintDocument. The idea of using it is: you obtain an instance of the class System.Drawing.Graphics and render all the graphical primitive you need.

See the code sample here:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(v=VS.100).aspx[^], it explains everything.

Also, look at this article:
Simplified .NET Printing in C#[^].

—SA


它的工作原理,试试这段代码:)

Its working, try this code :)
private void button1_Click(object sender, EventArgs e)
       {
           PrintDocument doc = new PrintDocument();
           doc.PrintPage += this.Doc_PrintPage;
           PrintDialog dlgSettings = new PrintDialog();
           dlgSettings.Document = doc;
           if (dlgSettings.ShowDialog() == DialogResult.OK)
           {
               doc.Print();
           }
       }




private void Doc_PrintPage(object sender, PrintPageEventArgs e)
        {
             float x = e.MarginBounds.Left;
              float y = e.MarginBounds.Top;
            Bitmap bmp = new Bitmap(this.groupBox1.Width, this.groupBox1.Height);
            this.groupBox1.DrawToBitmap(bmp, new Rectangle(0, 0, this.groupBox1.Width, this.groupBox1.Height));
            e.Graphics.DrawImage((Image)bmp, x, y);
        }


这篇关于如何在c#窗口应用程序中打印部分窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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