LoadStringFromFile into compiled script (so available on system that doesn't have the file)? [duplicate]

淺唱寂寞╮ 提交于 2019-12-23 02:24:33

问题


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")};


来源:https://stackoverflow.com/questions/26315825/loadstringfromfile-into-compiled-script-so-available-on-system-that-doesnt-hav

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