自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#打印控件的使用實(shí)例淺析

開(kāi)發(fā) 后端
C#打印控件的使用實(shí)例向你介紹了具體的C#打印控件的使用細(xì)節(jié),希望對(duì)你了解和使用C#打印控件有所幫助。

C#打印控件的使用是如何的呢?我們?cè)诰帉?xiě)C#打印控件實(shí)用程序的時(shí)候怎么操作呢?首先弄一個(gè)printDocument控件,然后在打印的按鈕中直接調(diào)用printDocument1.print()事件. 再次就是寫(xiě)printDocument的PrintPag事件了. 下面C#打印控件的使用具體實(shí)例:

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

另外要是想有打印預(yù)覽的話,那就還要一個(gè)printPreviewDialog控件了。要把該控件的document事件和PrintDocument關(guān)聯(lián)起來(lái),就可以了。

C#打印控件的使用的實(shí)例應(yīng)用就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#打印控件的使用有所幫助。

【編輯推薦】

  1. 實(shí)現(xiàn)C#打印文檔實(shí)例詳解
  2. C#打印文本文件實(shí)例詳解
  3. C#打印設(shè)置實(shí)例解析
  4. C#Lpt端口打印類的操作淺析
  5. C#打印設(shè)置實(shí)現(xiàn)源碼詳解
責(zé)任編輯:仲衡 來(lái)源: 新浪博客
相關(guān)推薦

2009-08-26 13:48:31

C#打印條碼

2009-08-13 14:56:46

C#的結(jié)構(gòu)體使用

2011-05-20 16:07:29

C#

2009-08-11 14:45:41

C# DataGrid

2009-09-08 14:54:40

C# listBox控

2009-08-28 16:31:21

C# treeview

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2009-08-27 13:30:11

C# interfac

2009-08-12 10:35:50

C#調(diào)用ActiveX

2009-08-06 16:58:40

C#編寫(xiě)ActiveX

2009-09-04 17:58:38

C# Web Brow

2009-08-28 15:05:35

C#編寫(xiě)Calenda

2009-08-17 17:49:20

C# 枚舉

2009-09-09 13:57:28

C# XML解析

2009-08-18 13:49:21

C# 操作Excel

2009-08-27 17:59:56

C#接口定義

2009-09-11 11:33:58

C# WinForm控Attribute

2009-08-27 18:09:49

C#接口的實(shí)現(xiàn)

2009-09-11 11:27:38

AttributeUsC# Attribut

2009-08-19 10:25:14

C#操作Word
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)