C#打印控件的使用实例浅析

开发 后端
C#打印控件的使用实例向你介绍了具体的C#打印控件的使用细节,希望对你了解和使用C#打印控件有所帮助。

C#打印控件的使用是如何的呢?我们在编写C#打印控件实用程序的时候怎么操作呢?首先弄一个printDocument控件,然后在打印的按钮中直接调用printDocument1.print()事件. 再次就是写printDocument的PrintPag事件了. 下面C#打印控件的使用具体实例:

//C#打印控件的使用实例  
private void button1_Click(object sender, EventArgs e)  
 
{  
 
 printDocument1.Print();  
 
}  
 
private void printDocument1_PrintPage(object sender,   
System.Drawing.Printing.PrintPageEventArgs e)  
 
{  
 
Font tabelTextFont = new Font("宋体", 10);  
 
if (dataGridView1.DataBindings != null)  
 
{  
 
int[] columnsWidth =   
new int[dataGridView1.Columns.Count];  
//C#打印控件的使用之得到所有列的个数  
 
int[] columnsLeft=new int[dataGridView1.Columns.Count];   
//for (int c = 0; c < columnsWidth.Length; c++)  
//C#打印控件的使用之得到列标题的宽度  
 
{  
 
columnsWidth[c] = (int)e.Graphics.MeasureString(  
dataGridView1.Columns[c].HeaderText , tabelTextFont).Width;  
 
}  
 
for (int rowIndex = 0;   
rowIndex < dataGridView1.Rows.Count; rowIndex++)  
//C#打印控件的使用之rowindex当前行  
 
{  
 
for (int columnIndex = 0;   
columnIndex < dataGridView1.Columns.Count; columnIndex++)  
//C#打印控件的使用之当前列  
 
 {  
 
 int w = (int)e.Graphics.MeasureString(  
dataGridView1.Columns[columnIndex].Name ,   
tabelTextFont).Width; columnsWidth[columnIndex] =   
w > columnsWidth[columnIndex] ? w : columnsWidth[columnIndex];  
 
}  
 
 }//C#打印控件的使用  
 
int rowHidth = 20;  
 
int tableLeft=60;  
 
int tableTop=70;  
 
columnsLeft[0]=tableLeft;  
 
for (int i=1;i<=columnsWidth.Length -1;i++)  
 
{  
 
columnsLeft[i] = columnsLeft[i - 1] + columnsWidth[i - 1]+15;  
 
}  
 
StringFormat sf=new StringFormat ();  
 
sf.Alignment=StringAlignment.Center ;//居中打印  
 
e.Graphics.DrawString("欢迎石印死了开的交流!",   
new Font("宋体", 15), Brushes.Black, new Point(  
e.PageBounds.Width / 2, 20),sf );//打印标题  
 
for (int c = 0; c < columnsWidth.Length; c++)  
//打印表中的列名  
 
{  
 
e.Graphics.DrawString(dataGridView1.Columns[c].HeaderText,  
new Font ("宋体",10,FontStyle.Bold),   
Brushes.Black, new Point(columnsLeft[c], tableTop));  
 e.Graphics.DrawLine(Pens.Black,  
 new Point(columnsLeft[c]-5, tableTop - 5),   
new Point(columnsLeft[c]-5, tableTop +  
 (dataGridView1 .Rows .Count+1)*rowHidth));  
 
}//C#打印控件的使用  
 
e.Graphics.DrawLine(Pens.Black,   
new Point(columnsLeft[dataGridView1.Columns.Count - 1] +   
columnsWidth[dataGridView1.Columns.Count - 1],   
tableTop - 5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] +   
columnsWidth[dataGridView1.Columns.Count - 1],   
tableTop + (dataGridView1.Rows.Count + 1) * rowHidth));  
//画***面的线  
 
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[0] - 5,   
tableTop - 5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] +  
 columnsWidth[dataGridView1.Columns.Count - 1], tableTop - 5));   
for (int rowIndex = 0;   
rowIndex < dataGridView1.Rows.Count; rowIndex++)//打印表中的内容  
 
{  
 
for (int columnIndex = 0;   
columnIndex < dataGridView1.Columns.Count; columnIndex++)  
 
{  
 
 e.Graphics.DrawString(  
dataGridView1.Rows[rowIndex].Cells[columnIndex].Value.ToString(),   
tabelTextFont, Brushes.Black, new Point(columnsLeft[columnIndex],   
tableTop + rowHidth * (rowIndex + 1)));  
 
}  
 
 e.Graphics.DrawLine(Pens.Black,   
new Point(columnsLeft[0]-5,   
tableTop + (rowIndex +1) * rowHidth-5),   
new Point(columnsLeft[dataGridView1.Columns.Count - 1] +   
columnsWidth[dataGridView1.Columns.Count - 1], tableTop +   
(rowIndex +1)*rowHidth-5));//循环画行  
 
}  
//C#打印控件的使用之  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.

另外要是想有打印预览的话,那就还要一个printPreviewDialog控件了。要把该控件的document事件和PrintDocument关联起来,就可以了。

C#打印控件的使用的实例应用就向你介绍到这里,希望对你了解和学习C#打印控件的使用有所帮助。

【编辑推荐】

  1. 实现C#打印文档实例详解
  2. C#打印文本文件实例详解
  3. C#打印设置实例解析
  4. C#Lpt端口打印类的操作浅析
  5. C#打印设置实现源码详解
责任编辑:仲衡 来源: 新浪博客
相关推荐

2009-08-26 13:48:31

C#打印条码

2009-08-13 14:56:46

C#的结构体使用

2009-08-11 14:45:41

C# DataGrid

2011-05-20 16:07:29

C#

2009-09-08 14:54:40

C# listBox控

2009-08-28 16:31:21

C# treeview

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-27 13:30:11

C# interfac

2009-08-28 15:05:35

C#编写Calenda

2009-09-04 17:58:38

C# Web Brow

2009-08-06 16:58:40

C#编写ActiveX

2009-08-12 10:35:50

C#调用ActiveX

2009-08-17 17:49:20

C# 枚举

2009-09-09 13:57:28

C# XML解析

2009-08-27 17:59:56

C#接口定义

2009-08-18 13:49:21

C# 操作Excel

2009-09-11 11:33:58

C# WinForm控Attribute

2009-08-27 18:09:49

C#接口的实现

2009-09-11 11:27:38

AttributeUsC# Attribut

2009-08-19 10:25:14

C#操作Word
点赞
收藏

51CTO技术栈公众号