生活随笔
收集整理的這篇文章主要介紹了
C#打印控件的使用实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#打印控件的使用是如何的呢?我們在編寫C#打印控件實用程序的時候怎么操作呢?首先弄一個printDocument控件,然后在打印的按鈕中直接調用printDocument1.print()事件. 再次就是寫printDocument的PrintPag事件了. 下面C#打印控件的使用具體實例:
privatevoid button1_Click(object sender, EventArgs e) { printDocument1.Print(); } privatevoid printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Font tabelTextFont = new Font("宋體", 10); if (dataGridView1.DataBindings != null) { int[] columnsWidth = newint[dataGridView1.Columns.Count]; int[] columnsLeft=newint[dataGridView1.Columns.Count]; //C#打印控件的使用之{ columnsWidth[c] = (int)e.Graphics.MeasureString( dataGridView1.Columns[c].HeaderText , tabelTextFont).Width; } for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++) { for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++) { int w = (int)e.Graphics.MeasureString( dataGridView1.Columns[columnIndex].Name , tabelTextFont).Width; columnsWidth[columnIndex] = w > columnsWidth[columnIndex] ? w : columnsWidth[columnIndex]; } }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)); }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));} } 另外要是想有打印預覽的話,那就還要一個printPreviewDialog控件了。要把該控件的document事件和PrintDocument關聯起來,就OK
總結
以上是生活随笔為你收集整理的C#打印控件的使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。