国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

winform打印和預(yù)覽

2019-11-17 02:42:28
字體:
供稿:網(wǎng)友

winform打印和預(yù)覽

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

二、附上調(diào)用代碼

三、提供源碼

  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 = ""; //標(biāo)題名稱        21         static string titleName2 = ""; //第二標(biāo)題名稱      22         static int rowIndex = 0;   //當(dāng)前行        23         static int page = 1; //當(dāng)前頁       24         static int rowsPerPage = 0;  //每頁顯示多少行 25         /// <summary> 26         /// 打印DataGridView 27         /// </summary> 28         /// <param name="dataGridView">要打印的DataGridView</param> 29         /// <param name="title">標(biāo)題</param> 30         /// <param name="title2">第二標(biāo)題,可以為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); //設(shè)置邊距              46                 ppvw.Document = printDoc;   //設(shè)置要打印的文檔                47                 ((Form)ppvw).WindowState = FormWindowState.Maximized; //最大化                48                 rowIndex = 0; //當(dāng)前行               49                 page = 1;  //當(dāng)前頁                              50                 printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件  51                 printDoc.EndPrint += new PrintEventHandler(printDoc_EndPrint); 52                 ppvw.Document.DefaultPageSettings.Landscape = true;    // 設(shè)置打印為橫向                53                 ppvw.ShowDialog(); //打開預(yù)覽 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; //當(dāng)前行           66             page = 1;  //當(dāng)前頁             67             rowsPerPage = 0;//每頁顯示多少行 68         } 69         private static void printDoc_PrintPage(object sender, PrintPageEventArgs e) 70         { 71  72             //標(biāo)題字體 73             Font titleFont = new Font("微軟雅黑", 16, FontStyle.Bold); 74             //標(biāo)題尺寸 75             SizeF titleSize = e.Graphics.MeasureString(titleName, titleFont, e.MarginBounds.Width); 76             //x坐標(biāo) 77             int x = e.MarginBounds.Left; 78             //y坐標(biāo) 79             int y = Convert.ToInt32(e.MarginBounds.Top - titleSize.Height); 80             //邊距以內(nèi)紙張寬度 81             int pagerWidth = e.MarginBounds.Width; 82             //畫標(biāo)題 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                 //畫第二標(biāo)題 89                 e.Graphics.DrawString(titleName2, dgv.Font, Brushes.Black, x + (pagerWidth - titleSize.Width) / 2 + 200, y); 90                 //第二標(biāo)題尺寸 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             //縱軸上 內(nèi)容與線的距離 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                 //內(nèi)容居中要加的寬度133                 float cenderWidth = (columnWidth - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Width) / 2;134                 if (cenderWidth < 0) cenderWidth = 0;135                 //內(nèi)容居中要加的高度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                 //畫內(nèi)容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
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 福建省| 阳泉市| 石景山区| 南郑县| 黄石市| 大名县| 罗甸县| 贵溪市| 金平| 大埔县| 玛多县| 哈巴河县| 都江堰市| 天水市| 高密市| 永城市| 图木舒克市| 同江市| 濮阳县| 钦州市| 清镇市| 砀山县| 安达市| 平定县| 大关县| 宁国市| 天峻县| 七台河市| 巴南区| 福建省| 焉耆| 新干县| 阜新| 长兴县| 汝城县| 蒲城县| 沁水县| 两当县| 罗山县| 日土县| 峡江县|