| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2613 人关注过本帖
标题:请问如何用GDI+将其画成表格,并将文字居中
只看楼主 加入收藏
lukebc
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:74
专家分:32
注 册:2009-8-18
结帖率:71.43%
收藏
 问题点数:0 回复次数:0 
请问如何用GDI+将其画成表格,并将文字居中
Graphics g = e.Graphics;
            int linesPerPage = 2; //页面的行号
            float yPosition = 0;   //绘制字符串的纵向位置
            int count = 0; //行计数器
            float leftMargin = e.MarginBounds.Left; //左边距
            float topMargin = e.MarginBounds.Top; //上边距
            Font printFont = new Font("宋体", 10);
            SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
            Graphics jx=this.CreateGraphics();
            Pen mypen = new Pen(Color.Black, 3);
            
            
            while (count < linesPerPage && datagridviewRowIndex < dataGridView1.Rows.Count)
            {
                string s1 = string.Empty, s2 = string.Empty, s3 = string.Empty, s4 = string.Empty, s5 = string.Empty;
                string s6 = string.Empty, s7 = string.Empty;//你有几个列这里就应该对应几个字段
                if (count.Equals(0))
                {
                    //这一部分是每页的列头和大标题
                    s1 = "物资类别";
                    s2 = "编号";
                    s3 = "入库编号";
                    s4 = "日期";
                    s5 = "材料名称";
                    s6 = "型号规格";
                    s7 = "数量";



                    //...
                    yPosition = topMargin + 5;
                    g.DrawString("入库信息", printFont, myBrush, leftMargin + 300, yPosition, new StringFormat());//打印大标题
                }
                else
                {
                    //这部分是每一页的内容
                    s1 = dataGridView1.Rows[datagridviewRowIndex].Cells[1].Value.ToString();
                    s2 = dataGridView1.Rows[datagridviewRowIndex].Cells[2].Value.ToString();
                    s3 = dataGridView1.Rows[datagridviewRowIndex].Cells[3].Value.ToString();
                    s4 = dataGridView1.Rows[datagridviewRowIndex].Cells[4].Value.ToString();
                    s5 = dataGridView1.Rows[datagridviewRowIndex].Cells[5].Value.ToString();
                    s6 = dataGridView1.Rows[datagridviewRowIndex].Cells[6].Value.ToString();
                    s7 = dataGridView1.Rows[datagridviewRowIndex].Cells[7].Value.ToString();
                    datagridviewRowIndex++;
                }
                yPosition = topMargin + (count * printFont.GetHeight(g)) + 50;
                //下面是打印数据或者列头
                g.DrawString(s1, printFont, myBrush, leftMargin +70, yPosition, new StringFormat());
                g.DrawString(s2, printFont, myBrush, leftMargin + 140, yPosition, new StringFormat());
                g.DrawString(s3, printFont, myBrush, leftMargin + 210, yPosition, new StringFormat());
                g.DrawString(s4, printFont, myBrush, leftMargin + 280, yPosition, new StringFormat());
                g.DrawString(s5, printFont, myBrush, leftMargin + 350, yPosition, new StringFormat());
                g.DrawString(s6, printFont, myBrush, leftMargin + 420, yPosition, new StringFormat());
                g.DrawString(s7, printFont, myBrush, leftMargin + 490, yPosition, new StringFormat());
                count++;
            }
            if (datagridviewRowIndex < dataGridView1.Rows.Count)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
搜索更多相关主题的帖子: 如何 count 字符串 计数器 
2016-09-25 14:27
快速回复:请问如何用GDI+将其画成表格,并将文字居中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016146 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved