程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> winform打印和預覽,winform打印預覽

winform打印和預覽,winform打印預覽

編輯:C#入門知識

winform打印和預覽,winform打印預覽


  在windows應用程序中文檔的打印是一項非常重要的功能,在以前一直是一個非常復雜的工作,Microsoft .Net Framework的打印功能都以組件的方式提供,為程序員提供了很大的方便。由於工作中常用到印功功能,個人寫了一個專門打印DataGridView對象一個類,可以實現預覽和打印功能,而且自動縮放字段、添加顏色;在預覽時可以根據不同的比例查看效果,可以查看第幾頁數據和直接打印第幾頁的 數據。請看效果圖。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing.Printing; 6 using System.Windows.Forms; 7 using System.Drawing; 8 using Dys.Component; 9 namespace Bll 10 { 11 /// <summary> 12 /// 打印 13 /// 開心懶人 14 /// 2014-10-10 15 /// </summary> 16 public class PrintDataGridView 17 { 18 19 static DataGridView dgv; 20 static string titleName = ""; //標題名稱 21 static string titleName2 = ""; //第二標題名稱 22 static int rowIndex = 0; //當前行 23 static int page = 1; //當前頁 24 static int rowsPerPage = 0; //每頁顯示多少行 25 /// <summary> 26 /// 打印DataGridView 27 /// </summary> 28 /// <param name="dataGridView">要打印的DataGridView</param> 29 /// <param name="title">標題</param> 30 /// <param name="title2">第二標題,可以為null</param> 31 public static void Print(DataGridView dataGridView, string title, string title2) 32 { 33 try 34 { 35 if (dataGridView == null) { return; } 36 titleName = title; 37 titleName2 = title2; 38 dgv = dataGridView; 39 PrintPreviewDialog ppvw = new PrintPreviewDialog(); 40 ppvw.PrintPreviewControl.Zoom = 1.0; //顯示比例為100% 41 PrintDocument printDoc = new PrintDocument(); 42 PrintDialog MyDlg = new PrintDialog(); 43 MyDlg.Document = printDoc; 44 printDoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 850, 1000); 45 printDoc.DefaultPageSettings.Margins = new Margins(60, 60, 60, 60); //設置邊距 46 ppvw.Document = printDoc; //設置要打印的文檔 47 ((Form)ppvw).WindowState = FormWindowState.Maximized; //最大化 48 rowIndex = 0; //當前行 49 page = 1; //當前頁 50 printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 51 printDoc.EndPrint += new PrintEventHandler(printDoc_EndPrint); 52 ppvw.Document.DefaultPageSettings.Landscape = true; // 設置打印為橫向 53 ppvw.ShowDialog(); //打開預覽 54 55 } 56 catch (Exception ex) 57 { 58 MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 59 } 60 61 } 62 63 static void printDoc_EndPrint(object sender, PrintEventArgs e) 64 { 65 rowIndex = 0; //當前行 66 page = 1; //當前頁 67 rowsPerPage = 0;//每頁顯示多少行 68 } 69 private static void printDoc_PrintPage(object sender, PrintPageEventArgs e) 70 { 71 72 //標題字體 73 Font titleFont = new Font("微軟雅黑", 16, FontStyle.Bold); 74 //標題尺寸 75 SizeF titleSize = e.Graphics.MeasureString(titleName, titleFont, e.MarginBounds.Width); 76 //x坐標 77 int x = e.MarginBounds.Left; 78 //y坐標 79 int y = Convert.ToInt32(e.MarginBounds.Top - titleSize.Height); 80 //邊距以內紙張寬度 81 int pagerWidth = e.MarginBounds.Width; 82 //畫標題 83 e.Graphics.DrawString(titleName, titleFont, Brushes.Black, x + (pagerWidth - titleSize.Width) / 2, y); 84 y += (int)titleSize.Height; 85 if (titleName2 != null && titleName2 != "") 86 { 87 88 //畫第二標題 89 e.Graphics.DrawString(titleName2, dgv.Font, Brushes.Black, x + (pagerWidth - titleSize.Width) / 2 + 200, y); 90 //第二標題尺寸 91 SizeF titleSize2 = e.Graphics.MeasureString(titleName2, dgv.Font, e.MarginBounds.Width); 92 y += (int)titleSize2.Height; 93 94 } 95 96 //表頭高度 97 int headerHeight = 0; 98 //縱軸上 內容與線的距離 99 int padding = 6; 100 //所有顯示列的寬度 101 int columnsWidth = 0; 102 //計算所有顯示列的寬度 103 foreach (DataGridViewColumn column in dgv.Columns) 104 { 105 106 //隱藏列返回 107 if (!column.Visible) continue; 108 //所有顯示列的寬度 109 columnsWidth += column.Width; 110 } 111 112 //計算表頭高度 113 foreach (DataGridViewColumn column in dgv.Columns) 114 { 115 116 //列寬 117 int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth)); 118 //表頭高度 119 int temp = (int)e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Height + 2 * padding; 120 if (temp > headerHeight) headerHeight = temp; 121 } 122 123 //畫表頭 124 125 foreach (DataGridViewColumn column in dgv.Columns) 126 { 127 128 //隱藏列返回 129 if (!column.Visible) continue; 130 //列寬 131 int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth)); 132 //內容居中要加的寬度 133 float cenderWidth = (columnWidth - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Width) / 2; 134 if (cenderWidth < 0) cenderWidth = 0; 135 //內容居中要加的高度 136 float cenderHeight = (headerHeight + padding - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Height) / 2; 137 if (cenderHeight < 0) cenderHeight = 0; 138 //畫背景 139 e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(x, y, columnWidth, headerHeight)); 140 //畫邊框 141 e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x, y, columnWidth, headerHeight)); 142 ////畫上邊線 143 144 //e.Graphics.DrawLine(Pens.Black, x, y, x + columnWidth, y); 145 146 ////畫下邊線 147 148 //e.Graphics.DrawLine(Pens.Black, x, y + headerHeight, x + columnWidth, y + headerHeight); 149 150 ////畫右邊線 151 152 //e.Graphics.DrawLine(Pens.Black, x + columnWidth, y, x + columnWidth, y + headerHeight); 153 154 //if (x == e.MarginBounds.Left) 155 156 //{ 157 158 // //畫左邊線 159 160 // e.Graphics.DrawLine(Pens.Black, x, y, x, y + headerHeight); 161 162 //} 163 164 //畫內容 165 e.Graphics.DrawString(column.HeaderText, column.InheritedStyle.Font, new SolidBrush(column.InheritedStyle.ForeColor), new RectangleF(x + cenderWidth, y + cenderHeight, columnWidth, headerHeight)); 166 x += columnWidth; 167 168 } 169 170 x = e.MarginBounds.Left; 171 y += headerHeight; 172 while (rowIndex < dgv.Rows.Count) 173 { 174 175 DataGridViewRow row = dgv.Rows[rowIndex]; 176 if (row.Visible) 177 { 178 179 int rowHeight = 0; 180 foreach (DataGridViewCell cell in row.Cells) 181 { 182 183 DataGridViewColumn column = dgv.Columns[cell.ColumnIndex]; 184 if (!column.Visible || cell.Value == null) continue; 185 int tmpWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth)); 186 int temp = (int)e.Graphics.MeasureString(cell.Value.ToString(), column.InheritedStyle.Font, tmpWidth).Height + 2 * padding; 187 if (temp > rowHeight) rowHeight = temp; 188 } 189 190 foreach (DataGridViewCell cell in row.Cells) 191 { 192 193 DataGridViewColumn column = dgv.Columns[cell.ColumnIndex]; 194 if (!column.Visible) continue; 195 int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth)); 196 e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x, y, columnWidth, rowHeight)); 197 198 if (cell.Value != null) 199 { 200 201 //內容居中要加的寬度 202 203 float cenderWidth = (columnWidth - e.Graphics.MeasureString(cell.Value.ToString(), cell.InheritedStyle.Font, columnWidth).Width) / 2; 204 205 if (cenderWidth < 0) cenderWidth = 0; 206 207 //內容居中要加的高度 208 209 float cenderHeight = (rowHeight + padding - e.Graphics.MeasureString(cell.Value.ToString(), cell.InheritedStyle.Font, columnWidth).Height) / 2; 210 211 if (cenderHeight < 0) cenderHeight = 0; 212 213 ////畫下邊線 214 215 //e.Graphics.DrawLine(Pens.Black, x, y + rowHeight, x + columnWidth, y + rowHeight); 216 217 ////畫右邊線 218 219 //e.Graphics.DrawLine(Pens.Black, x + columnWidth, y, x + columnWidth, y + rowHeight); 220 221 //if (x == e.MarginBounds.Left) 222 223 //{ 224 225 // //畫左邊線 226 227 // e.Graphics.DrawLine(Pens.Black, x, y, x, y + rowHeight); 228 229 //} 230 231 //畫內容 232 233 e.Graphics.DrawString(cell.Value.ToString(), column.InheritedStyle.Font, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF(x + cenderWidth, y + cenderHeight, columnWidth, rowHeight)); 234 235 } 236 237 x += columnWidth; 238 239 } 240 241 x = e.MarginBounds.Left; 242 243 y += rowHeight; 244 245 if (page == 1) rowsPerPage++; 246 247 //打印下一頁 248 249 if (y + rowHeight > e.MarginBounds.Bottom) 250 { 251 252 e.HasMorePages = true; 253 254 break; 255 256 } 257 258 } 259 260 rowIndex++; 261 262 } 263 264 //頁腳 265 string footer = " 第 " + page + " 頁,共 " + Math.Ceiling(((double)dgv.Rows.Count / rowsPerPage)).ToString() + " 頁"; 266 //畫頁腳 267 e.Graphics.DrawString(footer, dgv.Font, Brushes.Black, x + (pagerWidth - e.Graphics.MeasureString(footer, dgv.Font).Width) / 2, e.MarginBounds.Bottom); 268 page++; 269 270 } 271 272 273 } 274 275 } View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved