How to send text to TRichView

General TRichView support forum. Please post your questions here
Post Reply
MikeZ
Posts: 3
Joined: Tue Jul 15, 2008 6:45 pm

How to send text to TRichView

Post 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!
Sergey Tkachenko
Site Admin
Posts: 17571
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
MikeZ
Posts: 3
Joined: Tue Jul 15, 2008 6:45 pm

Post by MikeZ »

Thanks.
MikeZ
Posts: 3
Joined: Tue Jul 15, 2008 6:45 pm

Post 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).
Sergey Tkachenko
Site Admin
Posts: 17571
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
Post Reply