heting

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            40 隨筆 :: 9 文章 :: 45 評論 :: 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    /// 創建人 賀挺
           12    /// </summary>

           13    public class Printer
           14    {
           15        private DataGridView dataview;
           16        private PrintDocument printDoc;
           17        //打印有效區域的寬度
           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        //打印頁數
           26        int PageNumber;
           27        //當前打印頁的行數
           28        int pageSize = 20;
           29        //當前打印的頁碼
           30        int PageIndex;
           31
           32        private int PageWidth; //打印紙的寬度
           33        private int PageHeight; //打印紙的高度
           34        private int LeftMargin; //有效打印區距離打印紙的左邊大小
           35        private int TopMargin;//有效打印區距離打印紙的上面大小
           36        private int RightMargin;//有效打印區距離打印紙的右邊大小
           37        private int BottomMargin;//有效打印區距離打印紙的下邊大小
           38         
           39        int rows;
           40        
           41        /// <summary>
           42        /// 構造函數
           43        /// </summary>
           44        /// <param name="dataview">要打印的DateGridView</param>
           45        /// <param name="printDoc">PrintDocument用于獲取打印機的設置</param>

           46        public Printer(DataGridView dataview, PrintDocument printDoc)
           47        {
           48            this.dataview = dataview;
           49            this.printDoc = printDoc;
           50            PageIndex = 0;
           51            //獲取打印數據的具體行數
           52            this.rows = dataview.RowCount;
           53
           54            this.columns = dataview.ColumnCount;
           55            //判斷打印設置是否是橫向打印
           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        //畫表格
          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            //計算出列的總寬度和打印紙比率
          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        //打印行數據
          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方法中調用
          251        /// </summary>
          252        /// <param name="g">傳入PrintPage中PrintPageEventArgs中的Graphics</param>
          253        /// <returns>是否還有打印頁 有返回true,無則返回false</returns>

          254        public bool Print(Graphics g)
          255        {
          256            InitPrint();
          257            DrawTable(g);
          258            DrawHeader(g);
          259            DrawRows(g);
          260
          261            //打印頁碼
          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            //打印查詢的功能項名稱
          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 賀挺 閱讀(8505) 評論(8)  編輯  收藏 所屬分類: c#范例

          評論

          # re: 自己寫的一個c#winform打印類 2009-09-20 11:41 羅萊家紡
          是打開附件是的看見飛機快斯蒂芬  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類 2009-09-27 15:08 周志勇
          兄弟能不能寫一個應用實例,這樣有學習的榜樣與例子,我的QQ879947263  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類 2010-01-07 20:47 12
          你好我些問題要問你!!  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類 2010-01-07 20:48 12
          你好
          我需要你的幫助!!
          494961597  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類[未登錄] 2010-02-02 22:37 KK
          大哥,一復制都是行號,這樣很累的的呀  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類 2010-02-25 16:08 43
          全是行號,沒法看代碼  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類 2010-04-27 17:54 roro
          你敢不敢不顯示行號  回復  更多評論
            

          # re: 自己寫的一個c#winform打印類[未登錄] 2011-01-04 17:12 匿名
          調用你的方法但是 到這一行就顯示了“未將對象引用到實例化的錯誤” 。
          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  回復  更多評論
            

          主站蜘蛛池模板: 喜德县| 紫云| 延川县| 容城县| 耒阳市| 乐至县| 昌图县| 昌邑市| 武夷山市| 株洲市| 尉氏县| 开封市| 包头市| 温州市| 城固县| 汕头市| 五大连池市| 西丰县| 错那县| 鄂托克前旗| 怀柔区| 兰考县| 武功县| 濉溪县| 安龙县| 湟中县| 乌鲁木齐县| 楚雄市| 波密县| 昌吉市| 临城县| 枣强县| 利辛县| 东海县| 五原县| 夏河县| 桂林市| 双城市| 巴林左旗| 鄂温| 丹寨县|