VB6中如何将RichTextBox控件中加载的RTF格式内容以图像的形式输出?
相关问题:
文字文件转化为图片文件的简易办法(http://blog.csdn.net/cso/archive/2006/03/22/79084.aspx)一文中有不适合的地方,1、如果这个RichTextBox控件不显示或者部分被覆盖则不能正确显示图像,2、超出RichTextBox控件窗口区域的内容不能复制到图像中。
另一文《RTF文档导成图片》(http://blog.csdn.net/hbxtlhx/archive/2007/07/12/1686671.aspx)由于是讲在.net环境下的解决办法,未测试,也不知能否达到我的要求。
我的思路是:通过RichTextBox.hWnd 取得 hDC=GetDC(RichTextBox.hWnd),然后通过这个hDC在内存中取得它的整幅图像,不知可不可行,由于本人对API不太懂,具体的代码也不会写,因此请各位大侠帮忙,多谢。
--------------------编程问答-------------------- 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 = "test " + vbCrLf
.SelItalic = True
.SelAlignment = rtfCenter
.SelColor = vbRed
.SelText = "http://www.csdn.net"
End With
OverlayText Picture1, RichTextBox1, 1000, Picture1.Width - 1000, 400, Picture1.Height - 400
End Sub
--------------------编程问答-------------------- 可以参考一下 EM_FORMATRANGE 消息(http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_formatrange.asp)。这个消息可以让RichEdit按照指定的设备来格式化其文本。你可以利用SendMessage,让RichEdit将文本输出到Picture中。 --------------------编程问答-------------------- MARK --------------------编程问答-------------------- 这里有你想要的VB代码:http://www.cnpopsoft.com/article.asp?id=14
补充:VB , API