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

跟着Jacky学AI
新星创作者: 人工智能技术领域
2001-09-25 04:30:26
...全文
318 7 打赏 收藏 转发到动态 举报
写回复
用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
跟着Jacky学AI 2001-10-02
  • 打赏
  • 举报
回复
我要打印的内容是图文混编的!!请高手指教!!!
跟着Jacky学AI 2001-09-27
  • 打赏
  • 举报
回复
我已经实现了除打印分页的所有功能,现在问题是怎样将打印到 picturebox 中的内容分页?
我发现打印到picturebox 中的内容不管怎样,只能显示一页!请问有其他方法将richtextbox 的内容分页预览并打印预览吗?
跟着Jacky学AI 2001-09-26
  • 打赏
  • 举报
回复
我的分全在 这里了,高手来抢啊!!!!!!!
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()加入滚动代码即可实现。






代码转载自:https://pan.quark.cn/s/a4b39357ea24 LA 1010 逻辑分析仪被视作一种效能卓越的数字信号分析设备,其核心功能在于对数字通信协议,例如I²C,进行检测与解构。本资源将集阐述LA 1010的操作流程以及如何借助该设备对I²C协议的波形展开分析。 确保逻辑分析仪被正确地连接至目标系统是极为关键的环节。在运用LA 1010的过程,必须将分析仪的通道0与通道1分别对应连接至目标装置的SCL(时钟)与SDA(数据)线路。务必保证连接的稳固性且无任何干扰因素,以此确保数据采集的精确度。 随后,需要设定采样参数。采样频率对于能否成功捕捉到信号具有决定性的作用。针对I²C协议,通常选用的采样频率范围介于100kHz到400kHz之间,这一范围的选择取决于实际应用场景I²C总线的运行速度。然而,LA 1010所能达到的最高采样速率可能高达500MHz,因此需要根据具体需求进行相应的调整。此外,还必须精心挑选适配目标设备工作电压的电压等级,例如3.3V、5V或1.8V。 在软件操作层面,需要选取恰当的通道与协议种类,即I²C。一旦启动采样,逻辑分析仪便开始记录相关数据。软件界面通常会实时展示波形图,为观察与分析提供便利。 关于I²C协议波形的解读,我们可以遵循以下步骤: 1. 总线处于空闲状态时:SCL与SDA均维持在高电平位置,这表明总线当前未进行数据传输。 2. 传输起始信号:当SCL处于高电平期间,SDA线从高电平转换至低电平,此动作标志着数据传输的开始。 3. 地址、数据及应答的识别:在每一个SCL高电平脉冲的持续时间内,SDA线上的电平状态代表了数据位。地址与数据的传输均为双向过程,而读写标识则由SDA线上的电平来...
内容概要:本文介绍了一种基于有限元分析(FEA)获取的磁通链接图来构建高精度永磁同步电机(PMSM)数学模型的方法,并在Simulink环境实现仿真。该方法通过有限元软件提取电机在不同工况下的非线性磁通特性数据,建立精确的磁链-电流-转子位置映射关系,有效克服了传统线性模型在反映铁芯饱和、交叉耦合等非线性效应方面的局限性。所构建的模型能够更真实地模拟PMSM的实际电磁行为,适用于高性能控制算法(如矢量控制、直接转矩控制、模型预测控制等)的研发与验证,显著提升了控制系统在动态响应、效率优化和稳定性方面的仿真准确性。; 适合人群:具备电机控制理论基础、熟悉有限元分析与Simulink仿真的研究生、科研人员及从事电机设计与驱动系统开发的工程技术人员。; 使用场景及目标:①用于高精度永磁同步电机控制系统的设计、仿真与性能评估;②支持先进控制策略(如MPC、滑模控制、自适应控制)在非线性电机模型上的验证与优化;③为电机参数敏感性分析、能效优化、故障诊断与容错控制研究提供可靠的仿真平台; 阅读建议:建议读者结合ANSYS Maxwell、JMAG等有限元工具与Matlab/Simulink进行联合仿真,掌握从电磁场建模、数据提取、查表插值到控制系统集成的完整流程,并参考文模型架构与数据处理方法,逐步复现、验证并拓展适用于特定电机结构的高保真仿真模型。

7,787

社区成员

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

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