0

我正在尝试将一些罗马尼亚文本写入 RichEdit 组件(Delphi 7),即使我将字体属性 - 字符集设置为“EASTEUROPE_CHARSET”,它也不起作用。

我想要完成的是将一些文本(罗马尼亚语)粘贴到 RichEdit 中,加载到 StringList 中,将属性顺序设置为 true 并将其分配给另一个 RichEdit 组件(按字母顺序对列表进行排序)。

我知道这在 Delphi2009 及更高版本中应该不是问题,但此时我只能使用 Delphi 7。

单词示例:opoziţie,computerizată。

有任何想法吗?

最好的祝福,

4

3 回答 3

3

试试这个代码,它从 RichEdit1 读取文本作为 UNICODE 文本,手动将 S 和 T + Comma 转换为 S 和 T + Cedilla,然后使用 WideCharToMultiByte 将文本转换为代码页 1250。代码点转换需要完成,因为代码第 1250 页仅对 Ş 和 Ţ 基于 cedilla 的版本进行编码,而 Vista 和 Windows 7 下的新罗马尼亚语键盘生成 Ş 和 Ţ 的(正确)基于逗号的版本!

procedure TForm1.Button1Click(Sender: TObject);
var GetTextStruct:GETTEXTEX;
    GetLenStruct:GETTEXTLENGTHEX;
    RequiredBytes:Integer;
    NumberOfWideChars:Integer;
    WideBuff:PWideChar;
    AnsiBuff:PChar;
    i:Integer;
begin
  ;

  // Get length of text
  GetLenStruct.flags := GTL_NUMBYTES or GTL_USECRLF or GTL_PRECISE;
  GetLenStruct.codepage := 1200; // request unicode
  RequiredBytes := SendMessage(RichEdit1.Handle, EM_GETTEXTLENGTHEX, Integer(@GetLenStruct), 0);

  // Prepare structure to get all text
  FillMemory(@GetTextStruct, SizeOf(GetTextStruct), 0);
  GetTextStruct.cb := SizeOf(GetTextStruct);
  GetTextStruct.flags := GT_USECRLF;
  GetTextStruct.codepage := 1200; // request unicode

  WideBuff := GetMemory(RequiredBytes);
  try
    // Do the actual request
    SendMessage(RichEdit1.Handle, EM_GETTEXTEX, Integer(@GetTextStruct), Integer(WideBuff));
    // Replace the "new" diactrics with the old (make Romanian text compatible with code page 1250)
    NumberOfWideChars := RequiredBytes div 2;
    for i:=0 to NumberOfWideChars-1 do
    case Ord(WideBuff[i]) of
      $0218: WideBuff[i] := WideChar($015E);
      $0219: WideBuff[i] := WideChar($015F);
      $021A: WideBuff[i] := WideChar($0162);
      $021B: WideBuff[i] := WideChar($0163);
    end;
    // Convert to code-page 1250
    RequiredBytes := WideCharToMultiByte(1250, 0, WideBuff, -1, nil, 0, nil, nil);
    AnsiBuff := GetMemory(RequiredBytes);
    try
      WideCharToMultiByte(1250, 0, WideBuff, -1, AnsiBuff, RequiredBytes, nil, nil);
      Memo1.Lines.Text := AnsiBuff; // AnsiBuff now contains the CRLF-terminated version of the
                                    // text in RichEdi1, corectly translated to code page 1250
    finally FreeMemory(AnsiBuff);
    end;
  finally FreeMemory(WideBuff);
  end;

end;

然后使用类似的东西将 AnsiString 转换为 UNICODE 并推入 RichEdit。当然,唯一真正的解决方案是切换到 Delphi 2009 或 Delphi 2010 并全部使用 Unicode。

于 2010-07-21T07:16:48.220 回答
3

我已经用 Jedi 的 JvWideEditor 解决了这个问题。代码如下

procedure TForm2.SortUnicode;
var asrt:TWStringList;
    i:Integer;
begin
 JvWideEditor1.Lines.Clear;
 JvWideEditor2.Lines.Clear;
 asrt:=TWStringList.Create;
 if OpenDialog1.Execute then
  begin
   wPath:=OpenDialog1.FileName;
   JvWideEditor1.Lines.LoadFromFile(wPath,[foUnicodeLB]);
   try
   asrt.AddStrings(JvWideEditor1.Lines);
   for i:=asrt.Count-1 downto 0 do 
    begin
      if Trim(asrt.Strings[i])='' then
       asrt.Delete(i);
    end;
   asrt.Duplicates:=dupAccept;
   asrt.CaseSensitive:=true;
   asrt.Sorted:=True;

   JvWideEditor2.Lines.AddStrings(asrt);
   JvWideEditor2.Lines.SaveToFile(GetCurrentDir+'\res.txt',[foUnicodeLB]);
   finally
    FreeAndNil(asrt);
   end;
  end;
end;
于 2010-07-21T08:02:06.040 回答
2

检查 Windows 中的语言设置。如果您正在运行英文窗口,请尝试将“将非 unicode 程序视为...”设置为罗马尼亚语。或者,在本机罗马尼亚 Windows 上运行。要在混合环境中运行(需要同时显示不同的字符集),您可能需要 Unicode。

于 2010-07-20T11:47:44.473 回答