Import external RTF file for TRichEditViewer?

后端 未结 2 974
庸人自扰
庸人自扰 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: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.

提交回复
热议问题