All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
VS2005+Crystal,Report开发Web应用_[Asp.Net教程]
如何利用PHP+MYSQL保存和输出文件_php资料_编程技术
在Windows,xp系统上安装了SQL,server服务器版_[SQL,Server教程]
asp.net2.0服务器控件之RadioButton控件
理解C#值类型与引用类型_[Asp.Net教程]
C#网络应用编程基础练习题与答案(五)_[Asp.Net教程]
delphi中使用游标处理数据
C#中 DirectoryEntry组件应用实例
用C#截取指定长度的中英文混合字符串_[Asp.Net教程]
动态网页制作技术PHP常用的正则表达式_[PHP教程]
Reading number is top 10 pictures
Discharge accidentally Actresses by the breast1
这酸爽,让人不敢相信
Compared GDP and per capita income in China for 40 years
超级大兔子
含苞欲放的素颜美少女1
Born After 90 Beijing sports university campus flower photos3
Hunan road lawenforcement vehicle pursue overload car caused the car turn and man is died
Exquisite decoration is not paying too much2
小学生作文又现神作,你不得不佩服
Send some Valentine's day cartoon
Download software ranking
Sora aoi - one of more PK
1400篇各类破解文章
传奇私服架设教程-chm
Boxer vs Yellow5
株洲本地在线棋牌游戏
天龙八部十二宫服务端
Unix video tutorial11
Sora aoi, the nurse, uniform ,nursing assistant
Unix video tutorial8
天龙八部最新服务端
delv published in(发表于) 2014/1/16 9:30:15 Edit(编辑)
MIS开发中.net,Framework的打印功能_[Asp.Net教程]

MIS开发中.net,Framework的打印功能_[Asp.Net教程]

MIS开发中.net Framework的打印功能_[Asp.Net教程]

Microsoft .net Framework的打印功能都以组件的方式提供,为程序员提供了很大的方便,但是这几个组件的使用还是很复杂的,有必要解释一下。

  打印操作通常包括以下四个功能


  1 打印设置 设置打印机的一些参数比如更改打印机驱动程序等


  2 页面设置 设置页面大小纸张类型等


  3 打印预览 类似于word中的打印预览


  4 打印


  实现打印功能的核心是PrintDocument类这个类属于System.Drawing.Printing名字空间这个类封装了当前的打印设置页面设置以及所


  有的与打印有关的事件和方法


  这个类包括以下几个属性 事件 和方法


  1、PrinterSettings 属性


  存放打印机的设置信息这个属性不需要程序员设置因为它是由打印对话框获取的


  2、PrintCountroller 属性


  控制打印过程


  3、DefaultPageSettings 属性


  存放页面设置信息 打印纸大小方向等也不需要程序员设置因为它是由页面设置对话框获取的


  4、DocumentName 属性


  指定文档名称,出现在打印机状态窗口中


  1、BeginPrint事件


  在打印之前发出


  2. PrintPage事件


  每打印一页是发出,事件接受一个PrintPageEventArgs参数该参数封装了打印相关的信息


  PrintPageEventArgs参数有很多重要的属性


  1 Cancel 取消打印


  2 Graphics 页面的绘图对象


  3 HasMorePages 是否还有要打印的页面


  Print 方法 该方法没有参数 调用它将按照当前设置开始打印


  若实现打印功能首先构造PrintDocument对象添加打印事件


  PrintDocument printDocument;
  private void InitializeComponent()
  {
  ...
  printDocument=new PrintDocument();
  printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);
  ...
  }
  实现打印事件功能
  打印和绘图类似都是调用Graphics 类的方法进行画图 不同的是一个在显示器上一个在打印纸上并且打印要进行一些复杂的计算
  如换行 分页等。
  private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
  {
  Graphics g = e.Graphics; //获得绘图对象
  float linesPerPage = 0; //页面的行号
  float yPosition = 0; //绘制字符串的纵向位置
  int count = 0; //行计数器
  float leftMargin = e.MarginBounds.Left; //左边距
  float topMargin = e.MarginBounds.Top; //上边距
  string line = null; 行字符串
  Font printFont = this.textBox.Font; //当前的打印字体
  SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
  linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数
  //逐行的循环打印一页
  while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))
  {
  yPosition = topMargin + (count * printFont.GetHeight(g));
  g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
  count++;
  }


  如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会


  自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置



  if(line != null)
  e.HasMorePages = true;
  else
  e.HasMorePages = false;
  }


  打印设置,构造打印对话框 将对话框中设置的Document属性赋给printDocument这样会将用户的设置自动保存到printDocument


  的PrinterSettings属性中



  protected void FileMenuItem_PrintSet_Click(object sender,EventArgs e)
  {
  PrintDialog printDialog = new PrintDialog();
  printDialog.Document = printDocument;
  printDialog.ShowDialog();
  }


  页面设置和打印预览与打印设置原理相同都是构造对话框将用户在对话框中的设置保存到相应的类的属性中



  protected void FileMenuItem_PageSet_Click(object sender,EventArgs e)
  {
  PageSetupDialog pageSetupDialog = new PageSetupDialog();
  pageSetupDialog.Document = printDocument;
  pageSetupDialog.ShowDialog();
  }


  打印预览



  protected void FileMenuItem_PrintView_Click(object sender,EventArgs e)
  {
  PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
  printPreviewDialog.Document = printDocument;
  lineReader = new StringReader(textBox.Text);
  try
  {
  printPreviewDialog.ShowDialog();
  }
  catch(Exception excep)
  {
  MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
  }


  打印就可以直接调用printDocument的Print()方法因为用户可能在打印之前还要再更改打印设置所以


  在这里再次显示打印设置对话框



  protected void FileMenuItem_Print_Click(object sender,EventArgs e)
  {
  PrintDialog printDialog = new PrintDialog();
  printDialog.Document = printDocument;
  lineReader = new StringReader(textBox.Text);
  if (printDialog.ShowDialog() == DialogResult.OK)
  {
  try
  {
  printDocument.Print();
  }
  catch(Exception excep)
  {
  MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
  printDocument.PrintController.OnEndPrint(printDocument,new PrintEventArgs());
  }
  }
  }


  总结打印的过程是


  1 在应用程序窗体初始化时构造PrintDocument对象 添加 printDocument 的 PrintPage 方法


  2 实现PrintPage方法 4 在用户的单击事件中调用 printDocument 的 Print方法实现打印功能


  在这中间可能要用到 PrintDialog PrintPreviewDialog PageSetupDialog 设置和查看打印效


  果这些方法通常是由菜单的单击触发的。


出处:CSDN







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.