Solved

Hooking to a RichTextBox

Posted on 2004-03-24
6
293 Views
Last Modified: 2013-12-25
Hello,
I wish to I hook onto a Rich Text Box, (RichEdit20 Class) control, from a VB application.
The designated text box, doesn't have to be on a Visual Basic application,
It's easy to get it's handle, but...
how can I get / set it's RTF?
0
Comment
Question by:SiCkness
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10874059
hi SiCkness ,, hope this helps
this is for getting/setting the normal text :( not the rtf but i am working on it :)
Please Inform me with any Questions or ideas ,,,

======================================================

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 ' Used to send Msgs to the RTF Object
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long ' Used to get text from the RTF
Private Const WM_GETTEXT = &HD ' used here to get the textlength
Private Const EM_GETLINE = &HC4 ' Used to get the text of a specific Line in RTF
Private Const EM_GETLINECOUNT = &HBA ' used to get the Nr of lines in the RTF
Private Const EM_LINEINDEX = &HBB ' used to get the index of a specific line In RTF
Private Const EM_LINELENGTH = &HC1 ' Used to get the Line Length in RTF
Private Const WM_SETTEXT = &HC


Private Function GetRTFText(RTFHWND As Long, ReturnedLength As Long, ReturnedNrOfLines As Long) As String
' RTFHWND is the hwnd of the rtf object
' ReturnedLength is the identifier that will hold the length of the rtf
' ReturnedNrOfLines is the identifier that will hold the Nr of Lines of the rtf
 
 Dim RTFNrLines As Long ' Nr of lines
 Dim RTFLineIndex As Long ' Line Index
 Dim RTFTxtLength As Long 'the text length
 Dim RTFText As String 'all the text in the RTF
 Dim TmpBuffer As String 'a temporary buffer
 Dim XLoop As Integer ' the loop counter

'Nr of lines in the RTF Object
RTFNrLines = SendMessage(RTFHWND, EM_GETLINECOUNT, 0, 0)
ReturnedNrOfLines = RTFNrLines
'Length of all the text in the RTF
ReturnedLength = SendMessage(RTFHWND, WM_GETTEXT, 0, 0)

 For XLoop = 0 To RTFNrLines - 1
  RTFLineIndex = SendMessage(RTFHWND, EM_LINEINDEX, XLoop, 0)
  RTFLineLength = SendMessage(RTFHWND, EM_LINELENGTH, RTFLineIndex, 0)
  'recreate the Buffer to retrieve the text of the current Line
  TmpBuffer = Space(RTFLineLength)
  'Retrieve Text of current Line
   Call SendMessageStr(RTFHWND, EM_GETLINE, 0, ByVal TmpBuffer)
  RTFText = RTFText & TmpBuffer & vbCrLf
 Next XLoop

GetRTFText = RTFText ' Return the Text by the function
End Function

Private Sub Command1_Click()

-----------------------------------------------
'to get the text of the RTFBox:
Dim mytext As String
mytext = GetRTFText(RichTextBox1.HWND, 0, 0)
MsgBox mytext

'to clear the RTFTextBox :
Call SendMessageStr(RichTextBox1.HWND, WM_SETTEXT, 0, ByVal (""))
MsgBox "Text Cleared"

'to set the text of the RTFTextBox:
Call SendMessageStr(RichTextBox1.HWND, WM_SETTEXT, 0, ByVal ("Blablabla OHDev .... BlaBlaBla :)"))


----------------------------------
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10879180
still interested ?
0
 

Author Comment

by:SiCkness
ID: 10879260
oh I'm interested, and cool, WM_GETTEXT is a new thing for me
thanks, everything if very clear... good job

any chance you figure out how to get the RTF format?

thanks again
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Accepted Solution

by:
OHDev2004 earned 250 total points
ID: 10880472
yeah yeah ,, After a while fighting trough VB :)
Here it is Full functioning working code ,, lol

the previous comment was only to get the normal text not the RTF Code of it ,, after trying for a while i came up with this routine and yep it works :) but first i had to write it to a stream then to a file ,, then read it from a file because the previous comment was returning Plain text not RTF !

Here is the Full Code:
===================================

'In a Module
option explicit
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal RTFHwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long ' Used to send Msgs to the RTF Object
Private Declare Function WriteFile Lib "kernel32" (ByVal FileHndl As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Const WM_SETTEXT = &HC
Public Const WM_PASTE = &H302
Public Const WM_USER = &H400
Public Const EM_GETTEXTEX = (WM_USER + 94)
Public Const EM_STREAMOUT = (WM_USER + 74)
Public Const SF_TEXT = &H1
Public Const SF_RTF = &H2

Public Const OPEN_ALWAYS = 4
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Const GENERIC_WRITE = &H40000000

Public Type EDITSTREAM
 dwCookie As Long
 dwError As Long
 pfnCallback As Long
End Type


Public Function GetRTFText(RTFHwnd As Long, XType As String) As String
' RTFHwnd ---> The HWND of the RTFObject
' XType -----> The type of text to be retrieved ( "RTF" or "TXT")

Dim FileHndl As Long
Dim RtfStream As EDITSTREAM
Dim XFreeFile As Integer
Dim TBuffer As String, TmpStr As String
Dim TextType As Long ' Rtf or TXT

If XType = "RTF" Then 'to set the type of text to be retrieved
TextType = SF_RTF
Else
TextType = SF_TEXT
End If

On Error Resume Next

FileHndl = CreateFile(App.path & "\Tmp1.txt", GENERIC_WRITE, 0&, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
 If FileHndl <> 0 Then
    RtfStream.dwCookie = FileHndl
    RtfStream.pfnCallback = Val(AddressOf SaveCallBack)
    RtfStream.dwError = 0&
       SendMessage RTFHwnd, EM_STREAMOUT, ByVal TextType, RtfStream
       CloseHandle FileHndl
    End If
   
XFreeFile = FreeFile
 Open App.path & "\Tmp1.txt" For Input As #XFreeFile
   While Not EOF(XFreeFile)
   Input #XFreeFile, TBuffer
    TmpStr = TmpStr & TBuffer & vbNewLine
    Wend
    Close #XFreeFile
 GetRTFText = TmpStr
Call Kill(App.path & "\Tmp1.txt")
End Function

Public Function SaveCallBack(ByVal dwCookie As Long, pbBuff As Long, ByVal cb As Long, pcb As Long) As Long
    Dim retVal As Long
    On Error Resume Next
    retVal = WriteFile(dwCookie, pbBuff, cb, pcb, ByVal 0&)
    SaveCallBack = IIf(retVal <> 0, 0, Err.LastDllError)
End Function

'---------------------------------
'Examples :

Dim ReturnedText As String

'to get the text in RTF Format:
ReturnedText = GetRTFText(RichTextBox1.hWnd, "RTF")

'to get the text in PlainText Format:
ReturnedText = GetRTFText(RichTextBox1.hWnd, "TXT")

'to clear the RTFTextBox :
Call SendMessage(RichTextBox1.hWnd, WM_SETTEXT, 0, ByVal (""))

'to set RichText or Plaintext to the RTFTextBox:
Dim StrRtf As String
StrRtf = "{\rtf1\fbidis\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset0 MS Sans Serif;}}" & vbNewLine & "{\colortbl ;\red0\green128\blue0;}" & vbNewLine & "\viewkind4\uc1\pard\ltrpar\cf1\lang3073\f0\fs20 KPD-Team 1998\cf0\f1\fs17" & vbNewLine & "\par }"
'or
'StrRtf = "Blablabla OHDev :) Blablabla"
Call SendMessage(RichTextBox1.hWnd, WM_SETTEXT, 0, ByVal (StrRtf))
0
 

Author Comment

by:SiCkness
ID: 10887502
Very Interesting ...
Thanks alot OHdev!
Just know I looked alot for this answer, numerus website and codes...
and your the only one who did it

Cheers
Thanks again!
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10887675
Thanks SiCkness
I really enjoyed this one ,, you know with vb and some API's you can make magic :)
hope you don't use it in meany things ,, lol
Thanks again ! ,, OHDev
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now