Using TRichEdit at runtime without defining a parent

后端 未结 4 1679
囚心锁ツ
囚心锁ツ 2020-12-02 19:02

I need to use a TRichEdit at runtime to perform the rtf to text conversion as discussed here. I succeded in doing this but I had to set a dummy form as parent if not I canno

4条回答
  •  执念已碎
    2020-12-02 19:46

    TRichEdit control is an wrapper around the RichEdit control in Windows. Windows's controls are... well.. Windows, and they need an Window Handle to work. Delphi needs to call CreateWindow or CreateWindowEx to create the Handle, and both routines need an valid parent Window Handle to work. Delphi tries to use the handle of the control's parent (and it makes sense!). Happily one can use an alternative constructor (the CreateParanted(HWND) constructor) and the nice people at Microsoft made up the HWND_MESSAGE to be used as parent for windows that don't actually need a "window" (messaging-only).

    This code works as expected:

    procedure TForm2.Button2Click(Sender: TObject);
    var R:TRichEdit;
        L:TStringList;
    begin
      R := TRichEdit.CreateParented(HWND_MESSAGE);
      try
        R.PlainText := False;
        R.Lines.LoadFromFile('C:\Temp\text.rtf');
        R.PlainText := True;
    
        Memo1.Lines.Text := R.Lines.Text;
      finally 
        R.Free;
      end;
    end;
    

提交回复
热议问题