Example: Using OnDropFiles to accept files of different formats

<< Click to display table of contents >>

Example: Using OnDropFiles to accept files of different formats

By default, TRichViewEdit accepts the following types of dropped files:

graphics,

RVF,

RTF,

plain text (ANSI).

This example shows how to use OnDropFiles event to insert all these types of files, and all file formats supported by installed Microsoft Office text import converters.

This example assumes that RVOfficeConverter1:TRVOfficeConverter is placed on the form.

procedure TForm1.RichViewEdit1DropFiles(Sender: TCustomRichViewEdit;

  Files: TStrings; var FileAction: TRVDropFileAction;

var DoDefault: Boolean);

  {....................................................}

  { This function inserts one file FileName }

  function InsertFile(const FileName: String): Boolean;

  var

    gr: TRVGraphic;

    Ext: String;

    i: Integer;

  begin

    Result := False;

    try

      // 1. Trying to insert as a graphic

      gr := RVGraphicHandler.LoadFromFile(FileName);

      if gr <> nil then
      begin

        Sender.InsertPicture('', gr, rvvaBaseline);

        Result := True;

        exit;

      end;

      // 2. Trying to insert as RTF file

      Ext := LowerCase(ExtractFileExt(FileName));

      if Ext='.rtf' then 

      begin

        Sender.InsertRTFFromFileEd(FileName);

        Result := True;

        exit;

      end;

      // 3. Trying to insert as RVF file

      if Ext='.rvf' then

      begin

        Sender.InsertRVFFromFileEd(FileName);

        Result := True;

        exit;

      end;

      // 4. Trying to insert as text file

      if Ext='.txt' then

      begin

        Sender.InsertTextFromFile(FileName);

        Result := True;

        exit;

      end;

      // 5. Trying to insert using office converters

      for i := 0 to RVOfficeConverter1.ImportConverters.Count-1 do

        if Pos(Ext, RVOfficeConverter1.ImportConverters[i].Filter)>0 then

          if RVOfficeConverter1.ImportRTF(FileName, i) then

          begin

            RVOfficeConverter1.Stream.Position := 0;

            Sender.InsertRTFFromFileEd(RVOfficeConverter1.Stream);

            RVOfficeConverter1.Stream.SetSize(0);

            Result := True;

            exit;

          end;

    except

    end;

  end;

  {....................................................}

var i: Integer;

begin

  for i := 0 to Files.Count-1 do

    InsertFile(Files[i]);

  DoDefault := False;

end;