- 热门文章:
- · 手把手教你在Win2003下配置ASP.NET开发环境
- · 提供一个测试用的“平面按纽”控件代码!如想看结果,请自行编译
- · 转载:用Visual C#动态生成组件,请看!(三)
- · 转载:用Visual C#动态生成组件,请看!(一)
- · 转载:用Visual C#动态生成组件,请看!(二)
- · 可以用API playsound来播放wav
- · 用Api函数播放wav文件
- · 用Visual C#动态生成组件(转自中国在线)
- · Performance Tips and Tricks in .NET Applications(五)
- · Performance Tips and Tricks in .NET Applications(四)
- · Performance Tips and Tricks in .NET Applications(三)
- · Performance Tips and Tricks in .NET Applications(二)
上一篇:提供一个DataGrid的打印类 >>
续
protected override void OnPrintPage(PrintPageEventArgs ev)
{
base.OnPrintPage(ev) ;
int hh=this.currentLineNumber ;
bool hasMorePage=false;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
Graphics g=ev.Graphics;
float y=topMargin;
float x=leftMargin;
DataTable dt=(DataTable)printDataGrid.DataSource;
//打印头标题一
if(this.headtitle1!=null)
{
float headleft=(ev.MarginBounds.Width-g.MeasureString(this.headtitle1,new Font("宋体",16,FontStyle.Bold)).Width)/2+leftMargin;
g.DrawString(this.headtitle1,new Font("宋体",16,FontStyle.Bold),Brushes.Black,headleft,ev.MarginBounds.Top);
}
//打印头标题二
if(this.headtitle2!=null)
{
y+=new Font("宋体",16,FontStyle.Bold).GetHeight(ev.Graphics);
float headleft=(ev.MarginBounds.Width-g.MeasureString(this.headtitle2,new Font("宋体",16)).Width)/2+leftMargin;
g.DrawString(this.headtitle2,new Font("宋体",16,FontStyle.Bold),Brushes.Black,headleft,y);
}
//打印头标题三
if(this.headtitle3!=null)
{
float headleft=ev.MarginBounds.Width-g.MeasureString(this.headtitle3,new Font("宋体",12)).Width+leftMargin;
y+=printFont.GetHeight(ev.Graphics)*2;
g.DrawString(this.headtitle3,new Font("宋体",12),Brushes.Black,headleft,y);
}
//打印头标题四一
if(this.headtitle41!=null)
{
float headleft=leftMargin;
y+=printFont.GetHeight(ev.Graphics)*2;
g.DrawString(this.headtitle41,new Font("宋体",12),Brushes.Black,headleft,y);
}
//打印头标题四二
if(this.headtitle42!=null)
{
float headleft=ev.MarginBounds.Width*1/3+leftMargin;
g.DrawString(this.headtitle42,new Font("宋体",12),Brushes.Black,headleft,y);
}
//打印头标题四三
if(this.headtitle43!=null)
{
float headleft=ev.MarginBounds.Width*2/3+leftMargin;
g.DrawString(this.headtitle43,new Font("宋体",12),Brushes.Black,headleft,y);
}
//使用 MarginBounds 计算出每页打印的行数
int tailnumber=0;
//减去页尾一算一行
if(this.tailtitle11!=null||this.tailtitle12 !=null||this.tailtitle13!=null)
tailnumber+=1;
//减去页尾二一行
if(this.tailtitle2!=null)
tailnumber+=1;
//减去打印页码一行
if(this.isPrintPageNumber)
tailnumber+=1;
//减去标题列算一行
if(this.colname !=null)
tailnumber+=1;
this.numsPerPage = Convert.ToInt32((ev.MarginBounds.Height -y+ev.MarginBounds.Top) / (printFont.GetHeight(ev.Graphics)*(LineEmpty*2-1)+printFont.GetHeight(ev.Graphics))) ;
this.numsPerPage -=tailnumber;
//如果用户指定了每页打印的行数,就每页打印用户指定的行数
if((this.LinePerPage>0) && (this.LinePerPage <this.numsPerPage))
this.numsPerPage =this.LinePerPage ;
//打印页码
if(this.isPrintPageNumber)
{
int totalPageNumber=dt.Rows.Count/this.numsPerPage ;
if(dt.Rows.Count%this.numsPerPage>0)
totalPageNumber+=1;
string strPageNumber="当前第"+this.currentPageNumber + "页/共"+ totalPageNumber +"页";
y+=printFont.GetHeight(ev.Graphics);
float headleft=ev.MarginBounds.Width-g.MeasureString(strPageNumber,new Font("宋体",12)).Width+leftMargin;
g.DrawString(strPageNumber,new Font("宋体",12),Brushes.Black,headleft,y);
}
//打印表格线
y+=printFont.GetHeight(ev.Graphics)*2;
if(this.isNeedHorLine)
{
g.DrawLine(new Pen(Color.Black,2),x,y,x+ev.MarginBounds.Width,y);
}
//打印正文
printDataGridContext(ref ev,ref x,ref y,out hasMorePage);
//打印尾标题一一
if(this.tailtitle11!=null)
{
//y=ev.MarginBounds.Top*2;
x=ev.MarginBounds.Left ;
g.DrawString(this.tailtitle11.ToString(),new Font("宋体",12),Brushes.Black,x,y);
}
//打印尾标题一二
if(this.tailtitle12!=null)
{
//y=ev.MarginBounds.Top*2;
x=ev.MarginBounds.Left+ev.MarginBounds.Width*1/3 ;
g.DrawString(this.tailtitle12.ToString(),new Font("宋体",12),Brushes.Black,x,y);
}
//打印尾标题一三
if(this.tailtitle13!=null)
{
//y=ev.MarginBounds.Top*2;
x=ev.MarginBounds.Left+ev.MarginBounds.Width*2/3 ;
g.DrawString(this.tailtitle13.ToString(),new Font("宋体",12),Brushes.Black,x,y);
}
//打印尾标题二
if(this.tailtitle2!=null)
{
y+=new Font("宋体",12).GetHeight(ev.Graphics);
x=ev.MarginBounds.Width-g.MeasureString(this.tailtitle2,new Font("宋体",12)).Width+leftMargin;
g.DrawString(this.tailtitle2.ToString(),new Font("宋体",12),Brushes.Black,x,y);
}
if(hasMorePage)
ev.HasMorePages =true;
else
{
this.currentLineNumber =0;
//ev.HasMorePages =false;
}
}
private float GetFontSize(PrintPageEventArgs ev,DataTable dataTable,out float[] colWidth)
{
//单位都使用缇
//返回字型大小及宽度数组
//宽度数组使用1到最后列
int m, rownumber ;
float allWidth, curWidth ;
float paperWidth=ev.MarginBounds.Width;
float[] strW;
Graphics gp=ev.Graphics ;
rownumber = dataTable.Columns.Count ;
strW=new float[rownumber];
//指定打印字体
Font font=new Font("宋体",9);
//初始化宽度数组
for(m = 0;m<rownumber;m++)
{
try
{
if(this.colname!=null)
{
strW[m]=gp.MeasureString(this.colname[m].ToString(),font).Width;
}
else
{
strW[m] = 0;
}
}
catch(Exception Err)
{
}
}
//使用遍历,获得每列的最大宽度
for(int row=0;row<dataTable.Rows.Count;row++)
{
//curWidth = strW(n);
for(int col=0;col<dataTable.Columns.Count;col++)
{
curWidth = gp.MeasureString(dataTable.Rows[row][col].ToString().Trim(),font).Width;
if( strW[col] < curWidth)
strW[col] = curWidth;
}
}
//获得宽度累计
allWidth = 0;
for(m =0;m<dataTable.Columns.Count;m++)
{
allWidth += strW[m];
}
//决定比例
float bili ;
bili = paperWidth / allWidth;
//按比例改变数组值
for(m=0;m<dataTable.Columns.Count;m++)
{
strW[m]*= bili;
}
//每列宽度数组
colWidth = strW;
//返回字体大小
return 9 * bili;
}
/// <summary>
/// 打印DataGrid正文数据
/// </summary>
/// <param name="ev"></param>
private void printDataGridContext(ref PrintPageEventArgs ev,ref float xPosition,ref float yPosition,out bool hasMorePage)
{
hasMorePage=false;
float smallChange=0;
string cellText="";
float[] colWidth;
Graphics g=ev.Graphics;
//打印的开始y坐标
float y=yPosition;
//打印的开始x坐标
float x=xPosition;
//获得要打印的表
//获得DataGrid每列的宽度及打印字体
float fontSize=this.GetFontSize(ev,dt,out colWidth);
Font font =new Font("宋体",fontSize);
Pen pen=new Pen(Color.Black,1);
y+=font.GetHeight(ev.Graphics)*(LineEmpty-1);
//打印列中文标题数组
if(this.colname !=null)
{
for(int j=0;j<this.colname.Length ;j++)
{
cellText=this.colname[j].ToString();
switch(this.ColStyle)
{
case 0:
break;
case 1:
smallChange=(colWidth[j]-g.MeasureString(cellText,font).Width )/2;
break;
case 2:
smallChange=colWidth[j]-g.MeasureString(cellText,font).Width ;
break;
}
g.DrawString(cellText,font,Brushes.Black,x+smallChange,y);
x+=colWidth[j];
}
//打印一条横线
y+=font.GetHeight(ev.Graphics)*LineEmpty;
if(this.isNeedHorLine)
{
g.DrawLine(pen,xPosition,y,xPosition+ev.MarginBounds.Width,y);
}
// //减去表格头算一行
// this.numsPerPage -=1;
}
//
int temp=currentLineNumber;
for(currentLineNumber+=1;currentLineNumber<=temp+this.numsPerPage;currentLineNumber++)
{
//如果到记录集的末尾则结束
if(currentLineNumber>dt.Rows.Count)
{
goto ExitEnd;
}
//打印一行数据
x=xPosition;
y+=font.GetHeight(ev.Graphics)*(LineEmpty-1);
for(int j=0;j<dt.Columns.Count;j++)
{
cellText=printDataGrid[currentLineNumber-1,j].ToString();
switch(this.ColStyle)
{
case 0:
break;
case 1:
smallChange=(colWidth[j]-g.MeasureString(cellText,font).Width )/2;
break;
case 2:
smallChange=colWidth[j]-g.MeasureString(cellText,font).Width ;
break;
}
g.DrawString(cellText,font,Brushes.Black,x+smallChange,y);
x+=colWidth[j];
}
//打印一条横线
y+=font.GetHeight(ev.Graphics)*LineEmpty;
if(currentLineNumber==temp+this.numsPerPage||currentLineNumber==dt.Rows.Count)
{
if(this.isNeedHorLine )
{
g.DrawLine(new Pen(Color.Black,2),xPosition,y,xPosition+ev.MarginBounds.Width,y);
}
}
else
{
if(this.isNeedHorLine)
{
g.DrawLine(pen,xPosition,y,xPosition+ev.MarginBounds.Width,y);
}
}
}
currentLineNumber-=1;
ExitEnd:
//打印表格竖线
x=xPosition;
if(this.isNeedVertLine)
{
for(int i=0;i<colWidth.Length;i++)
{
if(i==0)
{
g.DrawLine(new Pen(Color.Black,2),x,yPosition,x,y);
}
else
{
g.DrawLine(pen,x,yPosition,x,y);
}
x+=colWidth[i];
}
g.DrawLine(new Pen(Color.Black,2),x,yPosition,x,y);
}
yPosition= y+printFont.GetHeight(ev.Graphics);
//当前页数加一
currentPageNumber+=1;
if(dt.Rows.Count>currentLineNumber)
hasMorePage =true;
}
}
}
下一篇:手把手教你在Win2003下配置ASP.NET开发环境 >>
相关文章:
- · 在C#中使用COM+实现事务控制 wirte(原作) (本人转了)
- · Vs.net MDI窗口的实现。
- · Performance Tips and Tricks in .NET Applications(一)
- · 在C#中使用COM+实现事务控制(转自CSDN,版权归人家所有,大家只有看的权力!)
- · 在C#应用程序中控制输入法(转)
- · 用winform的方式操纵webform--浅谈IHttpHandler
- · FT豹豹哥,你给“鼠标左键,弹出contextMenu”的程序
- · 抛砖引玉——实现LISTVIEW点击COLUMN排序的代码,可实现按时间,字符,数字排序(测试程序)
- · to joy, this is the start splash screen of SharpDevelop
- · 手把手教你在Win2003下配置ASP.NET开发环境
- · 抛砖引玉——实现LISTVIEW点击COLUMN排序的代码,可实现按时间,字符,数字排序(控件部分)
- · Serial通信的例子(一)
- · 用 C# 编写一个停放在任务栏上的图标程序
- · Serial通信的例子(二)
- · 通过事件,在两窗体间传递数据。
- · 利用WMI获得盘符信息
- · 查看WMI的NAMESPACE
- · 使用Environment类检测操作系统版本
- · C#如何实现两个WINFORM之间的访问(原创)。
- · 在C# 中操作列表框控件
- · 用Visual C#发送电子邮件
- · Visual C#如何使用Active X组件
- · 一个简单的配置文件类
- · 在.NET(正式版)环境下读写系统日志(From CSDN)
- · 一个server基类
- · 在.Net环境下用C#操纵活动目录
- · 让窗体飘动起来--C#中Timer组件用法
- · 在C#下用Microsoft Agent创建超酷语言精灵(附图)
- · C#开发WINDOWS应用程序时消息的处理
- · 用C#监视服务器信息
- · 手把手教你在Win2003下配置ASP.NET开发环境
- · c#根据一个传入的字符串得到需要使用的类
- · 用vb.net开发api viewer(5)
- · 在.NET运行时了解类型信息(3) Paul_Ni(原作)
- · 在.NET运行时了解类型信息(2) Paul_Ni(原作)
- · 在.NET运行时了解类型信息(1) Paul_Ni(原作)
- · 使用 .NET的IO(4) Paul_Ni(原作)
- · 使用 .NET的IO(5) Paul_Ni(原作)
