wmhysu

   :: 首页  :: 新随笔  :: 联系 ::  :: 管理

using System;
using System.Drawing;
using GwmPrinter.Object;

namespace GwmPrinter
{
 /// <summary>
 /// 定义了绘制报表各部分的方法,提供绘制整个报表的唯一方法
 /// </summary>
 public class DrawingMethod
 {
  private Report _report;
  private int _CurrentPage = 1;                                 //当前打印页码,即当前是第几页
  private System.Drawing.Printing.PrintDocument _PrintDocument;
  private Rectangle _rectangle;                        //绘制区
     private Rectangle _recBody;
  private Rectangle _recTableRecord;
  private Rectangle _recTableRowRecord;
  private int CurrentRowNum = 0;
  private int CurrentTableNum = 0;                              //用来协助控制CurrentRowNum管理不同表格
  private int PrePageLastRowNum = 0;                            //上一页最后的行数;
        private bool ev_HasMorePages;
  private Point _point;                                      
  private bool IsShowPageMsg;                                  //是否显示分页信息
  private Rectangle recCurrentTable;                           //当前表格的矩形,为分页绘制表格边框准备
  private bool IsMarginEveryPage;                              //是否每页都保持边距,默认false;如是则上下无边距
  private Font font;
  private string _PrinterName;
  private int CellMargin;                                      //字和边框的距离。紧临会出现字体显示不清楚
  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="report">包含报表各部分的一个Report对象</param>
  public DrawingMethod(Report report)
  {
   _PrintDocument = new System.Drawing.Printing.PrintDocument();
   this._report = report;
   _CurrentPage = 1;
   _rectangle = new Rectangle(0,0,0,0);                        //绘制区
   _recBody = _rectangle;
   _recTableRecord = _rectangle;
   _recTableRowRecord = _rectangle;
   CurrentRowNum = 0;
   CurrentTableNum = 0;
   ev_HasMorePages = false;
   this.IsShowPageMsg = report.IsShowPageMsg;
   this.IsMarginEveryPage = report.IsMarginEveryPage;
   this.font = report.Font; 
   if ( System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count>0 )
    this._PrinterName = System.Drawing.Printing.PrinterSettings.InstalledPrinters[0].ToString();
   else
    _PrinterName = null;
   this.CellMargin = report.CellMargin;
  }

  /// <summary>
  /// 绘制Body上方的内容
  /// </summary>
  /// <param name="_graphics">绘制区域</param>
  /// <param name="_rectangle">绘制的区域矩阵</param>
  /// <param name="ev">事件的提供数据</param>
  private void DrawBorderAbove(Graphics _graphics,ref Rectangle _rectangle,System.Drawing.Printing.PrintPageEventArgs ev)
  {
   _rectangle.X =_report.Margins.Left;
   _rectangle.Width -= (_report.Margins.Right+_report.Margins.Left);
   if (_report.IsAlwaysShowAbove || this.CurrentPage == 1 ||this.IsMarginEveryPage )
   {
    _rectangle.Y = _report.Margins.Top;
    _rectangle.Height -= _rectangle.Y;
    _rectangle.Height -= _report.Margins.Bottom;
       
    if ( _report.Title!=null )
     if ( this.CurrentPage == 1|| _report.Title.IsDisPlayAlways )
      DrawGridTitle(_graphics,ref _rectangle,_report.Title);
    if ( _report.Caption!=null )
     if ( this.CurrentPage == 1|| _report.Caption.IsDisPlayAlways )
      DrawGridCaption(_graphics,ref _rectangle,_report.Caption);
    if (  _report.Top!=null )
     if ( this.CurrentPage == 1|| _report.Top.IsDisPlayAlways )
      DrawGridTop(_graphics,ref _rectangle,_report.Top);
    if ( _report.Header!=null )
     if ( this.CurrentPage == 1 || _report.Header.IsDisPlayAlways )
      DrawGridHeader(_graphics,ref _rectangle,_report.Header);
   }   
   else
   {
    _rectangle.Y = 2;
    _rectangle.Height -= _rectangle.Y;
   }
   if ( _report.Footer!=null )
   
    if ( _report.Footer.IsDisPlayAlways )
    {
     GwmPrinter.Object.TableRow trFooter = _report.Footer.Table.GetObject(0);
     _rectangle.Height -= (int)((_graphics.MeasureString(trFooter.Text[0],trFooter.Font).Height+CellMargin)*_report.Footer.Table.TableHeight);
    }   
  }
      
  #region //绘制实现  
  private void DrawReport(object obj,System.Drawing.Printing.PrintPageEventArgs ev)
  {
   ev_HasMorePages = false;
   ev.HasMorePages = false;
   Graphics g = ev.Graphics ;
   _rectangle = ev.PageBounds;
   DrawBorderAbove(g,ref _rectangle,ev);
   DrawGridBody(g,ref _rectangle,ev);
   if ( _report.Footer!=null )
   {
    if ( !_report.Footer.IsDisPlayAlways )
    {
     if ( ev_HasMorePages )
     {
      ev.HasMorePages = true;
      return;
     }
    }
    if ( this.IsShowPageMsg )
    {
                    string pageMsg = "--第(";
     if ( _report.Footer.IsDisPlayAlways )
     {
      if ( ev_HasMorePages ) 
      {
       int PAGE = this.CurrentPage - 1;
       pageMsg += PAGE.ToString()+")页(后续)--";
      }
      else pageMsg += this.CurrentPage.ToString()+")页(共"+this.CurrentPage.ToString()+"页)--";
     }     
     else pageMsg = "--(共"+this.CurrentPage.ToString()+"页)--";
     string str = this._report.Footer.Table.GetObject(this._report.Footer.Table.Count-1).Text[0];
     if ( this._report.Footer.Table.GetObject(this._report.Footer.Table.Count-1).Text.GetLength(0)==1 && (str.Trim().Length == 0 || ( str.IndexOf("页")!=-1 && str.IndexOf("--")!= -1 )))
       this._report.Footer.Table.GetObject(this._report.Footer.Table.Count-1).Text[0] = pageMsg;
     if ( !this.ev_HasMorePages && this.CurrentPage == 1 )
     {
      _report.Footer.IsShowPageMsg = false;
     }
    }
                Rectangle rectangle = _rectangle;
    DrawGridFooter(g,ref rectangle,_report.Footer,_report.Footer.IsHasBorder,_report.Footer.Color,ev);
   }
   g.Dispose();
  }
  #endregion

  #region 绘制各个报表的部门对象模型
  /// <summary>
  /// 绘制Title
  /// </summary>
  /// <param name="g">绘制表面</param>
  /// <param name="rec">当前可绘制的区域矩阵</param>
  /// <param name="title">Title对象</param>
  private void DrawGridTitle(Graphics g,ref Rectangle rec,Title title)
  {
   Rectangle recTitle = rec;
   recTitle.X = rec.X;
   recTitle.Y = rec.Y;
   recTitle.Width = rec.Width;
   recTitle.Height =  (int)g.MeasureString(title.Text,title.Font).Height+CellMargin;
   Brush brush = new SolidBrush(title.Color);
   g.DrawString(title.Text,title.Font,brush,recTitle,title.StringFormat);
   brush.Dispose();
   rec.Y += recTitle.Height;
   rec.Height -= recTitle.Height;
  }

  /// <summary>
  /// 绘制Caption
  /// </summary>
  /// <param name="g">绘制表面</param>
  /// <param name="rec">当前可绘制的区域矩阵</param>
  /// <param name="caption">Caption对象</param>
  private void DrawGridCaption(Graphics g,ref Rectangle rec,Caption caption)
  {
   Rectangle recCaption = rec;
   recCaption.X = rec.X;
   recCaption.Y = rec.Y;
   recCaption.Width = rec.Width;
   recCaption.Height =  (int)g.MeasureString(caption.Text,caption.Font).Height+CellMargin;
   Brush brush = new SolidBrush(caption.Color);
   g.DrawString(caption.Text,caption.Font,brush,recCaption,caption.StringFormat);
   rec.Y += recCaption.Height;
   rec.Height -= recCaption.Height;
  }

  /// <summary>
  /// 绘制Top
  /// </summary>
  /// <param name="g">绘制表面</param>
  /// <param name="rec">当前可绘制的区域矩阵</param>
  /// <param name="top">Top对象</param>
  private void DrawGridTop(Graphics g,ref Rectangle rec,Top top)
  {
   int count = top.Count;
   for ( int i = 0;i<count;i++ )
    DrawGridTopRow(g,ref rec,top.GetObject(i));
  }

  /// <summary>
  /// 绘制TopRow
  /// </summary>
  /// <param name="g">绘制表面</param>
  /// <param name="rec">当前可绘制的区域矩阵</param>
  /// <param name="topRow">TopRow对象</param>
  private void DrawGridTopRow(Graphics g,ref Rectangle rec,TopRow topRow)
  {
   Rectangle recTopRow = rec;
   recTopRow.X = rec.X;
   recTopRow.Y = rec.Y;
   recTopRow.Height =  (int)(g.MeasureString(topRow.Text[0],topRow.Font).Height)+CellMargin;
   int intNum = topRow.Text.GetLength(0);
   Brush brush = new SolidBrush(topRow.Color);
   Pen pen = new Pen(brush,1.0f);
   for (int i = 0;i<intNum;i++)
   {
    recTopRow.Width = rec.Width*topRow.ColWidth[i]/topRow.ColWidth[intNum];
    g.DrawString(topRow.Text[i],topRow.Font,brush,recTopRow,topRow.StringFormat[i]);
    if ( topRow.IsHasGridLine )
    {
     //最后一个表格的话,就和表的边框重合 不然会有双线出现
     if ( i == intNum-1 )
                       recTopRow.Width += rec.Right - recTopRow.Right;
     g.DrawRectangle(pen,recTopRow);
    }
    recTopRow.X += recTopRow.Width;
    if ( System.Math.Abs ( recTopRow.X - rec.Right )<=5 )
    {
     recTopRow.X = rec.X;
     recTopRow.Width = rec.Width;
    }
   }
   if ( !topRow.IsHasGridLine )
    if ( topRow.IsHasBorder )
    {
     g.DrawRectangle(pen,recTopRow);
    }
   pen.Dispose();
   brush.Dispose();
   rec.Y += recTopRow.Height;
   rec.Height -= recTopRow.Height;
  }
  

  /// <summary>
  /// 绘制Header
  /// </summary>
  /// <param name="g">绘制Header</param>
  /// <param name="rec">当前可绘制的区域矩阵</param>
  /// <param name="header">Header对象</param>
  private void DrawGridHeader(Graphics g,ref Rectangle rec,Header header)
  {
      this._recBody = _rectangle;
   Rectangle recHeader = rec;
   recHeader.Width = rec.Width;
   recHeader.Height =  (int)((g.MeasureString(header.Table.GetObject(0).Text[0],header.Table.GetObject(0).Font).Height+CellMargin))*header.Table.TableHeight;
   DrawHeaderFooterTable(g,ref rec,ref this._recBody,recHeader,header.Table);
   rec.Y += recHeader.Height;
   rec.Height -= recHeader.Height;
   Brush brush = new SolidBrush(header.Color); 
   Pen pen = new Pen(brush);
   if ( header.IsHasBorder )
    g.DrawRectangle(pen,recHeader);
   pen.Dispose();
   brush.Dispose(); 
  }

  /// <summary>
  /// 绘制报表主体部分
  /// </summary>
  /// <param name="_graphics">绘制区域</param>
  /// <param name="_rectangle">绘制的区域矩阵</param>
  /// <param name="ev">打印的事件提供数据</param>
  private void DrawGridBody(Graphics g,ref Rectangle _rectangle,System.Drawing.Printing.PrintPageEventArgs ev)
  {
   int numTable = this._report.Body.ArrayList.Count;
   this._point = new Point(_rectangle.X,_rectangle.Y);
   _recBody = _rectangle;  
   _recTableRecord = _rectangle;
   _recTableRecord.Width = 0;
   _recTableRecord.Height = 0;
   Brush brush = new SolidBrush(Color.Black);
   //定义显示分页信息的字体用   
   for ( ;this.CurrentTableNum<numTable; CurrentTableNum++)
   { 
    Table table = this._report.Body.GetObject(CurrentTableNum);
    this.recCurrentTable.Height = (int)((g.MeasureString(this._report.Body.GetObject(CurrentTableNum).GetObject(0).Text[0],this._report.Body.GetObject(CurrentTableNum).GetObject(0).Font).Height+CellMargin)*this._report.Body.GetObject(CurrentTableNum).TableHeight);   
    if ( _recTableRecord.Width == 0 && this._report.Body.GetObject(CurrentTableNum).TableWidth != 1.0f )
    {
     if (  _rectangle.Bottom - _recBody.Y < this.recCurrentTable.Height )
     { 
      if ( this._report.Body.IsHasBorder )
      {
       Pen penBorder = new Pen(this._report.Body.Color,1.0f);
       g.DrawRectangle(penBorder,this._point.X,this._point.Y,_rectangle.Width,_recBody.Y-this._point.Y);
       penBorder.Dispose();      
      } 
      ev_HasMorePages = true;
      this.CurrentPage ++;
      return;
     }     
    }  
    Point pointTable = new Point(_recTableRecord.Right,_recTableRecord.Top);
    if ( DrawGridTable(g,ref _rectangle,ref _recBody,ref _recTableRecord,ref this.recCurrentTable,this._report.Body.GetObject(CurrentTableNum),this._report.Body.IsHasBorder,this._report.Body.Color,ev) )
    {
     //重新确定坐标,为绘制Footer做准备
     _rectangle.Y = this._recTableRowRecord.Y;  
     this.CurrentPage ++;
     return;
    }
    if ( this._report.Body.GetObject(CurrentTableNum).IsHasBorder )
    {
     Pen penBorder = new Pen(this._report.Body.GetObject(CurrentTableNum).Color,1.0f);
     g.DrawRectangle(penBorder,pointTable.X,pointTable.Y,_rectangle.Width,_recTableRowRecord.Y-this._point.Y);
     penBorder.Dispose();
    }

    //当前表格绘制结束,下一个表格从首行开始
    CurrentRowNum = 0;
   }
   if ( CurrentTableNum == numTable )
   {
    if ( this._report.Body.IsHasBorder )
    {
     Pen penBorder = new Pen(this._report.Body.Color,1.0f);
     g.DrawRectangle(penBorder,this._point.X,this._point.Y,_rectangle.Width,_recTableRowRecord.Y-this._point.Y);
     penBorder.Dispose();
    }
    _recBody.Y = _recTableRowRecord.Y;
    _recBody.Height = _rectangle.Bottom - _recTableRowRecord.Y;
    _recBody.Width = _rectangle.Width;
    _rectangle = _recBody;    
   }
  }

 

  private bool DrawGridTable(Graphics g,ref Rectangle rec,ref Rectangle recBody,ref Rectangle _recTableRecord,ref Rectangle recCurrentTable,Table _Table,bool BodyIsHasBorder,Color BodyBoderColor,System.Drawing.Printing.PrintPageEventArgs ev)
  {      
   
   //定义表格的绘制区域
   this.recCurrentTable = recBody;
   this.recCurrentTable.Width = (int)(rec.Width*_Table.TableWidth);
   if ( System.Math.Abs(this.recCurrentTable.Width - recBody.Width)<= 5 )
    this.recCurrentTable.Width = recBody.Width;
   if ( this.CurrentRowNum == 0 )
    this.recCurrentTable.Height = (int)((g.MeasureString(_Table.GetObject(0).Text[0],_Table.GetObject(0).Font).Height+CellMargin)*_Table.TableHeight);
   else
   {
    for ( int i = this.PrePageLastRowNum; i<this.CurrentRowNum;i++ )
     _Table.TableHeight -= _Table.GetObject(i).RowHeight;
    this.recCurrentTable.Height = (int)((g.MeasureString(_Table.GetObject(0).Text[0],_Table.GetObject(0).Font).Height+CellMargin)*_Table.TableHeight);     
   }
   _recTableRecord.Height = this.recCurrentTable.Height;
   //绘制整个表格      
   _recTableRowRecord = this.recCurrentTable;
   _recTableRowRecord.Width = 0;
   _recTableRowRecord.Height = 0;
   int intNum = _Table.Count;
   int RowHeight = (int)(g.MeasureString(_Table.GetObject(0).Text[0],_Table.GetObject(0).Font).Height+CellMargin);
   for (;this.CurrentRowNum<intNum;CurrentRowNum++)
   { 
    //判断是否画到表的右边框处,如果是则换行
    if ( _recTableRowRecord.Width == 0 )
     if ( recBody.Bottom - _recTableRowRecord.Y < (int)((g.MeasureString(_Table.GetObject(this.CurrentRowNum).Text[0],_Table.GetObject(this.CurrentRowNum).Font).Height+CellMargin)*_Table.GetObject(this.CurrentRowNum).RowHeight))
     { 
      Pen penBorder = new Pen( _Table.Color,1.0f );      
      if ( _Table.IsHasBorder )
      {
       this.recCurrentTable.Height = 0;
       for ( int i = this.PrePageLastRowNum;i< this.CurrentRowNum;i++ )
        this.recCurrentTable.Height += RowHeight*_Table.GetObject(i).RowHeight;
       g.DrawRectangle(penBorder,this.recCurrentTable);
      }
      if ( _report.Body.IsHasBorder )
      {
       penBorder = new Pen(_report.Body.Color,1.0f);
       Size size = new Size(rec.Width,_recTableRowRecord.Y - this._point.Y);
       Rectangle RecBorder = new Rectangle(this._point,size);
       g.DrawRectangle(penBorder,RecBorder);       
      }
      penBorder.Dispose();
      this.PrePageLastRowNum = this.CurrentRowNum;
      ev_HasMorePages = true;
      //ev.HasMorePages = true;
      return true;
     }
    DrawGridTableRow(g,this.recCurrentTable,ref _recTableRowRecord,_Table.GetObject(this.CurrentRowNum),ref CurrentRowNum,BodyIsHasBorder,BodyBoderColor,ev);
   }
   if ( _Table.IsHasBorder )
   {
    this.recCurrentTable.Height = 0;
    for ( int i = this.PrePageLastRowNum;i< this.CurrentRowNum;i++ )
     this.recCurrentTable.Height += RowHeight*_Table.GetObject(i).RowHeight;
    Pen pen = new Pen(_Table.Color,1.0f);
    g.DrawRectangle(pen,this.recCurrentTable);
    pen.Dispose();
   }
   
   _recTableRecord.Width += this.recCurrentTable.Width;
   recBody.X += this.recCurrentTable.Width;
   recBody.Width -= _recTableRecord.Width;
   //判断是否画到Body的右边处,如果是则定位到当前的左上角处
   if ( recBody.Width <=5)
   {
    this.recCurrentTable.Height = 0;
    for ( int i = this.PrePageLastRowNum;i< this.CurrentRowNum;i++ )
     this.recCurrentTable.Height += RowHeight*_Table.GetObject(i).RowHeight;
    rec.Y += this.recCurrentTable.Height;
    rec.Height -= this.recCurrentTable.Height;
    recBody = rec;
    _recTableRecord = recBody;
    _recTableRecord.Width = 0;
   }
   return false;
  }
 
  /// <summary>
  /// 绘制TableRow,同一行上属于同一表格的TableRow应该保持同高
  /// </summary>
  /// <param name="g">绘制表面</param>
  /// <param name="rec">当前可绘制区域</param>
  /// <param name="recTable">当前表格所占区域</param>
  /// <param name="recRecord">记录当前行是是否长度等于表格长,如果小于则记录当前位置信息</param>
  /// <param name="_TableRow">DataRow对象</param>
  /// <param name="CurrentRowNum">当前要打印的行数</param>
  /// <param name="point" >主体表格顶部左边的的坐标</param>
  /// <param name="Width">主题区域的宽度,画边框用</param>
  /// <param name="BodyIsHasBorder">是否绘制Body边框线</param>
  /// <param name="BodyBorderColor">Body边框线的颜色</param>
  /// <param name="ev">当前事件提供数据</param>
  private void DrawGridTableRow(Graphics g,Rectangle recTable,ref Rectangle _recTableRowRecord,TableRow _TableRow,ref int CurrentRowNum,bool BodyIsHasBorder,Color BodyBorderColor,System.Drawing.Printing.PrintPageEventArgs ev)
  {
   //定义表行边框
   Rectangle recTableRow = recTable;
   recTableRow.X = _recTableRowRecord.X+_recTableRowRecord.Width;
   recTableRow.Y = _recTableRowRecord.Y;
   recTableRow.Width = (int)(recTable.Width*_TableRow.RowWidth);
   if ( System.Math.Abs(recTable.Width-_recTableRowRecord.Width - recTableRow.Width)<=5 )
    recTableRow.Width = recTable.Width-_recTableRowRecord.Width;   
   recTableRow.Height =  (int)((g.MeasureString(_TableRow.Text[0],_TableRow.Font).Height+CellMargin)*_TableRow.RowHeight);   
   
   //绘制整行
   int intNum = _TableRow.Text.GetLength(0);
   Brush brush = new SolidBrush(_TableRow.Color);
   Pen pen = new Pen(brush,1.0f);
   Rectangle recRowCell = recTableRow;
   for (int i = 0;i<intNum;i++)
   {
    recRowCell.Width = recTableRow.Width*_TableRow.ColWidth[i]/_TableRow.ColWidth[intNum];
    if ( System.Math.Abs(recTable.X+recTable.Width - recRowCell.X - recRowCell.Width)<=5 )
                   recRowCell.Width = recTable.X+recTable.Width - recRowCell.X;
    g.DrawString(_TableRow.Text[i],_TableRow.Font,brush,recRowCell,_TableRow.StringFormat);

    //如果是最后一个格子就和表格右边一致,否则会出现双线;
    if ( _TableRow.IsHasGridLine )
    {
     if ( i == intNum-1 )
      recRowCell.Width += recTable.Right-recRowCell.Right ;
     g.DrawRectangle(pen,recRowCell);
    }
    recRowCell.X += recRowCell.Width;
   }
   if ( !_TableRow.IsHasGridLine )
    if ( _TableRow.IsHasBorder )
    {
     g.DrawRectangle(pen,recTableRow);
    }
   pen.Dispose();
   brush.Dispose();
   _recTableRowRecord.Width += recTableRow.Width;
   if ( System.Math.Abs( recTable.Width - _recTableRowRecord.Width ) <= 5)
   {
    _recTableRowRecord.X = recTable.X;
    _recTableRowRecord.Y += recTableRow.Height;
    _recTableRowRecord.Height -= recTableRow.Height;
    _recTableRowRecord.Width = 0;
   }
   
  }

  /// <summary>
  /// 绘制Footer
  /// </summary>
  /// <param name="g">绘制Footer</param>
  /// <param name="rec">当前可绘制的区域矩阵</param>
  /// <param name="footer">Footer对象</param>
  private void DrawGridFooter(Graphics g,ref Rectangle rec,Footer footer,bool BodyIsHasBorder,Color BodyBoderColor,System.Drawing.Printing.PrintPageEventArgs ev)
  {
   Rectangle recBody = rec;
   Rectangle recRecord = rec;
   recRecord.Width = 0;
   Rectangle recFooter = rec;
   recFooter.Width = rec.Width;
   recFooter.Height =  (int)(g.MeasureString(footer.Table.GetObject(0).Text[0],footer.Table.GetObject(0).Font).Height+CellMargin)*footer.Table.TableHeight;
   DrawHeaderFooterTable(g,ref rec,ref recBody,recFooter,footer.Table);
   Brush brush = new SolidBrush(footer.Color); 
   Pen pen = new Pen(brush);
   if ( footer.IsHasBorder )
    g.DrawRectangle(pen,recFooter);
   pen.Dispose();
   brush.Dispose();
   if ( this.ev_HasMorePages )
    ev.HasMorePages=true;
   else ev.HasMorePages=false;
  }
  
  private void DrawHeaderFooterTable(Graphics g,ref Rectangle rec,ref Rectangle recBody,Rectangle recCurrentTable,Table _Table)
  {      
   
   //定义表格的绘制区域
            Rectangle recTable = recBody;
   recTable.Width = (int)(rec.Width*_Table.TableWidth);
   if ( System.Math.Abs(rec.Width - recBody.Width)<= 5 )
    recTable.Width = recBody.Width;
   recTable.Height = (int)(g.MeasureString(_Table.GetObject(0).Text[0],_Table.GetObject(0).Font).Height+CellMargin)*_Table.TableHeight;
   _recTableRecord.Height = recTable.Height;
   //绘制整个表格 
   Rectangle _recTableRowRecord = recTable;
   _recTableRowRecord.Width = 0;
   _recTableRowRecord.Height = 0;
   int intNum = _Table.Count;
   int RowHeight = (int)(g.MeasureString(_Table.GetObject(0).Text[0],_Table.GetObject(0).Font).Height+CellMargin);
   for (int i = 0;i<intNum;i++)
   { 
    DrawHeaderFooterTableRow(g,ref recBody,recTable,ref _recTableRowRecord,_Table.GetObject(i));
   }
   if ( _Table.IsHasBorder )
   {
    Pen pen = new Pen(_Table.Color,1.0f);
    g.DrawRectangle(pen,recTable);
    pen.Dispose();
   }
  }
 

  private void DrawHeaderFooterTableRow(Graphics g,ref Rectangle recBody,Rectangle recTable,ref Rectangle _recTableRowRecord,TableRow _TableRow)
  {
   //定义表行边框
   Rectangle recTableRow = recTable;
   recTableRow.X = _recTableRowRecord.X+_recTableRowRecord.Width;
   recTableRow.Y = _recTableRowRecord.Y;
   recTableRow.Width = (int)(recTable.Width*_TableRow.RowWidth);
   if ( System.Math.Abs(recTable.Width-_recTableRowRecord.Width - recTableRow.Width)<=5 )
    recTableRow.Width = recTable.Width-_recTableRowRecord.Width;   
   recTableRow.Height =  (int)((g.MeasureString(_TableRow.Text[0],_TableRow.Font).Height+CellMargin)*_TableRow.RowHeight);   
   
   //绘制整行
   int intNum = _TableRow.Text.GetLength(0);
   Brush brush = new SolidBrush(_TableRow.Color);
   Pen pen = new Pen(brush,1.0f);
   Rectangle recRowCell = recTableRow;
   for (int i = 0;i<intNum;i++)
   {
    recRowCell.Width = recTableRow.Width*_TableRow.ColWidth[i]/_TableRow.ColWidth[intNum];
    if ( System.Math.Abs(recTable.X+recTable.Width - recRowCell.X - recRowCell.Width)<=5 )
     recRowCell.Width = recTable.X+recTable.Width - recRowCell.X;
    g.DrawString(_TableRow.Text[i],_TableRow.Font,brush,recRowCell,_TableRow.StringFormat);

    //如果是最后一个格子就和表格右边一致,否则会出现双线;
    if ( _TableRow.IsHasGridLine )
    {
     if ( i == intNum-1 )
      recRowCell.Width += recTable.Right-recRowCell.Right ;
     g.DrawRectangle(pen,recRowCell);
    }
    recRowCell.X += recRowCell.Width;
   }
   
   if ( _TableRow.IsHasBorder )
   {
    g.DrawRectangle(pen,recTableRow);
   }
   pen.Dispose();
   brush.Dispose();
   _recTableRowRecord.Width += recTableRow.Width;
   if ( System.Math.Abs( recTable.Width - _recTableRowRecord.Width ) < 5)
   {
    _recTableRowRecord.X = recTable.X;
    _recTableRowRecord.Y += recTableRow.Height;
    _recTableRowRecord.Height -= recTableRow.Height;
    _recTableRowRecord.Width = 0;
   }
  }
      
  #endregion

  #region 预览和打印
  public void PrinterPreview()
  {
   try
   {
    if ( this.PrinterName==null )
    {
     System.Windows.Forms.MessageBox.Show("在您的电脑上没有找到任何可用的打印机","提示");
     return;
    }
    CurrentRowNum = 0;
    CurrentTableNum = 0;
    ev_HasMorePages = false;
    this._PrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.DrawReport);
    this._PrintDocument.DefaultPageSettings.Landscape = this._report.IsLandscape;
    this._PrintDocument.DefaultPageSettings.Margins = this._report.Margins; 
    this._PrintDocument.DefaultPageSettings.PaperSize = this._report.PageSize;    
    this._PrintDocument.PrinterSettings.PrinterName = this.PrinterName;
    System.Windows.Forms.PrintPreviewDialog preview = new System.Windows.Forms.PrintPreviewDialog();
    preview.Document = _PrintDocument;
    preview.ShowDialog();
   }
   catch(Exception Ex)
   {
    System.Windows.Forms.MessageBox.Show(Ex.ToString());
   }
  }
  
  /// <summary>
  /// 默认打印方法,使用对象指定的页边距,横竖方式
  /// </summary>
  public void PrinterPrintTheReport()
  {
   try
   {
    if ( this.PrinterName==null )
    {
     System.Windows.Forms.MessageBox.Show("在您的电脑上没有找到任何可用的打印机","提示");
     return;
    }
    CurrentRowNum = 0;
          CurrentTableNum = 0;
                ev_HasMorePages = false;
    this._PrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.DrawReport);
    this._PrintDocument.DefaultPageSettings.Landscape = this._report.IsLandscape;
    this._PrintDocument.DefaultPageSettings.Margins = this._report.Margins; 
    this._PrintDocument.DefaultPageSettings.PaperSize = this._report.PageSize;    
    this._PrintDocument.PrinterSettings.PrinterName = this.PrinterName;
    this._PrintDocument.Print();    
   }
   catch(Exception Ex)
   {
    System.Windows.Forms.MessageBox.Show(Ex.ToString());
   }
  }
  
  #endregion

  /// <summary>
  /// 获取或是设置CurrentPage值
  /// </summary>
  public int CurrentPage
  {
   set
   {
    this._CurrentPage = value;
   }
   get
   {
    return this._CurrentPage;
   }
  }

  /// <summary>
  /// 获取或是设置_PrinterName值
  /// </summary>
  public string PrinterName
  {
   set
   {
    this._PrinterName = value;
   }
   get
   {
    return this._PrinterName;
   }
  }
 }
}

posted on 2006-09-14 17:59  空谷幽兰  阅读(689)  评论(0编辑  收藏  举报