如何将richtextbox 中的内容打印到picturebox 中,并在pictruebox中分页显示?(高手试试))

jacky_wxl(微信同号) 2001-09-25 04:30:26
...全文
256 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
TechnoFantasy 2001-10-03
  • 打赏
  • 举报
回复
MSDN上有范例,将RTF内容预览到PictureBox上:

SUMMARY
The SelPrint method of the RichTextBox control does not allow a programmer to set the position of the output on the printer. In addition, the RichTextBox control does not provide a method for displaying its contents as they would show up on the printer. This article explains how to set up a RichTextBox with a WYSIWYG (What You See Is What You Get) display and then how to print it.



MORE INFORMATION
The Visual Basic RichTextBox control is a sub-classed control based on the RichTextBox provided by the Win32 operating system. The operating system control supports many messages that are not exposed in Visual Basic. One of these messages is EM_SETTARGETDEVICE. The EM_SETTARGETDEVICE message is used to tell a RichTextBox to base its display on a target device such as a printer. Another message that is not fully exposed by Visual Basic is EM_FORMATRANGE. The EM_FORMATRANGE message sends a page at a time to an output device using the specified coordinates. Using these messages in Visual Basic, it is possible to make a RichTextBox support WYSIWYG display and output.

The following example illustrates how to take advantage of the EM_SETTARGETDEVICE and EM_FORMATRANGE messages from Visual Basic. The example provides two re-usable procedures to send these messages. The first procedure WYSIWYG_RTF() sets a RichTextBox to provide a WYSIWYG display based on the default printer and specified margins. The second procedure PrintRtf() prints the contents of the RichTextBox with the specified margins.

EXAMPLE
Start a new project in the Visual Basic 32-bit edition. Form1 is created by default.


Put a CommandButton and a RichTextBox control on Form1.


Add the following code to Form1:


Private Const AnInch As Long = 1440 '1440 twips per inch
Private Const QuarterInch As Long = 360

Private Sub Form_Load()
Dim PrintableWidth As Long
Dim PrintableHeight As Long
Dim x As Single

' Initialize Form and Command button
Me.Caption = "Rich Text Box WYSIWYG Printing Example"
Command1.Move 10, 10, 600, 380
Command1.Caption = "&Print"

' Set the font of the RTF to a TrueType font for best results
RichTextBox1.SelFontName = "Arial"
RichTextBox1.SelFontSize = 10

'initialize the printer object
x = Printer.TwipsPerPixelX
Printer.Orientation = vbPRORPortrait 'vbPRORLandscape

' Tell the RTF to base it's display off of the printer
Call WYSIWYG_RTF(RichTextBox1, QuarterInch, QuarterInch, QuarterInch, QuarterInch, PrintableWidth, PrintableHeight) '1440 Twips=1 Inch

' Set the form width to match the line width
Me.Width = PrintableWidth + 200
Me.Height = PrintableHeight + 800
End Sub

Private Sub Form_Resize()
' Position the RTF on form
RichTextBox1.Move 100, 500, Me.ScaleWidth - 200, Me.ScaleHeight - 600
End Sub

Private Sub Command1_Click()
' Print the contents of the RichTextBox with a one inch margin
PrintRTF RichTextBox1, AnInch, AnInch, AnInch, AnInch
End Sub

Insert a new standard module into the project, Module1.bas is created by default.


Add the following code to Module1:


Option Explicit

Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type CharRange
cpMin As Long ' First character of range (0 for start of doc)
cpMax As Long ' Last character of range (-1 for end of doc)
End Type

Private Type FormatRange
hdc As Long ' Actual DC to draw on
hdcTarget As Long ' Target DC for determining text formatting
rc As Rect ' Region of the DC to draw to (in twips)
rcPage As Rect ' Region of the entire DC (page size) (in twips)
chrg As CharRange ' Range of text to draw (see above declaration)
End Type

Private Const WM_USER As Long = &H400
Private Const EM_FORMATRANGE As Long = WM_USER + 57
Private Const EM_SETTARGETDEVICE As Long = WM_USER + 72
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113

Private Declare Function GetDeviceCaps Lib "gdi32" ( _
ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, _
lp As Any) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
(ByVal lpDriverName As String, ByVal lpDeviceName As String, _
ByVal lpOutput As Long, ByVal lpInitData As Long) As Long

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' WYSIWYG_RTF - Sets an RTF control to display itself the same as it
' would print on the default printer
'
' RTF - A RichTextBox control to set for WYSIWYG display.
'
' LeftMarginWidth - Width of desired left margin in twips
'
' RightMarginWidth - Width of desired right margin in twips
'
' Returns - The length of a line on the printer in twips
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub WYSIWYG_RTF(RTF As RichTextBox, LeftMarginWidth As Long, RightMarginWidth As Long, TopMarginWidth As Long, BottomMarginWidth As Long, PrintableWidth As Long, PrintableHeight As Long)
Dim LeftOffset As Long
Dim LeftMargin As Long
Dim RightMargin As Long
Dim TopOffset As Long
Dim TopMargin As Long
Dim BottomMargin As Long
Dim PrinterhDC As Long
Dim r As Long

' Start a print job to initialize printer object
Printer.Print Space(1)
Printer.ScaleMode = vbTwips

' Get the left offset to the printable area on the page in twips
LeftOffset = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX)
LeftOffset = Printer.ScaleX(LeftOffset, vbPixels, vbTwips)

' Calculate the Left, and Right margins
LeftMargin = LeftMarginWidth - LeftOffset
RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset

' Calculate the line width
PrintableWidth = RightMargin - LeftMargin

' Get the top offset to the printable area on the page in twips
TopOffset = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY)
TopOffset = Printer.ScaleX(TopOffset, vbPixels, vbTwips)

' Calculate the Left, and Right margins
TopMargin = TopMarginWidth - TopOffset
BottomMargin = (Printer.Height - BottomMarginWidth) - TopOffset

' Calculate the line width
PrintableHeight = BottomMargin - TopMargin


' Create an hDC on the Printer pointed to by the Printer object
' This DC needs to remain for the RTF to keep up the WYSIWYG display
PrinterhDC = CreateDC(Printer.DriverName, Printer.DeviceName, 0, 0)

' Tell the RTF to base it's display off of the printer
' at the desired line width
r = SendMessage(RTF.hWnd, EM_SETTARGETDEVICE, PrinterhDC, _
ByVal PrintableWidth)

' Abort the temporary print job used to get printer info
Printer.KillDoc
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' PrintRTF - Prints the contents of a RichTextBox control using the
' provided margins
'
' RTF - A RichTextBox control to print
'
' LeftMarginWidth - Width of desired left margin in twips
'
' TopMarginHeight - Height of desired top margin in twips
'
' RightMarginWidth - Width of desired right margin in twips
'
' BottomMarginHeight - Height of desired bottom margin in twips
'
' Notes - If you are also using WYSIWYG_RTF() on the provided RTF
' parameter you should specify the same LeftMarginWidth and
' RightMarginWidth that you used to call WYSIWYG_RTF()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
TopMarginHeight, RightMarginWidth, BottomMarginHeight)
Dim LeftOffset As Long, TopOffset As Long
Dim LeftMargin As Long, TopMargin As Long
Dim RightMargin As Long, BottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As Rect
Dim rcPage As Rect
Dim TextLength As Long
Dim NextCharPosition As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMode = vbTwips

' Get the offsett to the printable area on the page in twips
LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
PHYSICALOFFSETX), vbPixels, vbTwips)
TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, _
PHYSICALOFFSETY), vbPixels, vbTwips)

' Calculate the Left, Top, Right, and Bottom margins
LeftMargin = LeftMarginWidth - LeftOffset
TopMargin = TopMarginHeight - TopOffset
RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset

' Set printable area rect
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = Printer.ScaleWidth
rcPage.Bottom = Printer.ScaleHeight

' Set rect in which to print (relative to printable area)
rcDrawTo.Left = LeftMargin
rcDrawTo.Top = TopMargin
rcDrawTo.Right = RightMargin
rcDrawTo.Bottom = BottomMargin

' Set up the print instructions
fr.hdc = Printer.hdc ' Use the same DC for measuring and rendering
fr.hdcTarget = Printer.hdc ' Point at printer hDC
fr.rc = rcDrawTo ' Indicate the area on page to draw to
fr.rcPage = rcPage ' Indicate entire size of page
fr.chrg.cpMin = 0 ' Indicate start of text through
fr.chrg.cpMax = -1 ' end of the text

' Get length of text in RTF
TextLength = Len(RTF.Text)

' Loop printing each page until done
Do
' Print the page by sending EM_FORMATRANGE message
NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr)
If NextCharPosition >= TextLength Then Exit Do 'If done then exit
fr.chrg.cpMin = NextCharPosition ' Starting position for next page
Printer.NewPage ' Move on to next page
Printer.Print Space(1) ' Re-initialize hDC
fr.hdc = Printer.hdc
fr.hdcTarget = Printer.hdc
Loop

' Commit the print job
Printer.EndDoc

' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub

Save the project.


Run the project.


Enter or paste some text into the RichTextBox.


Press the Print command button. Note that the printed output should word-wrap at the same locations as displayed on the screen. Also, the output should be printed with the specified one-inch margin all around.
progame 2001-10-02
  • 打赏
  • 举报
回复
GZ
  • 打赏
  • 举报
回复
我要打印的内容是图文混编的!!请高手指教!!!
  • 打赏
  • 举报
回复
我已经实现了除打印分页的所有功能,现在问题是怎样将打印到 picturebox 中的内容分页?
我发现打印到picturebox 中的内容不管怎样,只能显示一页!请问有其他方法将richtextbox 的内容分页预览并打印预览吗?
  • 打赏
  • 举报
回复
我的分全在 这里了,高手来抢啊!!!!!!!
wqb 2001-09-26
  • 打赏
  • 举报
回复
通过richtextbox.text属性获得要打印的文本,picturebox的CurrentX和CurrentY属性设置要打印文本的开始位置。换行等只要加上换行符(chr(13))后系统可以自动控制,你要做的就是确定每一行要打多少字,然后把获得的richtextbox.text再要换行的地方添加上换行符。至于分页,你记下上一页的位置,重新打不就可以了吗?
zslwp 2001-09-26
  • 打赏
  • 举报
回复
要用到四个控件(二个PictureBox,一个VScroll,一个CommandBottun).
即picBox1,picBox2,VScroll1,用Picbox1容器里放一VScroll1和Picbox2.
属性:Command1.caption="OutPut To Pic"
(picbox1,picbox2).Autoredraw=True
(picbox2.scalemode=3)
代码如下:

Command1_click()
Pitcure.Print RichBox.text
if Picbox2.CurrentY > PicBox2.Heigh then
Picbox2.top =Picbox2+139//**139大概为一行**//
picbox2.height=picbox.height+140
同时初始化VScroll1.Value:Max:Min

End Sub

在VScroll_Change()加入滚动代码即可实现。






本DataGridView打印控件和.NET打印控件5.5版(含报表模板设计组件)2014年2月8日修改完成,完全免费,在.NET2.0及以上环境下都可以使用(VB打印、C#打印都是可以的),有帮助文档与使用实例。 与上一版本的5.4版相比,新控件5.5版的主要更改如下: 1、对控件进行了完善,DGVPrint、VB2008Print、EasyReport组件的打印预览速度有较大的提升; 2、VB2008Print与EasyReport添加了二维码打印的功能,该功能使用开源的Zxing.NET实现; 3、添加了DrawDGVEx函数,一方面可以以DGVprint对象的参数在VB2008Print组件打印,另一方面提供了并排打印多个表格的重载函数; 4、DGVPrint组件添加了报表头与报表尾内容的设置属性和参数; 5、DGVPrint与VB2008Print组件在以工资条方式打印表格时,添加了是否自动换行续打的功能(即,如果工资条列太多在一行打印不下时,可以指定自动换行续打); 6、修改了EasyReport组件的设计界面,每个区域的属性放在属性窗口显示; 7、EasyReport组件添加了简单的多栏打印支持功能; 8、VB2008Print组件添加了PrintDGVFixedWidth函数,方便将DataGridView按指定宽度进行打印; 9、去掉了网页打印功能(因为在IE11下没法使用,所以干脆去掉),去掉了打印RDLC报表的功能; 10、其他一些完善,比如在导出Excel时,如果Columheader不可见,则不导出;解决了快速导出Excel时部分特殊列没有表格线问题(比如是数值列,但使用了%等格式);VB2008Print组件添加了PageScale可以指定打印时的缩放比例等等。 本控件特色: 1、强大的DataGridView打印功能,不仅可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印打印DGV表格,基本上能完全按DGV控件本身设置的格式如字体、字号、背景颜色、前景颜色、单元格对齐方式等打印出来,文字图像都可以打印,而且是完全根据表格当前的显示顺序进行打印的,基本上做到了所见即所得的打印。 2、报表设计功能。报表模板设计组件EasyReport与WebEasyReport组件可以设计普通报表、分组报表、套打模板等,分别以DataGridView为数据源。控件的位置以毫米为计量单位,定位准确,很适合套打单据设计。 3、强大的图表打印功能。5.2版控件新增了一个Chartlet的组件,使用非常方便,可以生成柱形图、饼图、折线图等多种图形,而且可以设置2D或3D效果,既可以在打印控件打印出来,也可以在Graphics对象显示。 4、分组汇总打印DataGridVeiw功能,每组还可以自动换新页打印,还可以自动增加行号。 5、强大的文本打印输出功能,控件提供多个文本打印重载函数,打印文本时,如果需要,控件会自动换行和换页打印输出。还增加了以指定行间距及字符间距打印文本的功能,可以用固定行距,也可以用单倍或多倍行距打印文本。 6、强大的绘图功能,基本上.NET的GDI+的绘图函数(如直线、矩形、路径、多边形、曲线等)都有,只有个别函数的名称有点区别。 7、支持同一文档多种版面格式打印(类似于Word的节的功能):对同一份文档,不同的页面可以设置不同的格式(纸张大小、纸张方向、页边 距),只需要在新增一页时在NewPage方法指定要使用的页面格式即可,使用非常简单。 8、报表文件保存功能。本控件允许将当前打印预览的内容保存为报表文件,以后使用本控件重新打开该报表文件即可重现原来保存报表时的打印内容。 9、Excel导出功能,可以将DataGridView和GridView导出为Excel文件,5.2版控件还增加了不依赖Office的导出Excel功能,而且速度非常快,5.4版还增加了合并单元格的导出功能。 10、打印DataGridView时的打印方案保存与读取功能。可以将当前打印参数保存为打印方案文件,或者从保存的打印方案文件读取打印参数。 11、水印打印功能。根据需要,可以在页面打印或不打印以半透明空心文字打印水印。 12、强大的容器控件打印功能(DrawPanel函数)。借助该函数,您只需要在您的容器控件设计好要打印内容打印内容的相对位置,控件轻松帮你打印出来(如果超过一页,控件会自动换页续打)。 13、特殊文字效果打印功能。控件具有打印浮雕文字、阴影文字、空心文字、块文字的功能。 14、页眉页脚既可打印文字,也可打印图像,或者即打印图像又打印输出文字。 15、图像与图标打印输出功能。 16、多表头(跨行跨列的复杂表头)打印功能,多表头组件支持多表头显示打印、单元格内容的合并显示打印与导出。 17、自定义纸张支持功能。 18、纸张背景图片设置打印功能。 19、.NET4.0支持功能(是单独的一个文件) 20、直接打印窗口的TreeView控件功能 21、打印窗口的ListView功能。 22、RichTextBox控件的RTF文本打印功能。 23、斜线表头打印功能(5.4版新增)。 24、二维码打印功能(5.5版本增加)。 我将持续改进该控件,并将不断推出控件的新版本,要查看或下载控件的升级版本,请登陆网站:http://myyouping.download.csdn.net/ 。具体使用方法请参见帮助文件与实例文件,如有疑问或好的建议,请与我联系: 邮箱:myyouping@139.com Q Q:479781502

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧