在C#.Net中打印Windows窗体 [英] Print Windows form in C#.Net

查看:54
本文介绍了在C#.Net中打印Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有打印窗体的问题!但是我成功打印了表单的数据网格,但是我没有想法打印我在论坛中打印win-forms但是找不到相关的表格。几乎所有项目都是在VB.Net中完成的,但是我使用的是Dot net 4框架!我有一个带有2个日期选择器的表单1)从日期2)到日期和搜索按钮。每当我点击搜索按钮时,根据日期的结果显示在网格视图中,如果我点击我的表格上的打印按钮,我可以打印网格,我已经创建了一个字符串来打印当前日期,也打印但我现在想要的是打印日期选择器的内容!例如,如果我触发打印命令,它必须打印网格以及两个日期选择器的值!我们怎样才能实现表格的印刷?如果有人需要印刷网格代码我可以提供它,除非有必要!我正在寻求帮助打印表格的内容(自定义内容)。

I have a problem with printing windows form! However i am successful in printing the Data-grid of the form but don't have idea to print form I have researched in printing win-forms in forums but could not find the relevant . Almost all project are done in VB.Net but m using Dot net 4 framework! I have a form with 2 date picker 1) From Date 2) to date and a search button . Whenever i click search button the result according to date are shown in grid view and if i click on Print button on my form I can print the grid and i have made a string to print the current date which is also printed but what i want now is to print the content of date picker! For example if i fire print command it must print grid as well as the value of both date picker! How can we achieve printing of forms? If somebody needs codes of printing grid I can provide it unless it is necessary! I am seeking help in printing contents(Customized Contents) of form .

推荐答案

不打印表格,不打印数据网格。使用视图后面的某些数据层打印数据。如果使用与数据网格绑定的某些数据层,请使用此数据进行打印。即使您没有数据绑定,也意味着您应该使用一些代码来填充数据网格和其他控件。用它来打印。使用System.Drawing.Printing.PrintDocument类,请参阅 http:// msdn。 microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx [ ^ ];此页面包含一些有用的代码示例。



-SA
Don't print the form, don't print the data grid. Print data using some data layer behind your view. If you use some data layer bound with your data grid, print using this data. Even if you don't have data binding, it means you should have some code used to populate the data grid and other controls. Use it for printing. Use the class System.Drawing.Printing.PrintDocument, see http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx[^]; this page contains some useful code sample.

—SA


正如您指定的那样想要在DataGridView旁边打印表单的自定义内容,并且还提到您可以打印datagridview。下面是一些代码行,用于打印表单的其他内容,如图像,文本,日期等:

As you have specified that you want to print customized contents of the form beside DataGridView and also mentioned that you are able to print the datagridview. Some lines of code are here below for print other contents of the form like image, text, date etc.:
// 1)	For printing an image above each page 
// In PrintPage event of PrintDocument1, after setting margins use following code 
 
string app_path =  System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); // you can change path as required 
Point p2 = new Point(900, 5);	// Display a picture on top of each page 
e.Graphics.DrawImage(Image.FromFile(app_path + "\\Books.ico"), p2); 

// 2)	For Writing any message on top of the pages 
e.Graphics.DrawString("Books List - Subjectwise",
           new Font(dataGridView3.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left - 15,
e.MarginBounds.Top - e.Graphics.MeasureString("Books List - Subjectwise",
new Font(dataGridView3.Font, FontStyle.Bold),
e.MarginBounds.Width).Height - 10);  // 10 is the gap between top margin and Header

// 3)	For header date 
      String strDate = DateTime.Now.ToLongDateString();
      e.Graphics.DrawString(strDate,
      new Font(dataGridView3.Font, FontStyle.Bold), Brushes.Black,
      e.MarginBounds.Left - 15 +
      (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
      new Font(dataGridView3.Font, FontStyle.Bold),
      e.MarginBounds.Width).Width) - 100,
      e.MarginBounds.Top - e.Graphics.MeasureString("Report Date",
      new Font(new Font(dataGridView3.Font, FontStyle.Bold),
FontStyle.Bold), e.MarginBounds.Width).Height - 13);

// 4)	Here you can print your DataGridView contents 





以上代码是用C#2.0编写的

希望这对你有用。



Above Code is written in C#2.0
Hope this will be useful for you.


你可以使用dateTimePicker的'Text'属性在将它转换为DateTime类型后将它存储在DateTime变量中,如下所示:



You can use 'Text' property of dateTimePicker to store it in DateTime variable after converting it to DateTime type as below:

DateTime Issuedt = Convert.ToDateTime(dateTimePicker1.Text);





现在Issuedt可用于printPage事件。



Now Issuedt can be used in printPage event.


这篇关于在C#.Net中打印Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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