分享
 
 
 

用vb.net实现写字板程序报告(四)完

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

所有源代码均在这里下载:

http://www.up2e.com/resource.php[/url]

用vb.net实现写字板程序报告(四)完 ---by zigz (LuHai)

[url=mailto:luluhai@eastday.com]luluhai@eastday.com

5)有关打印预览

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

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

图5在RichTextBox中的文本

图6打印预览中的文本

打印预览相关代码:

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

' 必须确定所有的打印事件都是针对同一个 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

其它比如状态栏的状态提示,字体对话框,颜色对话框等等不细说了,请参考源代码。

总结:

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

全文完。

by zigz(LuHai)

luluhai@eastday.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有