Page 1 of 1
How to send text to TRichView
Posted: Tue Jul 15, 2008 7:03 pm
by MikeZ
Hello!
I have the first program with TRichView and the second one to manage it.
I do SendMessage(Wnd, WM_GETTEXT, 0, 0) to get text from 1st prog's TRichView. Unfortunatelly WM_SETTEXT does not work. How I can:
1. Obtain RTF-text from TRichView to the other program? (WM_GETTEXT get only plain text)
2. Set text to the TRichView in the other program? (Plain and RTF)
Best regards!
Posted: Tue Jul 15, 2008 8:27 pm
by Sergey Tkachenko
These messages are not supported. I tried to implement WM_SETTEXT, but it conflicted with VCL implementation of this message, so I had to disable it for Delphi 2005-2007.
Only EM_*** messages for text are supported.
And I do not know A standard way to get/set RTF from/to window.
I am afraid you need to create a component inherited from TRichViewEdit and implement your own messages in it.
Posted: Wed Jul 16, 2008 9:18 am
by MikeZ
Thanks.
Posted: Wed Jul 16, 2008 10:33 am
by MikeZ
Code (copy/paste from LoadFromStream) to send plain text (or RTF with 2 lines correction) to a window:
Code: Select all
Function AdjustLineBreaks(Dest, Source: PChar): Integer; Assembler;
Asm
PUSH ESI
PUSH EDI
MOV EDI,EAX
MOV ESI,EDX
MOV EDX,EAX
CLD
@@1: LODSB
@@2: OR AL,AL
JE @@4
CMP AL,0AH
JE @@3
STOSB
CMP AL,0DH
JNE @@1
MOV AL,0AH
STOSB
LODSB
CMP AL,0AH
JE @@1
JMP @@2
@@3: MOV EAX,0A0DH
STOSW
JMP @@1
@@4: STOSB
LEA EAX,[EDI-1]
SUB EAX,EDX
POP EDI
POP ESI
End;
Function StreamLoad(dwCookie: LongInt; pbBuff: pByte;
CB: LongInt; Var pcb: LongInt): LongInt; Stdcall;
Const
ReadError = $0001;
Var
Buffer, pBuff : PChar;
StreamInfo : PRichEditStreamInfo;
Begin
Result := NOERROR;
StreamInfo := PRichEditStreamInfo(POINTER(dwCookie));
Buffer := StrAlloc(CB + 1);
Try
CB := CB Div 2;
pcb := 0;
pBuff := Buffer + CB;
Try
If StreamInfo^.Converter <> Nil Then
pcb := StreamInfo^.Converter.ConvertReadStream(StreamInfo^.Stream, pBuff, CB);
If pcb > 0 Then
Begin
pBuff[pcb] := #0;
If pBuff[pcb - 1] = #13 Then pBuff[pcb - 1] := #0;
pcb := AdjustLineBreaks(Buffer, pBuff);
Move(Buffer^, pbBuff^, pcb);
End;
Except
Result := ReadError;
End;
Finally
StrDispose(Buffer);
End;
End;
Procedure SetText(Wnd: HWnd; S: String);
Var
MS : TMemoryStream;
EditStream : TEditStream;
Position : LongInt;
TextType : LongInt;
StreamInfo : TRichEditStreamInfo;
Converter : TConversion;
RichEdit : TCustomRichEdit;
Begin
MS := TMemoryStream.Create;
MS.Write(PChar(S)^, Length(S));
MS.Position := 0;
StreamInfo.Stream := MS;
RichEdit := TCustomRichEdit.Create(Nil);
Converter := RichEdit.DefaultConverter.Create;
StreamInfo.Converter := Converter;
Try
With EditStream Do
Begin
dwCookie := LongInt(POINTER(@StreamInfo));
pfnCallback := @StreamLoad;
dwError := 0;
End;
Position := MS.Position;
TextType := SF_TEXT;
SendMessage(Wnd, EM_STREAMIN, TextType, LongInt(@EditStream));
If (TextType = SF_RTF) And (EditStream.dwError <> 0) Then
Begin
MS.Position := Position;
TextType := SF_TEXT;
SendMessage(Wnd, EM_STREAMIN, TextType, LongInt(@EditStream));
If EditStream.dwError <> 0 Then
ShowMessage('Error!');
End;
Finally
Converter.Free;
End;
End;
It works in TRichEdit in the
my program, but does not in the TRichView in the
other one. Please try (If you want and have time) it to send text to TRichView from your prog to itself inside one application (I think it can be some feature of mine "
other" application to prevent text modification).
Posted: Tue Jul 22, 2008 12:54 pm
by Sergey Tkachenko
EM_STREAMIN and EM_STREAMOUT are not implemented in TRichView/TRichViewEdit.
It's not very hard to implement them, I'll try to do it in the next update.