Import external RTF file for TRichEditViewer?

后端 未结 2 941
庸人自扰
庸人自扰 2021-01-14 13:16

I\'m using TRichEditViewer on a custom page in an Inno Setup script. Is it possible to read an external RTF file into a variable, and use that variable as the c

相关标签:
2条回答
  • 2021-01-14 13:33

    You should be able to use LoadStringFromFile to read an RTF file into a string. From the Inno documentation:

    Prototype:

    function LoadStringFromFile(const FileName: String; var S: AnsiString): Boolean;

    Description:

    Loads the specified binary or non Unicode text file into the specified string. Returns True if successful, False otherwise.

    You should be able to define a string type variable for ANSI Inno Setup or AnsiString type variable for Unicode Inno Setup using something like:

    var
    #ifndef UNICODE
      rtfstr: string;
    #else
      rtfstr: AnsiString;
    #endif
    

    Then in the code:

    LoadStringFromFile('filenamehere.rtf', rtfstr);
    

    And then use code similar to what is below. In this example assume oRichViewer is a TRichEditViewer object:

    oRichViewer.UseRichEdit := True;
    oRichViewer.RTFText := rtfstr;
    

    This should have the effect of putting rtfstr which we loaded from the file earlier into the TRichEditViewer .

    0 讨论(0)
  • 2021-01-14 13:48

    If you don't want to copy your .rtf file to the temporary folder before displaying it, and want to fill variable with RTF content at compile time - you can use Inno Setup Preprocessor.

    First, create a macros ReadFileAsStr:

    #pragma parseroption -p- 
    
    #define FileHandle
    #define FileLine
    #define FileName
    #define Result
    #sub ProcessFileLine
      #define FileLine = FileRead(FileHandle) 
      #if Len(Result) > 0 && !FileEof(FileHandle)
        #expr Result = Result + "#10#13 +  \n"
      #endif
      #if FileLine != '\0'
        #expr Result = Result + "'" + FileLine + "'"
      #endif
    #endsub
    #sub ProcessFile
      #for {FileHandle = FileOpen(FileName); \
        FileHandle && !FileEof(FileHandle); ""} \
        ProcessFileLine
      #if FileHandle
        #expr FileClose(FileHandle)
      #endif   
    #endsub
    
    #define ReadFileAsStr(str AFileName) \
      Result = '', FileName = AFileName, ProcessFile, Result
    

    And use it:

    var
      rtfstr := {#emit ReadFileAsStr("path_to.rtf")};
    

    This works for most RTF files, but some characters inside RTF can broke this code. To fix this you need to escape ' and may be some other characters inside ProcessFileLine sub.

    0 讨论(0)
提交回复
热议问题