C#打印控件的使用实例

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

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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值