Richedit text from a string

Richedit text from a string

Postby StefanHaupt » Sun Jan 08, 2012 2:24 pm

Hi all,

I am trying to display a rtf string in a richedit control without success. What is wrong with the following code

Code: Select all  Expand view
cText := "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}"+CRLF+;
"{\colortbl ;\red0\green77\blue187;\red192\green80\blue77;}"+CRLF+;
"{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\cf1\lang1031\f0\fs22 This is \b colored \b0 text\cf0 . \cf2 The background is color 1 and the foreground is color 2\cf0\par"+CRLF+;
"}"
oRtf := TRichEdit():Redefine( 4000, { || "" }, oDlg )
//oRtf:lHighLight = .f.
oRtf:LoadRTF (cText)  // does not work
oRTF:Refresh()
//oRtf:LoadFromRTFFile( "Register.rtf" )  / from a file it is working


IsRTF (cText) returns .t., but it´s only working if I load it from a file.

Any help appreciated
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Richedit text from a string

Postby anserkk » Mon Jan 09, 2012 5:22 am

The following code is working fine here.

Instead of
oRtf:LoadRTF (cText)
Try
oRtf:SetText( cText )


Test.Prg
Code: Select all  Expand view
#Include "FiveWin.ch"
//----------------------------------------------------------------------------//
Function Main()

   Local oDlg, oRich, cRtf
   Local hRichDLL := LoadLibrary( "riched20.dll" )
   
   cRtf:="{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}"+CRLF+;
            "{\colortbl ;\red0\green77\blue187;\red192\green80\blue77;}"+CRLF+;
            "{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\cf1\lang1031\f0\fs22 This is \b colored \b0 text\cf0 . \cf2 The background is color 1 and the foreground is color 2\cf0\par"+CRLF+;
            "}"  

   DEFINE DIALOG oDlg NAME "Test"

   oRich = TRichEdit():Redefine( 100, { || "" }, oDlg )
   oRich:SetText( cRtf )
   oDlg:bStart = { || oRich:SetPos( 0 )}

   ACTIVATE DIALOG oDlg CENTERED

   FreeLibrary( hRichDLL )

Return nil


Test.RC
Code: Select all  Expand view
#define IDC_EDIT1   101
Test DIALOG 41, 64, 409, 199
STYLE DS_MODALFRAME | 0x4L | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Testing the RichEdit control"
FONT 8, "MS Sans Serif"
{
 CONTROL "", 100, "RichEdit20A", 4100 | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP, 6, 12, 398, 163
 PUSHBUTTON "&Load text file", 110, 5, 179, 50, 16
}


Image

Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Richedit text from a string

Postby StefanHaupt » Mon Jan 09, 2012 2:47 pm

Anser,

thanks, you put me in the right direction.

I tried :SetText(), the text was shown with all the control characters. The difference was in the .rc file. PellesC inserted a controltype "RichEdit20W", which is not working. If I change this to "RichEdit20A", everything works fine.
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Richedit text from a string

Postby reinaldocrespo » Thu Jan 12, 2012 8:38 pm

Stefan;

Check this thread:

viewtopic.php?f=3&t=15270

I had shown a short-fast-very simple function to extract text from an rtf field using regular expressions. The nice thing about it is that you don't need to create the rtf object in order to execute GetText() method.



Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Richedit text from a string

Postby StefanHaupt » Fri Jan 13, 2012 12:25 pm

Reinaldo,

thanks, I know this thread and your function is very usefull.

But in my case I wanted to display a formatted string and so I needed the rtf-class. And I found this class does work with other controltypes than "RichEdit20A".
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Richedit text from a string

Postby StefanHaupt » Fri Jan 13, 2012 2:20 pm

RichEdit20A is for Ansitext, RichEdit20W for Unicode. It seems that ::SetText() does not work with unicode, ::Loadrtf () does.
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 80 guests