Import external RTF file for TRichEditViewer?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 09:46:58

问题


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 content of the viewer? Or do I have to keep the actual RTF code in the script?

Any help gratefully received.


回答1:


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 .




回答2:


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.



来源:https://stackoverflow.com/questions/26209200/import-external-rtf-file-for-tricheditviewer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!