回 帖 发 新 帖 刷新版面

主题:[讨论]一个打印的问题!-----非常急!!!!

我是用winform的datagrid做的.但就是打印范围和跳页存在问题?
try//9个画图地方
            {
                int pHeight = 0;
                int Pwidth = 10; 
                
                
                Font headFont = new System.Drawing.Font("宋体",14,System.Drawing.FontStyle.Bold);//表开头
                Font fontTitle = new System.Drawing.Font("宋体",11);//样式表,统计数
                Font font = new System.Drawing.Font("宋体",9);//其它字体,如内容等

                //当打印第一页时
                pHeight += this.pageMargins.Top;        //高,边距

                //从第几页开始
                if(this.printdialog.PrinterSettings.FromPage!=0)
                {
                    pageNum=this.printdialog.PrinterSettings.FromPage;
                }

                if(this.pageNum == 0)                    //如果是第一页
                {
                    _rowNo = 0;                            //行号,第一行
                    //得到当前数据每一列的最宽值
                    colMaxWidth = GetMaxWidth(e.Graphics,font);
                    //表开头                    (页宽-上个页面传过来的标题长度)/2
                    float titleCenter = (this.pageWidth - e.Graphics.MeasureString(_tdName,headFont).Width)/2;//页面中间位置
                    //画图
                    e.Graphics.DrawString(_tdName,headFont,Brushes.Black,titleCenter,pHeight);//画图(字符串,样式,颜色,起始位置x,起始位置y)
                    
                    pHeight += (int)(e.Graphics.MeasureString(_tdName,headFont).Height) + 10;//算出字符串高度还+10:为下行的y轴位置

回复列表 (共2个回复)

沙发

                    int tilteHeight = (int)fontTitle.GetHeight();//得到统计数样式高度
                    //打印统计数
                    int col = -1;
                    int titlePos = (int)this.pageWidth/2;//页一半宽
                    for(int j=0;j< _htTitle.Length;j++)    //数据统计字符串(有几个统计值)
                    {
                        col++;
                        string printTitle = _htTitle[j];//第j个值
                        float titleWidth = e.Graphics.MeasureString(printTitle,fontTitle).Height;//算出它的高度

                        //如果打了两列,换行打
                        if(col == 2)
                        {
                            col = 0;
                            pHeight += (int)tilteHeight;//有2列,就换行,就要再加与y轴高度
                        }

                        if(col == 0)
                        {    //画图    0列                                (字符串,样式,颜色,起始位置x,起始位置y)
                            e.Graphics.DrawString(printTitle,fontTitle,Brushes.Black,this.pageMargins.Left,pHeight);
                        }
                        else
                        {    //画图    1列                                (字符串,样式,颜色,页面一半宽度(起始位置x),起始位置y)
                            e.Graphics.DrawString(printTitle,fontTitle,Brushes.Black,titlePos,pHeight);
                        }    
                    }//for
                    pHeight += (int)tilteHeight;//高度再加一次,表示换到下一行
                }//if(pageNum==0)

                if(_rowNo==0&&pageNum!=0)        //--------------------------------------算出第一页高度,并不打印出来(主要为了每页记录固定)
                {
                    int empheight=this.pageMargins.Top;
                    empheight+=(int)headFont.GetHeight()+10;
                    
                    if(_htTitle.Length%2==0)//统计记录
                    {
                        empheight+=((int)fontTitle.GetHeight()+10)*_htTitle.Length/2;
                    }
                    else
                    {
                        empheight+=((int)fontTitle.GetHeight()+10)*(_htTitle.Length/2+1);
                    }
                    pHeight=empheight;//
                }

板凳

e.Graphics.DrawLine(new Pen(Brushes.Black),    new System.Drawing.Point(this.pageMargins.Left,pHeight),
                    new System.Drawing.Point(this.pageWidth - this.pageMargins.Right,pHeight));
                pHeight += 8;//换行,加8象素,准备输出字符串

                //--------------------------------------
                //--------------------------------------
                //标题
                
                int fontHeight = (int)font.GetHeight(e.Graphics);

                colMaxWidth = GetMaxWidth(e.Graphics,font);

                Pwidth = this.pageMargins.Left;//页面左边距
                for(int i=0;i<_strHead.GetLength(0);i++)//共有几个字段名
                {
                    if(Pwidth+(int)colMaxWidth[i] > this.pageWidth - this.pageMargins.Right)
                    {
                        break;
                    }
                    string colName = _strHead[i,0];//得到列名
                    string dataType = _dvSource.Table.Columns[i].DataType.ToString();
                    float fCap = 0;
                    string strTemp = GetFixedString(_strHead[i,1],font,colMaxWidth[i],e.Graphics);
                    //画图        (字符串,样式,颜色,起始位置x,起始位置y)
                    e.Graphics.DrawString(strTemp,font,Brushes.Black,Pwidth + fCap,pHeight);
                    Pwidth += (int)colMaxWidth[i];//起始位置x
                }
                
                pHeight += 8;//换行,加8象素,准备输出字符串

                //-------------------------------------------
                //-------------------------------------------

                //标题线
                pHeight += fontHeight;//页面y的高度

                //画直线        (定义画直线,定义起始点(x,y),定义终点(x,y))
                e.Graphics.DrawLine(new Pen(Brushes.Black),
                    new System.Drawing.Point(this.pageMargins.Left,pHeight),
                    new System.Drawing.Point(this.pageWidth - this.pageMargins.Right,pHeight));
                
                pHeight += 8;//换行,加8象素,准备输出字符串

                //---------------------------------------
                //---------------------------------------

                //页面高度-底边距-画图(直线高度)
                float footHeight = this.pageHeight - this.pageMargins.Bottom - fontHeight;

                //换页
                if((pageNum!=0)&&(this.printdialog.PrinterSettings.FromPage!=0))
                {
//                    pHeight=pHeight+printdocu.DefaultPageSettings.Bounds.Height*this.printdialog.PrinterSettings.FromPage;
//                    footHeight=footHeight+printdocu.DefaultPageSettings.Bounds.Height*this.printdialog.PrinterSettings.FromPage;
                    //_rowNo=48;
                    
                    int a=pHeight;
                    while(pHeight < footHeight)
                    {
                        pHeight += fontHeight;//页面高度累加y
                        _rowNo++;
                    }
                    pHeight=a;
                    //_rowNo=48;
                }

我来回复

您尚未登录,请登录后再回复。点此登录或注册