频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

用vb.net实现写字板程序报告

来源: 作者:zigz 出处:巧巧读书 2007-07-20 进入讨论组
上一页 1 2 3 4 5 下一页 

  5)有关打印预览  

  起初以为很简单,但最后发现预览总是无法预览到实际文件,最终还是在微软站点上获得了相关信息,并很好的利用他到本应用程序中,而且十分成功,可以成功预览了。为了怕自己误导别人,所以把它原文也打印出来。

  下面是两幅图片用来演示打印预览的效果。

  
用vb.net实现写字板程序报告(图六)


  
用vb.net实现写字板程序报告(图七)
  

  打印预览相关代码:  

  (注意!以下有关打印的代码均来自微软技术文档中)  

  ' 必须确定所有的打印事件都是针对同一个 PrintDocument  

   Private WithEvents pdoc As New PrintDocument()  

   ' 打印文件是一个函数性的打印事件,每当要打印时该事件被触发  

   ' 下面是一个非常快速和有用的精确计算要打印的文本是否能够被包括到整张打印页面  

   '是我从微软站点上得到的资料,我把它应用到了我的程序中  

   Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage  

   ' Declare a variable to hold the position of the last printed char. Declare 
   ' as static so that subsequent PrintPage events can reference it.  

   Static intCurrentChar As Int32  

   ' Initialize the font to be used for printing.  

   Dim font As New font("Microsoft Sans Serif", 24)  

   Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32

     With pdoc.DefaultPageSettings  

   ' Initialize local variables that contain the bounds of the printing

     ' area rectangle.  

   intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom  

   intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

  

  

  

   ' Initialize local variables to hold margin values that will serve  

   ' as the X and Y coordinates for the upper left corner of the printing

     ' area rectangle.  

   marginLeft = .Margins.Left ' X coordinate  

   marginTop = .Margins.Top ' Y coordinate  

   End With  

   ' If the user selected Landscape mode, swap the printing area height

     ' and width.  

   If pdoc.DefaultPageSettings.Landscape Then  

   Dim intTemp As Int32  

   intTemp = intPrintAreaHeight  

   intPrintAreaHeight = intPrintAreaWidth  

   intPrintAreaWidth = intTemp  

   End If  

   ' Calculate the total number of lines in the document based on the height of

   ' the printing area and the height of the font.  

   Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)  

   ' Initialize the rectangle structure that defines the printing area.  

   Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)  

   ' Instantiate the StringFormat class, which encapsulates text layout   

   ' information (such as alignment and line spacing), display manipulations   
   ' (such as ellipsis insertion and national digit substitution) and OpenType

     ' features. Use of StringFormat causes MeasureString and DrawString to use    ' only an integer number of lines when printing each page, ignoring partial

     ' lines that would otherwise likely be printed if the number of lines per

     ' page do not divide up cleanly for each page (which is usually the case).  

   ' See further discussion in the SDK documentation about StringFormatFlags. 

   Dim fmt As New StringFormat(StringFormatFlags.LineLimit)  

   ' Call MeasureString to determine the number of characters that will fit in 

   ' the printing area rectangle. The CharFitted Int32 is passed ByRef and used

   ' later when calculating intCurrentChar and thus HasMorePages. LinesFilled  

   ' is not needed for this sample but must be passed when passing CharsFitted.  

   ' Mid is used to pass the segment of remaining text left off from the   

   ' previous page of printing (recall that intCurrentChar was declared as   

   ' static.  

   Dim intLinesFilled, intCharsFitted As Int32  

   e.Graphics.MeasureString(Mid(rtbox.Text, intCurrentChar + 1), font, _  

   New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _  

   intCharsFitted, intLinesFilled)  

   ' Print the text to the page.  

   e.Graphics.DrawString(Mid(rtbox.Text, intCurrentChar + 1), font, _  

   Brushes.Black, rectPrintingArea, fmt)  

   ' Advance the current char to the last char printed on this page. As   

   ' intCurrentChar is a static variable, its value can be used for the next  

   ' page to be printed. It is advanced by 1 and passed to Mid() to print the  
   ' next page (see above in MeasureString()).  

   intCurrentChar += intCharsFitted  

   ' HasMorePages tells the printing module whether another PrintPage event  

   ' should be fired.  

   If intCurrentChar < rtbox.Text.Length Then  

   e.HasMorePages = True  

   Else  

   e.HasMorePages = False  

   ' You must explicitly reset intCurrentChar as it is static.  

   intCurrentChar = 0  

  End If  

   End Sub  

   Private Sub printpreview()  

   Dim ppd As New PrintPreviewDialog()  

   Try  

   ppd.Document = pdoc  

   ppd.ShowDialog()  

   Catch exp As Exception  

   MessageBox.Show("有错误发生!!不能预览 !" & _  

   "确信现在你是否能够 " & _  

   "连接到一个打印机?" & _  

   "然后预览才可以.", Me.Text, _  

   MessageBoxButtons.OK, MessageBoxIcon.Error)  

   End Try  

   End Sub  

   Private Sub mPrintpreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mPrintpreview.Click  

   printpreview()  

  End Sub
  

  其它比如状态栏的状态提示,字体对话框,颜色对话框等等不细说了,请参考源代码。收藏地址:http://www.qqread.com/vbdotnet/d283416.html 更多文章 更多内容请看.NET移动与嵌入式技术.NET开发手册.NET实用开发专题,或进入讨论组讨论。
上一页 1 2 3 4 5 下一页 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章