<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    heting

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      40 隨筆 :: 9 文章 :: 45 評(píng)論 :: 0 Trackbacks
      1using System;
      2using System.Collections.Generic;
      3using System.Text;
      4using System.Windows.Forms;
      5using System.Drawing.Printing;
      6using System.Drawing;
      7
      8namespace Etaocn.Util
      9{
     10    /// <summary>
     11    /// 創(chuàng)建人 賀挺
     12    /// </summary>

     13    public class Printer
     14    {
     15        private DataGridView dataview;
     16        private PrintDocument printDoc;
     17        //打印有效區(qū)域的寬度
     18        int width;
     19        int height;
     20        int columns;
     21        double Rate;
     22        bool hasMorePage = false;
     23        int currRow = 0;
     24        int rowHeight = 20;
     25        //打印頁(yè)數(shù)
     26        int PageNumber;
     27        //當(dāng)前打印頁(yè)的行數(shù)
     28        int pageSize = 20;
     29        //當(dāng)前打印的頁(yè)碼
     30        int PageIndex;
     31
     32        private int PageWidth; //打印紙的寬度
     33        private int PageHeight; //打印紙的高度
     34        private int LeftMargin; //有效打印區(qū)距離打印紙的左邊大小
     35        private int TopMargin;//有效打印區(qū)距離打印紙的上面大小
     36        private int RightMargin;//有效打印區(qū)距離打印紙的右邊大小
     37        private int BottomMargin;//有效打印區(qū)距離打印紙的下邊大小
     38         
     39        int rows;
     40        
     41        /// <summary>
     42        /// 構(gòu)造函數(shù)
     43        /// </summary>
     44        /// <param name="dataview">要打印的DateGridView</param>
     45        /// <param name="printDoc">PrintDocument用于獲取打印機(jī)的設(shè)置</param>

     46        public Printer(DataGridView dataview, PrintDocument printDoc)
     47        {
     48            this.dataview = dataview;
     49            this.printDoc = printDoc;
     50            PageIndex = 0;
     51            //獲取打印數(shù)據(jù)的具體行數(shù)
     52            this.rows = dataview.RowCount;
     53
     54            this.columns = dataview.ColumnCount;
     55            //判斷打印設(shè)置是否是橫向打印
     56            if (!printDoc.DefaultPageSettings.Landscape)
     57            {
     58
     59                PageWidth = printDoc.DefaultPageSettings.PaperSize.Width;
     60                PageHeight = printDoc.DefaultPageSettings.PaperSize.Height;
     61
     62            }

     63            else
     64            {
     65
     66                PageHeight = printDoc.DefaultPageSettings.PaperSize.Width;
     67                PageWidth = printDoc.DefaultPageSettings.PaperSize.Height;
     68
     69            }

     70            LeftMargin = printDoc.DefaultPageSettings.Margins.Left;
     71            TopMargin = printDoc.DefaultPageSettings.Margins.Top;
     72            RightMargin = printDoc.DefaultPageSettings.Margins.Right;
     73            BottomMargin = printDoc.DefaultPageSettings.Margins.Bottom;
     74
     75
     76            height = PageHeight - TopMargin - BottomMargin - 2;
     77            width = PageWidth - LeftMargin - RightMargin - 2;
     78
     79            double tempheight = height;
     80            double temprowHeight = rowHeight;
     81            while (true)
     82            {
     83                string temp = Convert.ToString(tempheight / Math.Round(temprowHeight, 3));
     84                int i = temp.IndexOf('.');
     85                double tt = 100;
     86                if (i != -1)
     87                {
     88                    tt = Math.Round(Convert.ToDouble(temp.Substring(temp.IndexOf('.'))), 3);
     89                }

     90                if (tt <= 0.01)
     91                {
     92                    rowHeight = Convert.ToInt32(temprowHeight);
     93                    break;
     94                }

     95                else
     96                {
     97                    temprowHeight = temprowHeight + 0.01;
     98
     99                }

    100            }

    101            pageSize = height / rowHeight;
    102            if ((rows + 1<= pageSize)
    103            {
    104                pageSize = rows + 1;
    105                PageNumber = 1;
    106            }

    107            else
    108            {
    109                PageNumber = rows / (pageSize - 1);
    110                if (rows % (pageSize - 1!= 0)
    111                {
    112                    PageNumber = PageNumber + 1;
    113                }

    114
    115            }

    116
    117
    118
    119        }

    120
    121
    122
    123        /// <summary>
    124        /// 初始化打印
    125        /// </summary>

    126        private void InitPrint()
    127        {
    128            PageIndex = PageIndex + 1;
    129            if (PageIndex == PageNumber)
    130            {
    131                hasMorePage = false;
    132                if (PageIndex != 1)
    133                {
    134                    pageSize = rows % (pageSize - 1+ 1;
    135                }

    136            }

    137            else
    138            {
    139                hasMorePage = true;
    140            }

    141
    142
    143
    144        }

    145        //打印頭
    146        private void DrawHeader(Graphics g)
    147        {
    148
    149            Font font = new Font("宋體"12, FontStyle.Bold);
    150            int temptop = (rowHeight / 2+ TopMargin + 1;
    151            int templeft = LeftMargin + 1;
    152
    153            for (int i = 0; i < this.columns; i++)
    154            {
    155                string headString = this.dataview.Columns[i].HeaderText;
    156                float fontHeight = g.MeasureString(headString, font).Height;
    157                float fontwidth = g.MeasureString(headString, font).Width;
    158                float temp = temptop - (fontHeight) / 3;
    159                g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));
    160                templeft = templeft + (int)(this.dataview.Columns[i].Width / Rate) + 1;
    161            }

    162
    163        }

    164        //畫(huà)表格
    165        private void DrawTable(Graphics g)
    166        {
    167
    168            Rectangle border = new Rectangle(LeftMargin, TopMargin, width, (pageSize) * rowHeight);
    169            g.DrawRectangle(new Pen(Brushes.Black, 2), border);
    170            for (int i = 1; i < pageSize; i++)
    171            {
    172                if (i != 1)
    173                {
    174                    g.DrawLine(new Pen(Brushes.Black, 1), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));
    175                }

    176                else
    177                {
    178                    g.DrawLine(new Pen(Brushes.Black, 2), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));
    179                }

    180            }

    181
    182            //計(jì)算出列的總寬度和打印紙比率
    183            Rate = Convert.ToDouble(GetDateViewWidth()) / Convert.ToDouble(width);
    184            int tempLeft = LeftMargin + 1;
    185            int endY = (pageSize) * rowHeight + TopMargin;
    186            for (int i = 1; i < columns; i++)
    187            {
    188                tempLeft = tempLeft + 1 + (int)(this.dataview.Columns[i - 1].Width / Rate);
    189                g.DrawLine(new Pen(Brushes.Black, 1), new Point(tempLeft, TopMargin), new Point(tempLeft, endY));
    190            }

    191
    192        }

    193         /// <summary>
    194         /// 獲取打印的列的總寬度
    195         /// </summary>
    196         /// <returns></returns>

    197        private int GetDateViewWidth()
    198        {
    199            int total = 0;
    200            for (int i = 0; i < this.columns; i++)
    201            {
    202                total = total + this.dataview.Columns[i].Width;
    203            }

    204            return total;
    205        }

    206
    207        //打印行數(shù)據(jù)
    208        private void DrawRows(Graphics g)
    209        {
    210
    211            Font font = new Font("宋體"12, FontStyle.Regular);
    212            int temptop = (rowHeight / 2+ TopMargin + 1 + rowHeight;
    213
    214
    215            for (int i = currRow; i < pageSize + currRow - 1; i++)
    216            {
    217                int templeft = LeftMargin + 1;
    218                for (int j = 0; j < columns; j++)
    219                {
    220                    string headString = this.dataview.Rows[i].Cells[j].Value.ToString();
    221                    float fontHeight = g.MeasureString(headString, font).Height;
    222                    float fontwidth = g.MeasureString(headString, font).Width;
    223                    float temp = temptop - (fontHeight) / 3;
    224                    while (true)
    225                    {
    226                        if (fontwidth <= (int)(this.dataview.Columns[j].Width / Rate))
    227                        {
    228                            break;
    229                        }

    230                        else
    231                        {
    232                            headString = headString.Substring(0, headString.Length - 1);
    233                            fontwidth = g.MeasureString(headString, font).Width;
    234                        }

    235                    }

    236                    g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));
    237
    238                    templeft = templeft + (int)(this.dataview.Columns[j].Width / Rate) + 1;
    239                }

    240
    241                temptop = temptop + rowHeight;
    242
    243
    244            }

    245            currRow = pageSize + currRow - 1;
    246
    247        }

    248
    249        /// <summary>
    250        /// 在PrintDocument中的PrintPage方法中調(diào)用
    251        /// </summary>
    252        /// <param name="g">傳入PrintPage中PrintPageEventArgs中的Graphics</param>
    253        /// <returns>是否還有打印頁(yè) 有返回true,無(wú)則返回false</returns>

    254        public bool Print(Graphics g)
    255        {
    256            InitPrint();
    257            DrawTable(g);
    258            DrawHeader(g);
    259            DrawRows(g);
    260
    261            //打印頁(yè)碼
    262            string pagestr = PageIndex + " / " + PageNumber;
    263            Font font = new Font("宋體"12, FontStyle.Regular);
    264            g.DrawString(pagestr, font, Brushes.Black, new PointF((PageWidth / 2- g.MeasureString(pagestr, font).Width, PageHeight - (BottomMargin / 2- g.MeasureString(pagestr, font).Height));
    265            //打印查詢的功能項(xiàng)名稱
    266            string temp = dataview.Tag.ToString() + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
    267            g.DrawString(temp, font, Brushes.Black, new PointF(PageWidth - 5 - g.MeasureString(temp, font).Width, PageHeight - 5 - g.MeasureString(temp, font).Height));
    268            return hasMorePage;
    269        }

    270
    271
    272
    273
    274
    275
    276    }

    277}

    278
    posted on 2009-09-19 09:46 賀挺 閱讀(8502) 評(píng)論(8)  編輯  收藏 所屬分類: c#范例

    評(píng)論

    # re: 自己寫(xiě)的一個(gè)c#winform打印類 2009-09-20 11:41 羅萊家紡
    是打開(kāi)附件是的看見(jiàn)飛機(jī)快斯蒂芬  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類 2009-09-27 15:08 周志勇
    兄弟能不能寫(xiě)一個(gè)應(yīng)用實(shí)例,這樣有學(xué)習(xí)的榜樣與例子,我的QQ879947263  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類 2010-01-07 20:47 12
    你好我些問(wèn)題要問(wèn)你!!  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類 2010-01-07 20:48 12
    你好
    我需要你的幫助!!
    494961597  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類[未登錄](méi) 2010-02-02 22:37 KK
    大哥,一復(fù)制都是行號(hào),這樣很累的的呀  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類 2010-02-25 16:08 43
    全是行號(hào),沒(méi)法看代碼  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類 2010-04-27 17:54 roro
    你敢不敢不顯示行號(hào)  回復(fù)  更多評(píng)論
      

    # re: 自己寫(xiě)的一個(gè)c#winform打印類[未登錄](méi) 2011-01-04 17:12 匿名
    調(diào)用你的方法但是 到這一行就顯示了“未將對(duì)象引用到實(shí)例化的錯(cuò)誤” 。
    string temp = dataview.Tag.ToString() + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
    267 g.DrawString(temp, font, Brushes.Black, new PointF(PageWidth - 5 - g.MeasureString(temp, font).Width, PageHeight - 5 - g.MeasureString(temp, font).Height));“


    dataview.Tag為null  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 日本三级在线观看免费| 色婷婷精品免费视频| 伊人久久亚洲综合影院首页| 亚洲人成网站免费播放| 猫咪免费观看人成网站在线| 国产一级高青免费| 18禁美女裸体免费网站| 成年女人喷潮毛片免费播放| 男人的天堂亚洲一区二区三区| 四虎永久在线精品视频免费观看| 国产午夜亚洲不卡| 亚洲免费在线播放| 亚洲欧美一区二区三区日产| 一个人看的hd免费视频| 日本免费一区二区三区| 日韩视频免费在线| 亚洲中文字幕无码一区| 亚洲视频一区在线| 偷自拍亚洲视频在线观看| 免费网站看av片| 毛片免费在线播放| 亚洲综合精品香蕉久久网| 亚洲激情电影在线| 免费观看四虎精品成人| 97av免费视频| 成人亚洲综合天堂| 99人中文字幕亚洲区 | 久久亚洲色一区二区三区| 337p日本欧洲亚洲大胆艺术| 亚洲AV网一区二区三区| 九九美女网站免费| 日本高清免费网站| 亚洲精品在线观看视频| 成人亚洲国产精品久久| 亚洲免费网站在线观看| 亚洲人成网站观看在线播放| 亚洲中文字幕在线无码一区二区 | 久久国产一片免费观看| 大地资源二在线观看免费高清| 青青草原亚洲视频| 亚洲狠狠婷婷综合久久|