 |
可以参考一下 EM_FORMATRANGE 消息(http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_formatrange.asp)。这个消息可以让RichEdit按照指定的设备来格式化其文本。你可以利用SendMessage,让RichEdit将文本输出到Picture中。下面的例子是参考VB Helper的例子(http://www.vb-helper.com/howto_overlay_rich_text.html)改写的。
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Const WM_USER = &H400
Private Const EM_FORMATRANGE = (WM_USER + 57)
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type CHARRANGE
char_before As Long
char_after As Long
End Type
Private Type FORMATRANGE
hdc As Long
target_hdc As Long
target_area As RECT
entire_area As RECT
char_range As CHARRANGE
End Type
' Overlay the text on the PictureBox inside
' the specified rectangle (in twips).
Private Sub OverlayText(ByVal pic As PictureBox, ByVal rch _
As RichTextBox, ByVal xmin As Single, ByVal xmax As _
Single, ByVal ymin As Single, ByVal ymax As Single)
Dim format_range As FORMATRANGE
' Prepare the FORMATRANGE structure.
With format_range
.hdc = pic.hdc
.target_hdc = pic.hdc
With .entire_area
.Left = 0
.Right = pic.ScaleX(pic.ScaleWidth, _
pic.ScaleMode, vbTwips)
.Top = 0
.Bottom = pic.ScaleY(pic.ScaleHeight, _
pic.ScaleMode, vbTwips)
End With
With .target_area
.Left = xmin
.Right = xmax
.Top = ymin
.Bottom = ymax
End With
With .char_range
.char_before = 0
.char_after = -1
End With
End With
' Display as much text as will fit.
' Setting wParam = True makes the RichTextBox
' display the text instead of just measuring it.
SendMessage rch.hwnd, _
EM_FORMATRANGE, True, format_range
' Clear resources allocated by the RichTextBox.
SendMessage rch.hwnd, EM_FORMATRANGE, False, ByVal 0&
End Sub
Private Sub Command1_Click()
With RichTextBox1
.SelFontSize = 18
.SelAlignment = rtfCenter
.SelBold = True
.SelText = "问专家" + vbCrLf
.SelItalic = True
.SelAlignment = rtfCenter
.SelColor = vbRed
.SelText = "http://www.china-askpro.com"
End With
OverlayText Picture1, RichTextBox1, 1000, Picture1.Width - 1000, 400, Picture1.Height - 400
End Sub
VB.NET的用户可以参考微软的Knowledge Base的文章:“811401“ HOW TO:使用 Microsoft Visual Basic .NET 打印 RichTextBox 的内容”。
此问题由李海、happywqw回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, Windows 9x控件, listview, treeview, monthview, progress bar, 图形、图象, picture, graph, image, draw。
|