delphi - 使用指针和资源字符串

标签 delphi lazarus freepascal

我试图显示附加到Resourcestring的常量数组的内容;但它无法正常运行(Showmessage应该显示“ Primavera”),但是在完成应用程序时会收到空白消息和异常。例如,示例代码在Lazurus中运行良好。我想念一些东西...

unit U_Translate;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

Resourcestring

  RS1 = 'Primavera'; RS2 = 'Verano'; RS3 = 'Otoño'; RS4 = 'Invierno';

Const
 CEstacion: Array [1..4] of ^String = (@RS1,@RS2,@RS3,@RS4);

var Form1: TForm1;

implementation
$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 ShowMessage (CEstacion[1]^);
end;

end.

最佳答案

如果在const数组中使用资源字符串,则在单元初始化期间将初始化数组的值。那时,它们是从二进制资源加载的。它们为空的原因仅意味着您拥有一些翻译资源,而您尚未翻译那些资源字符串。

以这种方式使用资源字符串还意味着您不能只在运行时更改语言并更改值(除非您显式地重新初始化数组,但这需要大量显式的低级代码-请参见System._InitResStrings

另一种方法是像这样使用PResStringRec数组:

const
  CEstacion: array [1..4] of PResStringRec = (@RS1,@RS2,@RS3,@RS4);


然后像这样调用它(当使用资源字符串时,编译器通常会为您插入对LoadResString的调用)

ShowMessage(LoadResString(CEstacion[1]));


作为语言扩展,我们需要的是能够声明array of resourcestring,编译器会将其翻译为PResStringRec数组,并插入LoadResString调用,就像普通资源字符串一样。

关于delphi - 使用指针和资源字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50562012/

相关文章:

delphi - Delphi 6 中记录常量可以是记录的记录吗?

delphi - Val 不适用于 UInt64?

sqlite - FireDac 未将 "&"符号插入 SQLITE varchar

delphi - Pascal 中字符串的格式化输入

c - 是否有类似 access() 的功能但针对特定的用户 ID?

freepascal - 为什么属性默认值在 free pascal 中不起作用?

android - 我如何让我的代码正确地从零开始工作?

string - 删除 Pascal 字符串中的短词

Free Pascal Lazarus 1.0.2 (FPC 2.6.0) 中的 WinAPI 函数

pascal - 集合中允许有负整数吗?