LoadStringFromFile into compiled script (so availa

2019-06-03 17:28发布

问题:

This question already has an answer here:

  • Inno Setup - How to display localized RTF text in custom page 1 answer

In another question, I asked about importing an RTF file into InnoSetup to use for a custom wizard page:

Import external RTF file for TRichEditViewer?

Unfortunately, I was only able to figure out how to use the answer so that the external file would be loaded if that file existed on the user's system, and in this case, it doesn't exist on the user's system; it's a file I created to display the installer.

I couldn't figure out how to load the external file so that it would be saved inside the compiled installer script and be visible on someone else's system.

Here is the code I put together. I've experimented with creating separate procedures for loading the string, but haven't been able to figure out how to make it work. I'll be grateful for any help:

procedure CreateTheWizardPages;

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

var
     Page: TWizardPage;
     RichEditViewer: TRichEditViewer;
     vDosFolder: String; 
begin

  LoadStringFromFile('c:\dropbox\vdosinst\custom.rtf', rtfstr);

  if RegQueryStringValue(HKEY_CURRENT_USER, 'Software\WPDOS.org','vDosDir', vDosFolder) then 
     begin

    if ((DirExists(vDosFolder + '\62Config')) OR (DirExists(vDosFolder + '\61Config')) OR (DirExists(vDosFolder + '\51Config'))) then
      begin

    Page := CreateCustomPage(wpInfoBefore, 'How to install vDosWP-VDM with an existing vDosWP system', 'Read this message for important information!'); 

    RichEditViewer := TRichEditViewer.Create(Page);
    RichEditViewer.Width := Page.SurfaceWidth;
    RichEditViewer.Height := Page.SurfaceHeight;
    RichEditViewer.Parent := Page.Surface;
    RichEditViewer.ScrollBars := ssVertical;
    RichEditViewer.UseRichEdit := True;
    RichEditViewer.RTFText := rtfstr;
    RichEditViewer.ReadOnly := True;

    end;
  end; 
end;

procedure InitializeWizard();         // ISSI_ added to string
begin
  CreateTheWizardPages;
end;

回答1:

You can add this compile time macro before your code block:

#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

This macro outputs file content as string constant. 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.

Then you can use this macro in [Code] block this way:

RichEditViewer.RTFText := {#emit ReadFileAsStr("custom.rtf")};