• 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
  • 相关阅读:
    VC++中用API调用对话框资源
    《游民文化与中国社会》:类似于《潜规则》从书面的文字中发现背后的规律,偏学术
    转贴:正确认识淋巴瘤
    《知乎周刊·乔布斯往事》:作为书来看内容还有点少并且水平一般
    《我读书少,你可别骗我》;四个搞笑短篇,比较好玩
    《终极问题2.0》:客户满意度的一个非常好的判断方法
    《改革的逻辑》:专家点谈产权制度、土地制度和货币制度,有不少深刻的见解
    《新工业革命》:过于纠缠细节,主题不是很清晰
    《总要有人说出真相》:业余研究者,可读性比较差
    《我在华为的日子》:华为程序员的流水账,可读性比较差
  • 原文地址:https://www.cnblogs.com/codeding/p/4168030.html
Copyright © 2020-2023  润新知