首页 | 互联网 | IT动态 | 网络设备 | 服务器 | IDC | 安全 | Cisco | Windows | Linux | Java | .Net | Oracle | CIW | 华为 | 专题
IT技术 | 网页设计 | 平面设计 | 电子书下载 | 教学视频 | 方案 | 数字网校 | 直播室 | 虚拟考场 | 面授培训 | 搜索 | 博客 | 沙龙 | 论坛
首页 | JAVA | C# | VB | VB.NET | C/C++ | delphi | 工程管理 | 其他语言 | 论坛
免费注册一站通帐号,参与直播、论坛、下载、博客、网摘、评论,展现我的风采!
您现在的位置: 中国IT实验室 >> 桌面开发 >> VB.net >> 文章正文
用vb.net实现写字板程序报告
来源:中国IT实验室整理  时间:2007-4-7

 

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

用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

7、总结

总体来说,本程序达到了windows写字板85%的功能,很可惜就是没有做标尺的效果,也有些思路,就是利用拖动控件代码,设置两个控件,左右对称。 规定这两个控件只能在一条水平线上拖动,根据两个控件的移动来确定Richtextbox中文本前后间距的空间大小,大致思路就是这样。

所有源代码均在这里下载:http://www.up2e.com/resource.php

上一页  [1] [2] [3] 

【责编:Lili】

中国IT教育热线咨询

相关文章
VB.NET中的多窗体编程:升级到 .NET
解析VB的事件驱动编程
编写快速高效的VB程序
VB中运用反射原理优化程序代码
VB中数据集合对象的应用
递归过程在VB中的应用实例
VB/VB.NET/C#导出到Excel的方法
关于Visual Basic 9.0的动态标识符
推荐文章
· 用C#创建COM对象
· IT管理十大失误及其对策
· VC中利用MFC设计绘图程序初步
· JAVA中对象创建和初始化过程
· C语言中的位域的使用
· 浅谈Java桌面应用程序开发
· C#的前途如何?
· 几种VC++数据库开发技术的相对比较
 精彩友情推荐
·锐捷交换机报价
·锐捷交换机
·锐捷网络网络交换机
·smc交换机
·smc交换机报价
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
最新更新 推荐文章
·Visual Basic 9.0隐式类型的局部…09-30
·JMX+J2SE5.0实现Web应用的安全管…09-30
·多线程、Socket技术及委托技术的…09-21
·Visual C#多线程参数传递浅析09-21
·浅谈Java中利用JCOM实现仿Excel编…09-21
·基于Java的界面布局DSL的设计与实…09-21
·Java开发中的事件驱动模型实例详…09-21
·并发工程原则应用到软件项目中09-06
·Delphi初学者应小心的六大陷阱09-06
·VC开发多语言界面支持的简单方法09-06
·用C#创建COM对象09-06
·用C#创建COM对象09-06
·IT管理十大失误及其对策08-30
·VC中利用MFC设计绘图程序初步08-23
·JAVA中对象创建和初始化过程08-23
·C语言中的位域的使用08-09
·浅谈Java桌面应用程序开发08-09
·C#的前途如何?08-02
·几种VC++数据库开发技术的相对比较07-12
·用Visual C#实现网络封包监视07-12
·VB.NET中的TextBox控件详解07-12
·VB.NET实现PC与掌上电脑PPC的双向通信07-05
  培训中心