LoadStringFromFile编译成脚本(如此系统上没有该文件)? [英] LoadStringFromFile into compiled script (so available on system that doesn't have the file)?

查看:421
本文介绍了LoadStringFromFile编译成脚本(如此系统上没有该文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个问题中,我询问了如何将一个RTF文件导入到InnoSetup中以用于自定义向导页面:

为TRichEditViewer导入外部RTF文件?

不幸的是,我只是能够弄清楚如何使用这个答案,这样如果这个文件存在于用户的系统中,这个外部文件就会被加载,在这种情况下,这个文件在用户的系统上是不存在的。这是我创建的一个文件来显示安装程序。

我无法弄清楚如何加载外部文件,以便将其保存在编译的安装脚本中,在别人的系统上可见。



这是我放在一起的代码。我已经尝试创建单独的过程来加载字符串,但一直没能弄清楚如何使其工作。

 过程CreateTheWizardPages; 

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

var
页面:TWizardPage;
RichEditViewer:TRichEditViewer;
vDosFolder:String;
begin

LoadStringFromFile('c:\dropbox\vdosinst\custom.rtf',rtfstr);如果RegQueryStringValue(HKEY_CURRENT_USER,'Software\WPDOS.org','vDosDir',vDosFolder)那么


begin

if((DirExists(vDosFolder +或者(DirExists(vDosFolder +'\51Config')))然后
begin

Page:= CreateCustomPage(wpInfoBefore,'如何使用现有vDosWP系统安装vDosWP-VDM','阅读此消息以获取重要信息!

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;

程序InitializeWizard(); // ISSI_添加到字符串
begin
CreateTheWizardPages;
end;


解决方案

您可以在代码块之前添加此编译时宏:

  #pragma parseroption -p- 


#define FileHandle
#define FileLine
#define FileName
#define结果
#sub ProcessFileLine
#define FileLine = FileRead(FileHandle)
#if Len(Result)> 0&& !FileEof(FileHandle)
#expr Result = Result##10#13 + \\\

#endif
#if FileLine!='\ 0'
# expr Result = Result +'+ FileLine +'
#endif
#endsub
#sub ProcessFile
#{FileHandle = FileOpen(FileName); \
FileHandle&& !FileEof(文件句柄); } \
ProcessFileLine
#if FileHandle
#expr FileClose(FileHandle)
#endif
#endsub

#define ReadFileAsStr (str AFileName)\
Result ='',FileName = AFileName,ProcessFile,Result

该宏以字符串常量输出文件内容。这适用于大多数RTF文件,但RTF内的一些字符可能会破坏此代码。
要解决这个问题,你需要转义',可能是 ProcessFileLine 子。

然后你可以在中使用这个宏[Code] block this:

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


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;

解决方案

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

这篇关于LoadStringFromFile编译成脚本(如此系统上没有该文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆